Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

bflad/petstore-java

Repository files navigation

openapi

Developer-friendly & type-safe Java SDK specifically catered to leverage openapi API.



Important

This SDK is not yet ready for production use. To complete setup please follow the steps outlined in your workspace. Delete this section before > publishing to a package manager.

Summary

Petstore - OpenAPI 3.1: This is a sample Pet Store Server based on the OpenAPI 3.1 specification.

Some useful links:

For more information about the API: Find out more about Swagger

Table of Contents

SDK Installation

Getting started

JDK 11 or later is required.

The samples below show how a published SDK artifact is used:

Gradle:

implementation 'org.openapis:openapi:0.0.1'

Maven:

<dependency>
    <groupId>org.openapis</groupId>
    <artifactId>openapi</artifactId>
    <version>0.0.1</version>
</dependency>

How to build

After cloning the git repository to your file system you can build the SDK artifact from source to the build directory by running ./gradlew build on *nix systems or gradlew.bat on Windows systems.

If you wish to build from source and publish the SDK artifact to your local Maven repository (on your filesystem) then use the following command (after cloning the git repo locally):

On *nix:

./gradlew publishToMavenLocal -Pskip.signing

On Windows:

gradlew.bat publishToMavenLocal -Pskip.signing

SDK Example Usage

Example

package hello.world;

import java.lang.Exception;
import java.util.List;
import org.openapis.openapi.Petstore;
import org.openapis.openapi.models.components.Category;
import org.openapis.openapi.models.components.Pet;
import org.openapis.openapi.models.errors.*;
import org.openapis.openapi.models.operations.UpdatePetResponse;

public class Application {

    public static void main(String[] args) throws ApiErrorInvalidInput, ApiErrorUnauthorized, ApiErrorNotFound, Exception {

        Petstore sdk = Petstore.builder()
                .apiKey(System.getenv().getOrDefault("API_KEY", ""))
            .build();

        Pet req = Pet.builder()
                .name("doggie")
                .photoUrls(List.of(
                    "<value 1>"))
                .id(10L)
                .category(Category.builder()
                    .id(1L)
                    .name("Dogs")
                    .build())
                .build();

        UpdatePetResponse res = sdk.pet().updatePet()
                .request(req)
                .call();

        if (res.pet().isPresent()) {
            // handle response
        }
    }
}

Authentication

Per-Client Security Schemes

This SDK supports the following security scheme globally:

Name Type Scheme
apiKey apiKey API key

To authenticate with the API the apiKey parameter must be set when initializing the SDK client instance. For example:

package hello.world;

import java.lang.Exception;
import java.util.List;
import org.openapis.openapi.Petstore;
import org.openapis.openapi.models.components.Category;
import org.openapis.openapi.models.components.Pet;
import org.openapis.openapi.models.errors.*;
import org.openapis.openapi.models.operations.UpdatePetResponse;

public class Application {

    public static void main(String[] args) throws ApiErrorInvalidInput, ApiErrorUnauthorized, ApiErrorNotFound, Exception {

        Petstore sdk = Petstore.builder()
                .apiKey(System.getenv().getOrDefault("API_KEY", ""))
            .build();

        Pet req = Pet.builder()
                .name("doggie")
                .photoUrls(List.of(
                    "<value 1>"))
                .id(10L)
                .category(Category.builder()
                    .id(1L)
                    .name("Dogs")
                    .build())
                .build();

        UpdatePetResponse res = sdk.pet().updatePet()
                .request(req)
                .call();

        if (res.pet().isPresent()) {
            // handle response
        }
    }
}

Available Resources and Operations

Available methods

Error Handling

Handling errors in this SDK should largely match your expectations. All operations return a response object or raise an exception.

By default, an API error will throw a models/errors/APIException exception. When custom error responses are specified for an operation, the SDK may also throw their associated exception. You can refer to respective Errors tables in SDK docs for more details on possible exception types for each operation. For example, the updatePet method throws the following exceptions:

