Skip to content

Commit

Permalink
feat(api): manual updates (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-bot committed Aug 8, 2024
1 parent fbe2ed0 commit 324bff1
Show file tree
Hide file tree
Showing 199 changed files with 32,172 additions and 12,142 deletions.
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 87
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/manugoyal%2Fbraintrust-sdk-4e541327f59519ac389f4e7a3b2326cce3412b4e39e0b971f8428b5d4772bc7c.yml
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/manugoyal%2Fbraintrust-sdk-344821ebe82661cb5df7988d5b0541cf89a5a6af89efca9723aff12d369524d2.yml
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,7 @@ implementation("com.braintrustdata.api:braintrust-java:0.6.0")

### Configure the client

Use `BraintrustOkHttpClient.builder()` to configure the client. At a minimum you need to set `.apiKey()`:

```java
import com.braintrustdata.api.client.BraintrustClient;
import com.braintrustdata.api.client.okhttp.BraintrustOkHttpClient;

BraintrustClient client = BraintrustOkHttpClient.builder()
.apiKey("My API Key")
.build();
```
Use `BraintrustOkHttpClient.builder()` to configure the client.

Alternately, set the environment with `BRAINTRUST_API_KEY`, and use `BraintrustOkHttpClient.fromEnv()` to read from the environment.

Expand All @@ -69,7 +60,7 @@ BraintrustClient client = BraintrustOkHttpClient.builder()

| Property | Environment variable | Required | Default value |
| -------- | -------------------- | -------- | ------------- |
| apiKey | `BRAINTRUST_API_KEY` | true ||
| apiKey | `BRAINTRUST_API_KEY` | false ||

Read the documentation for more configuration options.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class BraintrustOkHttpClient private constructor() {
clientOptions.responseValidation(responseValidation)
}

fun apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
fun apiKey(apiKey: String?) = apply { clientOptions.apiKey(apiKey) }

fun fromEnv() = apply { clientOptions.fromEnv() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class BraintrustOkHttpClientAsync private constructor() {
clientOptions.responseValidation(responseValidation)
}

fun apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
fun apiKey(apiKey: String?) = apply { clientOptions.apiKey(apiKey) }

fun fromEnv() = apply { clientOptions.fromEnv() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private constructor(
@get:JvmName("jsonMapper") val jsonMapper: JsonMapper,
@get:JvmName("clock") val clock: Clock,
@get:JvmName("baseUrl") val baseUrl: String,
@get:JvmName("apiKey") val apiKey: String,
@get:JvmName("apiKey") val apiKey: String?,
@get:JvmName("headers") val headers: ListMultimap<String, String>,
@get:JvmName("queryParams") val queryParams: ListMultimap<String, String>,
@get:JvmName("responseValidation") val responseValidation: Boolean,
Expand Down Expand Up @@ -94,13 +94,12 @@ private constructor(

fun maxRetries(maxRetries: Int) = apply { this.maxRetries = maxRetries }

fun apiKey(apiKey: String) = apply { this.apiKey = apiKey }
fun apiKey(apiKey: String?) = apply { this.apiKey = apiKey }

fun fromEnv() = apply { System.getenv("BRAINTRUST_API_KEY")?.let { apiKey(it) } }

fun build(): ClientOptions {
checkNotNull(httpClient) { "`httpClient` is required but was not set" }
checkNotNull(apiKey) { "`apiKey` is required but was not set" }

val headers = ArrayListMultimap.create<String, String>()
val queryParams = ArrayListMultimap.create<String, String>()
Expand All @@ -125,7 +124,7 @@ private constructor(
jsonMapper ?: jsonMapper(),
clock,
baseUrl,
apiKey!!,
apiKey,
headers.toUnmodifiable(),
queryParams.toUnmodifiable(),
responseValidation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,17 @@

package com.braintrustdata.api.models

import com.braintrustdata.api.core.BaseDeserializer
import com.braintrustdata.api.core.BaseSerializer
import com.braintrustdata.api.core.Enum
import com.braintrustdata.api.core.JsonField
import com.braintrustdata.api.core.JsonValue
import com.braintrustdata.api.core.NoAutoDetect
import com.braintrustdata.api.core.getOrThrow
import com.braintrustdata.api.core.toUnmodifiable
import com.braintrustdata.api.errors.BraintrustInvalidDataException
import com.braintrustdata.api.models.*
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.ObjectCodec
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
import java.util.Objects
import java.util.Optional

class AclListParams
constructor(
private val objectId: String,
private val objectType: ObjectType?,
private val objectType: AclObjectType?,
private val endingBefore: String?,
private val ids: Ids?,
private val limit: Long?,
Expand All @@ -38,7 +24,7 @@ constructor(

fun objectId(): String = objectId

fun objectType(): Optional<ObjectType> = Optional.ofNullable(objectType)
fun objectType(): Optional<AclObjectType> = Optional.ofNullable(objectType)

fun endingBefore(): Optional<String> = Optional.ofNullable(endingBefore)

Expand Down Expand Up @@ -114,7 +100,7 @@ constructor(
class Builder {

private var objectId: String? = null
private var objectType: ObjectType? = null
private var objectType: AclObjectType? = null
private var endingBefore: String? = null
private var ids: Ids? = null
private var limit: Long? = null
Expand All @@ -140,7 +126,7 @@ constructor(
fun objectId(objectId: String) = apply { this.objectId = objectId }

/** The object type that the ACL applies to */
fun objectType(objectType: ObjectType) = apply { this.objectType = objectType }
fun objectType(objectType: AclObjectType) = apply { this.objectType = objectType }

/**
* Pagination cursor id.
Expand Down Expand Up @@ -248,228 +234,4 @@ constructor(
additionalBodyProperties.toUnmodifiable(),
)
}

class ObjectType
@JsonCreator
private constructor(
private val value: JsonField<String>,
) : Enum {

@com.fasterxml.jackson.annotation.JsonValue fun _value(): JsonField<String> = value

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}

return other is ObjectType && this.value == other.value
}

override fun hashCode() = value.hashCode()

override fun toString() = value.toString()

companion object {

@JvmField val ORGANIZATION = ObjectType(JsonField.of("organization"))

@JvmField val PROJECT = ObjectType(JsonField.of("project"))

@JvmField val EXPERIMENT = ObjectType(JsonField.of("experiment"))

@JvmField val DATASET = ObjectType(JsonField.of("dataset"))

@JvmField val PROMPT = ObjectType(JsonField.of("prompt"))

@JvmField val PROMPT_SESSION = ObjectType(JsonField.of("prompt_session"))

@JvmField val GROUP = ObjectType(JsonField.of("group"))

@JvmField val ROLE = ObjectType(JsonField.of("role"))

@JvmField val ORG_MEMBER = ObjectType(JsonField.of("org_member"))

@JvmField val PROJECT_LOG = ObjectType(JsonField.of("project_log"))

@JvmField val ORG_PROJECT = ObjectType(JsonField.of("org_project"))

@JvmStatic fun of(value: String) = ObjectType(JsonField.of(value))
}

enum class Known {
ORGANIZATION,
PROJECT,
EXPERIMENT,
DATASET,
PROMPT,
PROMPT_SESSION,
GROUP,
ROLE,
ORG_MEMBER,
PROJECT_LOG,
ORG_PROJECT,
}

enum class Value {
ORGANIZATION,
PROJECT,
EXPERIMENT,
DATASET,
PROMPT,
PROMPT_SESSION,
GROUP,
ROLE,
ORG_MEMBER,
PROJECT_LOG,
ORG_PROJECT,
_UNKNOWN,
}

fun value(): Value =
when (this) {
ORGANIZATION -> Value.ORGANIZATION
PROJECT -> Value.PROJECT
EXPERIMENT -> Value.EXPERIMENT
DATASET -> Value.DATASET
PROMPT -> Value.PROMPT
PROMPT_SESSION -> Value.PROMPT_SESSION
GROUP -> Value.GROUP
ROLE -> Value.ROLE
ORG_MEMBER -> Value.ORG_MEMBER
PROJECT_LOG -> Value.PROJECT_LOG
ORG_PROJECT -> Value.ORG_PROJECT
else -> Value._UNKNOWN
}

fun known(): Known =
when (this) {
ORGANIZATION -> Known.ORGANIZATION
PROJECT -> Known.PROJECT
EXPERIMENT -> Known.EXPERIMENT
DATASET -> Known.DATASET
PROMPT -> Known.PROMPT
PROMPT_SESSION -> Known.PROMPT_SESSION
GROUP -> Known.GROUP
ROLE -> Known.ROLE
ORG_MEMBER -> Known.ORG_MEMBER
PROJECT_LOG -> Known.PROJECT_LOG
ORG_PROJECT -> Known.ORG_PROJECT
else -> throw BraintrustInvalidDataException("Unknown ObjectType: $value")
}

fun asString(): String = _value().asStringOrThrow()
}

@JsonDeserialize(using = Ids.Deserializer::class)
@JsonSerialize(using = Ids.Serializer::class)
class Ids
private constructor(
private val string: String? = null,
private val strings: List<String>? = null,
private val _json: JsonValue? = null,
) {

private var validated: Boolean = false

fun string(): Optional<String> = Optional.ofNullable(string)

fun strings(): Optional<List<String>> = Optional.ofNullable(strings)

fun isString(): Boolean = string != null

fun isStrings(): Boolean = strings != null

fun asString(): String = string.getOrThrow("string")

fun asStrings(): List<String> = strings.getOrThrow("strings")

fun _json(): Optional<JsonValue> = Optional.ofNullable(_json)

fun <T> accept(visitor: Visitor<T>): T {
return when {
string != null -> visitor.visitString(string)
strings != null -> visitor.visitStrings(strings)
else -> visitor.unknown(_json)
}
}

fun validate(): Ids = apply {
if (!validated) {
if (string == null && strings == null) {
throw BraintrustInvalidDataException("Unknown Ids: $_json")
}
validated = true
}
}

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}

return other is Ids && this.string == other.string && this.strings == other.strings
}

override fun hashCode(): Int {
return Objects.hash(string, strings)
}

override fun toString(): String {
return when {
string != null -> "Ids{string=$string}"
strings != null -> "Ids{strings=$strings}"
_json != null -> "Ids{_unknown=$_json}"
else -> throw IllegalStateException("Invalid Ids")
}
}

companion object {

@JvmStatic fun ofString(string: String) = Ids(string = string)

@JvmStatic fun ofStrings(strings: List<String>) = Ids(strings = strings)
}

interface Visitor<out T> {

fun visitString(string: String): T

fun visitStrings(strings: List<String>): T

fun unknown(json: JsonValue?): T {
throw BraintrustInvalidDataException("Unknown Ids: $json")
}
}

class Deserializer : BaseDeserializer<Ids>(Ids::class) {

override fun ObjectCodec.deserialize(node: JsonNode): Ids {
val json = JsonValue.fromJsonNode(node)
tryDeserialize(node, jacksonTypeRef<String>())?.let {
return Ids(string = it, _json = json)
}
tryDeserialize(node, jacksonTypeRef<List<String>>())?.let {
return Ids(strings = it, _json = json)
}

return Ids(_json = json)
}
}

class Serializer : BaseSerializer<Ids>(Ids::class) {

override fun serialize(
value: Ids,
generator: JsonGenerator,
provider: SerializerProvider
) {
when {
value.string != null -> generator.writeObject(value.string)
value.strings != null -> generator.writeObject(value.strings)
value._json != null -> generator.writeObject(value._json)
else -> throw IllegalStateException("Invalid Ids")
}
}
}
}
}
Loading

0 comments on commit 324bff1

Please sign in to comment.