// GENERATED by tools/gen — DO NOT EDIT.

///|
fn[T : ToJson] parameter_value(value : T) -> String {
  match Json(value) {
    String(value) => value
    True => "true"
    False => "false"
    Number(value, repr~) =>
      match repr {
        Some(text) => text
        None => value.to_string()
      }
    other => other.stringify()
  }
}

///|
fn percent_encode(value : String) -> String {
  let hex = b"0123456789ABCDEF"
  let output : Array[Byte] = []
  for byte in @utf8.encode(value) {
    let code = byte.to_int()
    if (code >= 65 && code <= 90) ||
      (code >= 97 && code <= 122) ||
      (code >= 48 && code <= 57) ||
      byte == b'-' ||
      byte == b'.' ||
      byte == b'_' ||
      byte == b'~' {
      output.push(byte)
    } else {
      output.push(b'%')
      output.push(hex[code >> 4])
      output.push(hex[code & 15])
    }
  }
  try! @utf8.decode(Bytes::from_array(output))
}

///|
fn append_query(url : String, name : String, value : String) -> String {
  let separator = if url.contains("?") { "&" } else { "?" }
  url + separator + percent_encode(name) + "=" + percent_encode(value)
}

///|
fn parse_json_body(body : Bytes) -> Json raise {
  @json.parse(@utf8.decode(body))
}

///|
fn[T : @json.FromJson] decode_response(
  response : @http.Response,
) -> T raise @runtime.SdkError {
  @json.from_json(parse_json_body(response.body)) catch {
    error =>
      raise @runtime.Decode(message=error.to_string(), body=response.body)
  }
}

///|
///
/// Create a Message
pub fn messages_post_request(
  body : CreateMessageParams,
  anthropic_version? : String,
  anthropic_user_profile_id? : String,
  anthropic_workspace_id? : String,
) -> @http.Request {
  let mut request = @http.Request::post("/v1/messages").json_body(Json(body))
  match anthropic_version {
    Some(value) =>
      request = request.header("anthropic-version", parameter_value(value))
    None => ()
  }
  match anthropic_user_profile_id {
    Some(value) =>
      request = request.header(
        "anthropic-user-profile-id",
        parameter_value(value),
      )
    None => ()
  }
  match anthropic_workspace_id {
    Some(value) =>
      request = request.header("anthropic-workspace-id", parameter_value(value))
    None => ()
  }
  request
}

///|
///
/// Decodes a parsed successful messages_post response.
pub fn messages_post_decode_json(
  value : Json,
) -> Message raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful messages_post response.
pub fn messages_post_decode(
  response : @http.Response,
) -> Message raise @runtime.SdkError {
  decode_response(response)
}

///|
///
/// List Models
pub fn models_list_request(
  before_id? : String,
  after_id? : String,
  limit? : Int,
  anthropic_version? : String,
  anthropic_beta? : Array[String],
  x_api_key? : String,
  anthropic_workspace_id? : String,
) -> @http.Request {
  let mut url = "/v1/models"
  match before_id {
    Some(value) => url = append_query(url, "before_id", parameter_value(value))
    None => ()
  }
  match after_id {
    Some(value) => url = append_query(url, "after_id", parameter_value(value))
    None => ()
  }
  match limit {
    Some(value) => url = append_query(url, "limit", parameter_value(value))
    None => ()
  }
  let mut request = @http.Request::get(url)
  match anthropic_version {
    Some(value) =>
      request = request.header("anthropic-version", parameter_value(value))
    None => ()
  }
  match anthropic_beta {
    Some(values) =>
      request = request.header(
        "anthropic-beta",
        values.map(value => parameter_value(value)).join(","),
      )
    None => ()
  }
  match x_api_key {
    Some(value) => request = request.header("x-api-key", parameter_value(value))
    None => ()
  }
  match anthropic_workspace_id {
    Some(value) =>
      request = request.header("anthropic-workspace-id", parameter_value(value))
    None => ()
  }
  request
}

///|
///
/// Decodes a parsed successful models_list response.
pub fn models_list_decode_json(
  value : Json,
) -> ListResponseModelInfo raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful models_list response.
pub fn models_list_decode(
  response : @http.Response,
) -> ListResponseModelInfo raise @runtime.SdkError {
  decode_response(response)
}

///|
///
/// Get a Model
pub fn models_get_request(
  model_id : String,
  anthropic_version? : String,
  anthropic_beta? : Array[String],
  x_api_key? : String,
  anthropic_workspace_id? : String,
) -> @http.Request {
  let mut request = @http.Request::get(
    "/v1/models/" + percent_encode(parameter_value(model_id)),
  )
  match anthropic_version {
    Some(value) =>
      request = request.header("anthropic-version", parameter_value(value))
    None => ()
  }
  match anthropic_beta {
    Some(values) =>
      request = request.header(
        "anthropic-beta",
        values.map(value => parameter_value(value)).join(","),
      )
    None => ()
  }
  match x_api_key {
    Some(value) => request = request.header("x-api-key", parameter_value(value))
    None => ()
  }
  match anthropic_workspace_id {
    Some(value) =>
      request = request.header("anthropic-workspace-id", parameter_value(value))
    None => ()
  }
  request
}

///|
///
/// Decodes a parsed successful models_get response.
pub fn models_get_decode_json(
  value : Json,
) -> ModelInfo raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful models_get response.
pub fn models_get_decode(
  response : @http.Response,
) -> ModelInfo raise @runtime.SdkError {
  decode_response(response)
}

///|
///
/// Count tokens in a Message
pub fn messages_count_tokens_post_request(
  body : CountMessageTokensParams,
  anthropic_version? : String,
  anthropic_user_profile_id? : String,
  anthropic_workspace_id? : String,
) -> @http.Request {
  let mut request = @http.Request::post("/v1/messages/count_tokens").json_body(
    Json(body),
  )
  match anthropic_version {
    Some(value) =>
      request = request.header("anthropic-version", parameter_value(value))
    None => ()
  }
  match anthropic_user_profile_id {
    Some(value) =>
      request = request.header(
        "anthropic-user-profile-id",
        parameter_value(value),
      )
    None => ()
  }
  match anthropic_workspace_id {
    Some(value) =>
      request = request.header("anthropic-workspace-id", parameter_value(value))
    None => ()
  }
  request
}

///|
///
/// Decodes a parsed successful messages_count_tokens_post response.
pub fn messages_count_tokens_post_decode_json(
  value : Json,
) -> CountMessageTokensResponse raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful messages_count_tokens_post response.
pub fn messages_count_tokens_post_decode(
  response : @http.Response,
) -> CountMessageTokensResponse raise @runtime.SdkError {
  decode_response(response)
}