> ## Documentation Index
> Fetch the complete documentation index at: https://docs.yasmina.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Java

# Yasminaai Java Library

[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github\&utm_medium=github\&utm_campaign=readme\&utm_source=https%3A%2F%2Fgithub.com%2FYasminaAI%2Fmotor-java-sdk)
[![Maven Central](https://img.shields.io/maven-central/v/io.github.yasminaai/motor-java)](https://central.sonatype.com/artifact/io.github.yasminaai/motor-java)

The Yasminaai Java library provides convenient access to the Yasminaai APIs from Java.

## Table of Contents

* [Installation](#installation)
* [Reference](#reference)
* [Usage](#usage)
* [Environments](#environments)
* [Base Url](#base-url)
* [Exception Handling](#exception-handling)
* [Advanced](#advanced)
  * [Custom Client](#custom-client)
  * [Retries](#retries)
  * [Timeouts](#timeouts)
  * [Custom Headers](#custom-headers)
  * [Access Raw Response Data](#access-raw-response-data)
* [Contributing](#contributing)

## Installation

### Gradle

Add the dependency in your `build.gradle` file:

```groovy theme={null}
dependencies {
  implementation 'io.github.yasminaai:motor-java:0.1.0'
}
```

### Maven

Add the dependency in your `pom.xml` file:

```xml theme={null}
<dependency>
  <groupId>io.github.yasminaai</groupId>
  <artifactId>motor-java</artifactId>
  <version>0.1.0</version>
</dependency>
```

## Reference

A full reference for this library is available [here](https://github.com/YasminaAI/motor-java-sdk/blob/HEAD/./reference.md).

## Usage

Instantiate and use the client with the following:

```java theme={null}
package com.example.usage;

import com.yasminaai.api.YasminaaiApiClient;
import com.yasminaai.api.resources.quotes.requests.PostQuoteRequestsRequest;

public class Example {
    public static void main(String[] args) {
        YasminaaiApiClient client = YasminaaiApiClient
            .builder()
            .token("<token>")
            .build();

        client.quotes().requestQuotes(
            PostQuoteRequestsRequest
                .builder()
                .otp("123456")
                .ownerId("owner_id")
                .phone("phone")
                .birthdate("2023-01-15")
                .carEstimatedCost(1.1)
                .build()
        );
    }
}
```

## Environments

This SDK allows you to configure different environments for API requests.

```java theme={null}
import com.yasminaai.api.YasminaaiApiClient;
import com.yasminaai.api.core.Environment;

YasminaaiApiClient client = YasminaaiApiClient
    .builder()
    .environment(Environment.Sandbox)
    .build();
```

## Base Url

You can set a custom base URL when constructing the client.

```java theme={null}
import com.yasminaai.api.YasminaaiApiClient;

YasminaaiApiClient client = YasminaaiApiClient
    .builder()
    .url("https://example.com")
    .build();
```

## Exception Handling

When the API returns a non-success status code (4xx or 5xx response), an API exception will be thrown.

```java theme={null}
import com.yasminaai.api.core.YasminaaiApiApiException;

try{
    client.quotes().requestQuotes(...);
} catch (YasminaaiApiApiException e){
    // Do something with the API exception...
}
```

## Advanced

### Custom Client

This SDK is built to work with any instance of `OkHttpClient`. By default, if no client is provided, the SDK will construct one.
However, you can pass your own client like so:

```java theme={null}
import com.yasminaai.api.YasminaaiApiClient;
import okhttp3.OkHttpClient;

OkHttpClient customClient = ...;

YasminaaiApiClient client = YasminaaiApiClient
    .builder()
    .httpClient(customClient)
    .build();
```

### Retries

The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long
as the request is deemed retryable and the number of retry attempts has not grown larger than the configured
retry limit (default: 2). Before defaulting to exponential backoff, the SDK will first attempt to respect
the `Retry-After` header (as either in seconds or as an HTTP date), and then the `X-RateLimit-Reset` header
(as a Unix timestamp in epoch seconds); failing both of those, it will fall back to exponential backoff.

Which status codes are retried depends on the `retry-status-codes` generator configuration:

**`legacy`** (current default): retries on

* [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout)
* [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests)
* [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#server_error_responses) (All server errors, including 500)

**`recommended`**: retries on

* [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout)
* [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests)
* [502](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/502) (Bad Gateway)
* [503](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/503) (Service Unavailable)
* [504](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/504) (Gateway Timeout)

Use the `maxRetries` client option to configure this behavior.

```java theme={null}
import com.yasminaai.api.YasminaaiApiClient;

YasminaaiApiClient client = YasminaaiApiClient
    .builder()
    .maxRetries(1)
    .build();
```

### Timeouts

The SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level.

```java theme={null}
import com.yasminaai.api.YasminaaiApiClient;
import com.yasminaai.api.core.RequestOptions;

// Client level
YasminaaiApiClient client = YasminaaiApiClient
    .builder()
    .timeout(60)
    .build();

// Request level
client.quotes().requestQuotes(
    ...,
    RequestOptions
        .builder()
        .timeout(60)
        .build()
);
```

### Custom Headers

The SDK allows you to add custom headers to requests. You can configure headers at the client level or at the request level.

```java theme={null}
import com.yasminaai.api.YasminaaiApiClient;
import com.yasminaai.api.core.RequestOptions;

// Client level
YasminaaiApiClient client = YasminaaiApiClient
    .builder()
    .addHeader("X-Custom-Header", "custom-value")
    .addHeader("X-Request-Id", "abc-123")
    .build();
;

// Request level
client.quotes().requestQuotes(
    ...,
    RequestOptions
        .builder()
        .addHeader("X-Request-Header", "request-value")
        .build()
);
```

### Access Raw Response Data

The SDK provides access to raw response data, including headers, through the `withRawResponse()` method.
The `withRawResponse()` method returns a raw client that wraps all responses with `body()` and `headers()` methods.
(A normal client's `response` is identical to a raw client's `response.body()`.)

```java theme={null}
YasminaaiApiHttpResponse response = client.quotes().withRawResponse().requestQuotes(...);

System.out.println(response.body());
System.out.println(response.headers().get("X-My-Header"));
```

## Contributing

While we value open-source contributions to this SDK, this library is generated programmatically.
Additions made directly to this library would have to be moved over to our generation code,
otherwise they would be overwritten upon the next generated release. Feel free to open a PR as
a proof of concept, but know that we will not be able to merge it as-is. We suggest opening
an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!