Error Type Status Code Content Type
models/errors/ApiErrorInvalidInput 400 application/json
models/errors/ApiErrorUnauthorized 401 application/json
models/errors/ApiErrorNotFound 404 application/json
models/errors/APIException 4XX, 5XX */*

Example

package hello.world;

import java.lang.Exception;
import java.util.List;
import org.openapis.openapi.Petstore;
import org.openapis.openapi.models.components.Category;
import org.openapis.openapi.models.components.Pet;
import org.openapis.openapi.models.errors.*;
import org.openapis.openapi.models.operations.UpdatePetResponse;

public class Application {

    public static void main(String[] args) throws ApiErrorInvalidInput, ApiErrorUnauthorized, ApiErrorNotFound, Exception {

        Petstore sdk = Petstore.builder()
                .apiKey(System.getenv().getOrDefault("API_KEY", ""))
            .build();

        Pet req = Pet.builder()
                .name("doggie")
                .photoUrls(List.of(
                    "<value 1>"))
                .id(10L)
                .category(Category.builder()
                    .id(1L)
                    .name("Dogs")
                    .build())
                .build();

        UpdatePetResponse res = sdk.pet().updatePet()
                .request(req)
                .call();

        if (res.pet().isPresent()) {
            // handle response
        }
    }
}

Server Selection

Server Variables

The default server https://{environment}.petstore.io contains variables and is set to https://prod.petstore.io by default. To override default values, the following builder methods are available when initializing the SDK client instance:

Variable BuilderMethod Supported Values Default Description
environment environment(ServerEnvironment environment) - "prod"
- "staging"
- "dev"
"prod" The environment name. Defaults to the production environment.

Example

package hello.world;

import java.lang.Exception;
import java.util.List;
import org.openapis.openapi.Petstore;
import org.openapis.openapi.SDK.Builder.ServerEnvironment;
import org.openapis.openapi.models.components.Category;
import org.openapis.openapi.models.components.Pet;
import org.openapis.openapi.models.errors.*;
import org.openapis.openapi.models.operations.UpdatePetResponse;

public class Application {

    public static void main(String[] args) throws ApiErrorInvalidInput, ApiErrorUnauthorized, ApiErrorNotFound, Exception {

        Petstore sdk = Petstore.builder()
                .environment(ServerEnvironment.DEV)
                .apiKey(System.getenv().getOrDefault("API_KEY", ""))
            .build();

        Pet req = Pet.builder()
                .name("doggie")
                .photoUrls(List.of(
                    "<value 1>"))
                .id(10L)
                .category(Category.builder()
                    .id(1L)
                    .name("Dogs")
                    .build())
                .build();

        UpdatePetResponse res = sdk.pet().updatePet()
                .request(req)
                .call();

        if (res.pet().isPresent()) {
            // handle response
        }
    }
}

Override Server URL Per-Client

The default server can be overridden globally using the .serverURL(String serverUrl) builder method when initializing the SDK client instance. For example:

package hello.world;

import java.lang.Exception;
import java.util.List;
import org.openapis.openapi.Petstore;
import org.openapis.openapi.models.components.Category;
import org.openapis.openapi.models.components.Pet;
import org.openapis.openapi.models.errors.*;
import org.openapis.openapi.models.operations.UpdatePetResponse;

public class Application {

    public static void main(String[] args) throws ApiErrorInvalidInput, ApiErrorUnauthorized, ApiErrorNotFound, Exception {

        Petstore sdk = Petstore.builder()
                .serverURL("https://prod.petstore.io")
                .apiKey(System.getenv().getOrDefault("API_KEY", ""))
            .build();

        Pet req = Pet.builder()
                .name("doggie")
                .photoUrls(List.of(
                    "<value 1>"))
                .id(10L)
                .category(Category.builder()
                    .id(1L)
                    .name("Dogs")
                    .build())
                .build();

        UpdatePetResponse res = sdk.pet().updatePet()
                .request(req)
                .call();

        if (res.pet().isPresent()) {
            // handle response
        }
    }
}

Debugging

Debug

You can setup your SDK to emit debug logs for SDK requests and responses.

For request and response logging (especially json bodies), call enableHTTPDebugLogging(boolean) on the SDK builder like so:

SDK.builder()
    .enableHTTPDebugLogging(true)
    .build();

Example output:

Sending request: http://localhost:35123/bearer#global GET
Request headers: {Accept=[application/json], Authorization=[******], Client-Level-Header=[added by client], Idempotency-Key=[some-key], x-speakeasy-user-agent=[speakeasy-sdk/java 0.0.1 internal 0.1.0 org.openapis.openapi]}
Received response: (GET http://localhost:35123/bearer#global) 200
Response headers: {access-control-allow-credentials=[true], access-control-allow-origin=[*], connection=[keep-alive], content-length=[50], content-type=[application/json], date=[Wed, 09 Apr 2025 01:43:29 GMT], server=[gunicorn/19.9.0]}
Response body:
{
  "authenticated": true, 
  "token": "global"
}

WARNING: This should only used for temporary debugging purposes. Leaving this option on in a production system could expose credentials/secrets in logs. Authorization headers are redacted by default and there is the ability to specify redacted header names via SpeakeasyHTTPClient.setRedactedHeaders.

NOTE: This is a convenience method that calls HTTPClient.enableDebugLogging(). The SpeakeasyHTTPClient honors this setting. If you are using a custom HTTP client, it is up to the custom client to honor this setting.

Another option is to set the System property -Djdk.httpclient.HttpClient.log=all. However, this second option does not log bodies.

Development

Maturity

This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributions

While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.

SDK Created by Speakeasy

About

No description, website, or topics provided.

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages