Skip to content

Commit

Permalink
chore(docs): update readme (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] committed Dec 19, 2024
1 parent 029360f commit 1f3da27
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 45 deletions.
88 changes: 50 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ Use `OpenlayerOkHttpClient.builder()` to configure the client.
Alternately, set the environment with `OPENLAYER_API_KEY`, and use `OpenlayerOkHttpClient.fromEnv()` to read from the environment.

```java
import com.openlayer.api.client.OpenlayerClient;
import com.openlayer.api.client.okhttp.OpenlayerOkHttpClient;

OpenlayerClient client = OpenlayerOkHttpClient.fromEnv();

// Note: you can also call fromEnv() from the client builder, for example if you need to set additional properties
Expand All @@ -68,10 +71,10 @@ Read the documentation for more configuration options.

### Example: creating a resource

To create a new inference pipeline data, first use the `InferencePipelineDataStreamParams` builder to specify attributes,
then pass that to the `stream` method of the `data` service.
To create a new inference pipeline data, first use the `InferencePipelineDataStreamParams` builder to specify attributes, then pass that to the `stream` method of the `data` service.

```java
import com.openlayer.api.core.JsonValue;
import com.openlayer.api.models.InferencePipelineDataStreamParams;
import com.openlayer.api.models.InferencePipelineDataStreamResponse;
import java.util.List;
Expand All @@ -85,7 +88,7 @@ InferencePipelineDataStreamParams params = InferencePipelineDataStreamParams.bui
.costColumnName("cost")
.timestampColumnName("timestamp")
.build()))
.row(List.of(InferencePipelineDataStreamParams.Row.builder()
.rows(List.of(InferencePipelineDataStreamParams.Row.builder()
.putAdditionalProperty("user_query", JsonValue.from("what is the meaning of life?"))
.putAdditionalProperty("output", JsonValue.from("42"))
.putAdditionalProperty("tokens", JsonValue.from(7))
Expand All @@ -104,14 +107,14 @@ InferencePipelineDataStreamResponse response = client.inferencePipelines().data(

To make a request to the Openlayer API, you generally build an instance of the appropriate `Params` class.

In [Example: creating a resource](#example-creating-a-resource) above, we used the `InferencePipelineDataStreamParams.builder()` to pass to
the `stream` method of the `data` service.
In [Example: creating a resource](#example-creating-a-resource) above, we used the `InferencePipelineDataStreamParams.builder()` to pass to the `stream` method of the `data` service.

Sometimes, the API may support other properties that are not yet supported in the Java SDK types. In that case,
you can attach them using the `putAdditionalProperty` method.
Sometimes, the API may support other properties that are not yet supported in the Java SDK types. In that case, you can attach them using the `putAdditionalProperty` method.

```java
import com.openlayer.api.models.core.JsonValue;
import com.openlayer.api.core.JsonValue;
import com.openlayer.api.models.InferencePipelineDataStreamParams;

InferencePipelineDataStreamParams params = InferencePipelineDataStreamParams.builder()
// ... normal properties
.putAdditionalProperty("secret_param", JsonValue.from("4242"))
Expand All @@ -125,15 +128,19 @@ InferencePipelineDataStreamParams params = InferencePipelineDataStreamParams.bui
When receiving a response, the Openlayer Java SDK will deserialize it into instances of the typed model classes. In rare cases, the API may return a response property that doesn't match the expected Java type. If you directly access the mistaken property, the SDK will throw an unchecked `OpenlayerInvalidDataException` at runtime. If you would prefer to check in advance that that response is completely well-typed, call `.validate()` on the returned model.

```java
import com.openlayer.api.models.InferencePipelineDataStreamResponse;

InferencePipelineDataStreamResponse response = client.inferencePipelines().data().stream().validate();
```

### Response properties as JSON

In rare cases, you may want to access the underlying JSON value for a response property rather than using the typed version provided by
this SDK. Each model property has a corresponding JSON version, with an underscore before the method name, which returns a `JsonField` value.
In rare cases, you may want to access the underlying JSON value for a response property rather than using the typed version provided by this SDK. Each model property has a corresponding JSON version, with an underscore before the method name, which returns a `JsonField` value.

```java
import com.openlayer.api.core.JsonField;
import java.util.Optional;

JsonField field = responseObj._field();

if (field.isMissing()) {
Expand All @@ -155,6 +162,8 @@ if (field.isMissing()) {
Sometimes, the server response may include additional properties that are not yet available in this library's types. You can access them using the model's `_additionalProperties` method:

```java
import com.openlayer.api.core.JsonValue;

JsonValue secret = projectCreateResponse._additionalProperties().get("secret_field");
```

Expand All @@ -168,31 +177,33 @@ This library throws exceptions in a single hierarchy for easy handling:

- **`OpenlayerException`** - Base exception for all exceptions

- **`OpenlayerServiceException`** - HTTP errors with a well-formed response body we were able to parse. The exception message and the `.debuggingRequestId()` will be set by the server.
- **`OpenlayerServiceException`** - HTTP errors with a well-formed response body we were able to parse. The exception message and the `.debuggingRequestId()` will be set by the server.

| 400 | BadRequestException |
| ------ | ----------------------------- |
| 401 | AuthenticationException |
| 403 | PermissionDeniedException |
| 404 | NotFoundException |
| 422 | UnprocessableEntityException |
| 429 | RateLimitException |
| 5xx | InternalServerException |
| others | UnexpectedStatusCodeException |
| 400 | BadRequestException |
| ------ | ----------------------------- |
| 401 | AuthenticationException |
| 403 | PermissionDeniedException |
| 404 | NotFoundException |
| 422 | UnprocessableEntityException |
| 429 | RateLimitException |
| 5xx | InternalServerException |
| others | UnexpectedStatusCodeException |

- **`OpenlayerIoException`** - I/O networking errors
- **`OpenlayerInvalidDataException`** - any other exceptions on the client side, e.g.:
- We failed to serialize the request body
- We failed to parse the response body (has access to response code and body)
- **`OpenlayerIoException`** - I/O networking errors
- **`OpenlayerInvalidDataException`** - any other exceptions on the client side, e.g.:
- We failed to serialize the request body
- We failed to parse the response body (has access to response code and body)

## Network options

### Retries

Requests that experience certain errors are automatically retried 2 times by default, with a short exponential backoff. Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, and >=500 Internal errors will all be retried by default.
You can provide a `maxRetries` on the client builder to configure this:
Requests that experience certain errors are automatically retried 2 times by default, with a short exponential backoff. Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, and >=500 Internal errors will all be retried by default. You can provide a `maxRetries` on the client builder to configure this:

```java
import com.openlayer.api.client.OpenlayerClient;
import com.openlayer.api.client.okhttp.OpenlayerOkHttpClient;

OpenlayerClient client = OpenlayerOkHttpClient.builder()
.fromEnv()
.maxRetries(4)
Expand All @@ -204,6 +215,10 @@ OpenlayerClient client = OpenlayerOkHttpClient.builder()
Requests time out after 1 minute by default. You can configure this on the client builder:

```java
import com.openlayer.api.client.OpenlayerClient;
import com.openlayer.api.client.okhttp.OpenlayerOkHttpClient;
import java.time.Duration;

OpenlayerClient client = OpenlayerOkHttpClient.builder()
.fromEnv()
.timeout(Duration.ofSeconds(30))
Expand All @@ -215,24 +230,24 @@ OpenlayerClient client = OpenlayerOkHttpClient.builder()
Requests can be routed through a proxy. You can configure this on the client builder:

```java
import com.openlayer.api.client.OpenlayerClient;
import com.openlayer.api.client.okhttp.OpenlayerOkHttpClient;
import java.net.InetSocketAddress;
import java.net.Proxy;

OpenlayerClient client = OpenlayerOkHttpClient.builder()
.fromEnv()
.proxy(new Proxy(
Type.HTTP,
new InetSocketAddress("proxy.com", 8080)
))
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("example.com", 8080)))
.build();
```

## Making custom/undocumented requests

This library is typed for convenient access to the documented API. If you need to access undocumented
params or response properties, the library can still be used.
This library is typed for convenient access to the documented API. If you need to access undocumented params or response properties, the library can still be used.

### Undocumented request params

To make requests using undocumented parameters, you can provide or override parameters on the params object
while building it.
To make requests using undocumented parameters, you can provide or override parameters on the params object while building it.

```kotlin
FooCreateParams address = FooCreateParams.builder()
Expand All @@ -243,10 +258,7 @@ FooCreateParams address = FooCreateParams.builder()

### Undocumented response properties

To access undocumented response properties, you can use `res._additionalProperties()` on a response object to
get a map of untyped fields of type `Map<String, JsonValue>`. You can then access fields like
`._additionalProperties().get("secret_prop").asString()` or use other helpers defined on the `JsonValue` class
to extract it to a desired type.
To access undocumented response properties, you can use `res._additionalProperties()` on a response object to get a map of untyped fields of type `Map<String, JsonValue>`. You can then access fields like `._additionalProperties().get("secret_prop").asString()` or use other helpers defined on the `JsonValue` class to extract it to a desired type.

## Logging

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class InferencePipelineRowUpdateParamsTest {
fun createInferencePipelineRowUpdateParams() {
InferencePipelineRowUpdateParams.builder()
.inferencePipelineId("182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
.inferenceId("inferenceId")
.row(JsonValue.from(mapOf<String, Any>()))
.config(
InferencePipelineRowUpdateParams.Config.builder()
Expand All @@ -23,7 +24,6 @@ class InferencePipelineRowUpdateParamsTest {
.timestampColumnName("timestamp")
.build()
)
.inferenceId("inferenceId")
.build()
}

Expand All @@ -32,6 +32,7 @@ class InferencePipelineRowUpdateParamsTest {
val params =
InferencePipelineRowUpdateParams.builder()
.inferencePipelineId("182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
.inferenceId("inferenceId")
.row(JsonValue.from(mapOf<String, Any>()))
.config(
InferencePipelineRowUpdateParams.Config.builder()
Expand All @@ -42,7 +43,6 @@ class InferencePipelineRowUpdateParamsTest {
.timestampColumnName("timestamp")
.build()
)
.inferenceId("inferenceId")
.build()
val expected = QueryParams.builder()
expected.put("inferenceId", "inferenceId")
Expand All @@ -54,8 +54,8 @@ class InferencePipelineRowUpdateParamsTest {
val params =
InferencePipelineRowUpdateParams.builder()
.inferencePipelineId("182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
.row(JsonValue.from(mapOf<String, Any>()))
.inferenceId("inferenceId")
.row(JsonValue.from(mapOf<String, Any>()))
.build()
val expected = QueryParams.builder()
expected.put("inferenceId", "inferenceId")
Expand All @@ -67,6 +67,7 @@ class InferencePipelineRowUpdateParamsTest {
val params =
InferencePipelineRowUpdateParams.builder()
.inferencePipelineId("182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
.inferenceId("inferenceId")
.row(JsonValue.from(mapOf<String, Any>()))
.config(
InferencePipelineRowUpdateParams.Config.builder()
Expand All @@ -77,7 +78,6 @@ class InferencePipelineRowUpdateParamsTest {
.timestampColumnName("timestamp")
.build()
)
.inferenceId("inferenceId")
.build()
val body = params.getBody()
assertThat(body).isNotNull
Expand All @@ -99,8 +99,8 @@ class InferencePipelineRowUpdateParamsTest {
val params =
InferencePipelineRowUpdateParams.builder()
.inferencePipelineId("182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
.row(JsonValue.from(mapOf<String, Any>()))
.inferenceId("inferenceId")
.row(JsonValue.from(mapOf<String, Any>()))
.build()
val body = params.getBody()
assertThat(body).isNotNull
Expand All @@ -112,8 +112,8 @@ class InferencePipelineRowUpdateParamsTest {
val params =
InferencePipelineRowUpdateParams.builder()
.inferencePipelineId("182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
.row(JsonValue.from(mapOf<String, Any>()))
.inferenceId("inferenceId")
.row(JsonValue.from(mapOf<String, Any>()))
.build()
assertThat(params).isNotNull
// path param "inferencePipelineId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class RowServiceTest {
rowService.update(
InferencePipelineRowUpdateParams.builder()
.inferencePipelineId("182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
.inferenceId("inferenceId")
.row(JsonValue.from(mapOf<String, Any>()))
.config(
InferencePipelineRowUpdateParams.Config.builder()
Expand All @@ -34,7 +35,6 @@ class RowServiceTest {
.timestampColumnName("timestamp")
.build()
)
.inferenceId("inferenceId")
.build()
)
println(inferencePipelineRowUpdateResponse)
Expand Down

0 comments on commit 1f3da27

Please sign in to comment.