Skip to content

A go SDK for accessing the The-Speakeasy-Bar API.

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

speakeasy-sdks/test-ryan-3

Repository files navigation

Template SDK

How to use this repository

đź‘€ This template repository is designed to bootstrap a Speakeasy managed SDK repository using Github's repository clone feature. Once this repository is setup it will automatically keep your SDK up to date and published to a package manager.

Creating an SDK

  1. To get started, simply clone the repository by clicking on the "Use template" button and give it a name.

Screenshot 2023-09-06 at 09 20 52

  1. Configure the Speakeasy workflow to generate the SDK. Go to the generation workflow file and configure the language, mode and location of your openapi document. For complete documentation on all the available generation configurations, see here. You will also need to add a SPEAKEASY_API_KEY as a repository secret. If you don't already have a key you can get one by making a workspace on Speakeasy here.

  2. Configure the Speakeasy workflow to publish the SDK. Go to the publishing workflow file and configure any relevant package manager credentials as repository secrets. For complete documentation on all the available publishing configurations, see here.

  3. Configure the generation by editing the gen.yaml file in the root of the repo. This file controls the generator and determines various attributes of the SDK like packageName, sdkClassName, inlining of parameters, and other ergonomics.

  4. Finally go to the Actions tab, choose the generation workflow and click "Force Generate". This will trigger a new generation of your SDK using the configuration you provided above. Depending on whether you configured pr or direct mode above your updated SDK will appear in PR or in the main branch.

Screenshot 2023-09-06 at 10 01 46

🚀 You should have a working SDK for your API 🙂 . To check out all the features of the SDK please see our docs site.

Local development

Once you have the SDK setup you may want to iterate on the SDK. Speakeasy supports OpenAPI vendor extensions that can be added to your spec to customize the SDK ergonomics (method names, namespacing resources etc.) and functionality (adding retries, pagination, multiple server support etc)

To get started install the Speakeasy CLI.

In your terminal, run:

brew install speakeasy-api/homebrew-tap/speakeasy

Once you annonate your spec with an extension you will want to run speakeasy validate to check the spec for correctness and speakeasy generate to recreate the SDK locally. More documentation on OpenAPI extensions here. Here's an example of adding a multiple server support to the spec so that your SDK supports production and sandbox versions of your API.

info:
  title: Example
  version: 0.0.1
servers:
  - url: https://prod.example.com # Used as the default URL by the SDK
    description: Our production environment
    x-speakeasy-server-id: prod
  - url: https://sandbox.example.com
    description: Our sandbox environment
    x-speakeasy-server-id: sandbox

Once you're finished iterating and happy with the output push only the latest version of spec into the repo and regenerate the SDK using step 6 above.

SDK Installation

go get github.com/speakeasy-sdks/test-ryan-3

SDK Example Usage

Sign in

First you need to send an authentication request to the API by providing your username and password. In the request body, you should specify the type of token you would like to receive: API key or JSON Web Token. If your credentials are valid, you will receive a token in the response object: res.object.token: str.

package main

import (
	"context"
	testryan3 "github.com/speakeasy-sdks/test-ryan-3"
	"github.com/speakeasy-sdks/test-ryan-3/models/operations"
	"log"
)

func main() {
	s := testryan3.New()

	operationSecurity := operations.LoginSecurity{
		Password: "<PASSWORD>",
		Username: "<USERNAME>",
	}

	ctx := context.Background()
	res, err := s.Authentication.Login(ctx, operations.LoginRequestBody{
		Type: operations.TypeAPIKey,
	}, operationSecurity)
	if err != nil {
		log.Fatal(err)
	}
	if res.Object != nil {
		// handle response
	}
}

Browse available drinks

Once you are authenticated, you can use the token you received for all other authenticated endpoints. For example, you can filter the list of available drinks by type.

package main

import (
	"context"
	testryan3 "github.com/speakeasy-sdks/test-ryan-3"
	"github.com/speakeasy-sdks/test-ryan-3/models/components"
	"github.com/speakeasy-sdks/test-ryan-3/models/operations"
	"log"
)

func main() {
	s := testryan3.New(
		testryan3.WithSecurity(components.Security{
			APIKey: testryan3.String("<YOUR_API_KEY>"),
		}),
	)

	ctx := context.Background()
	res, err := s.Drinks.ListDrinks(ctx, operations.ListDrinksRequest{})
	if err != nil {
		log.Fatal(err)
	}
	if res.Classes != nil {
		// handle response
	}
}

Create an order

When you submit an order, you can include a callback URL along your request. This URL will get called whenever the supplier updates the status of your order.

package main

import (
	"context"
	testryan3 "github.com/speakeasy-sdks/test-ryan-3"
	"github.com/speakeasy-sdks/test-ryan-3/models/components"
	"github.com/speakeasy-sdks/test-ryan-3/models/operations"
	"log"
)

func main() {
	s := testryan3.New(
		testryan3.WithSecurity(components.Security{
			APIKey: testryan3.String("<YOUR_API_KEY>"),
		}),
	)

	ctx := context.Background()
	res, err := s.Orders.CreateOrder(ctx, operations.CreateOrderRequest{
		RequestBody: []components.OrderInput{
			components.OrderInput{
				ProductCode: "APM-1F2D3",
				Quantity:    26535,
				Type:        components.OrderTypeDrink,
			},
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	if res.Order != nil {
		// handle response
	}
}

Subscribe to webhooks to receive stock updates

package main

import (
	"context"
	testryan3 "github.com/speakeasy-sdks/test-ryan-3"
	"github.com/speakeasy-sdks/test-ryan-3/models/components"
	"github.com/speakeasy-sdks/test-ryan-3/models/operations"
	"log"
)

func main() {
	s := testryan3.New(
		testryan3.WithSecurity(components.Security{
			APIKey: testryan3.String("<YOUR_API_KEY>"),
		}),
	)

	ctx := context.Background()
	res, err := s.Config.SubscribeToWebhooks(ctx, []operations.RequestBody{
		operations.RequestBody{},
	})
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response
	}
}

Available Resources and Operations

  • Login - Authenticate with the API by providing a username and password.

Error Handling

Handling errors in this SDK should largely match your expectations. All operations return a response object or an error, they will never return both. When specified by the OpenAPI spec document, the SDK will return the appropriate subclass.

Error Object Status Code Content Type
sdkerrors.BadRequest 400 application/json
sdkerrors.APIError 5XX application/json
sdkerrors.SDKError 4xx-5xx /

Example

package main

import (
	"context"
	"errors"
	testryan3 "github.com/speakeasy-sdks/test-ryan-3"
	"github.com/speakeasy-sdks/test-ryan-3/models/components"
	"github.com/speakeasy-sdks/test-ryan-3/models/operations"
	"github.com/speakeasy-sdks/test-ryan-3/models/sdkerrors"
	"log"
)

func main() {
	s := testryan3.New(
		testryan3.WithSecurity(components.Security{
			APIKey: testryan3.String("<YOUR_API_KEY>"),
		}),
	)

	ctx := context.Background()
	res, err := s.Config.SubscribeToWebhooks(ctx, []operations.RequestBody{
		operations.RequestBody{},
	})
	if err != nil {

		var e *sdkerrors.BadRequest
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}

		var e *sdkerrors.APIError
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}

		var e *sdkerrors.SDKError
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}
	}
}

Server Selection

Select Server by Name

You can override the default server globally using the WithServer option when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the names associated with the available servers:

Name Server Variables
prod https://speakeasy.bar None
staging https://staging.speakeasy.bar None
customer https://{organization}.{environment}.speakeasy.bar environment (default is prod), organization (default is api)

Example

package main

import (
	"context"
	testryan3 "github.com/speakeasy-sdks/test-ryan-3"
	"github.com/speakeasy-sdks/test-ryan-3/models/components"
	"github.com/speakeasy-sdks/test-ryan-3/models/operations"
	"log"
)

func main() {
	s := testryan3.New(
		testryan3.WithServer("customer"),
		testryan3.WithSecurity(components.Security{
			APIKey: testryan3.String("<YOUR_API_KEY>"),
		}),
	)

	ctx := context.Background()
	res, err := s.Ingredients.ListIngredients(ctx, operations.ListIngredientsRequest{})
	if err != nil {
		log.Fatal(err)
	}
	if res.Classes != nil {
		// handle response
	}
}

Variables

Some of the server options above contain variables. If you want to set the values of those variables, the following options are provided for doing so:

  • WithEnvironment testryan3.ServerEnvironment
  • WithOrganization string

Override Server URL Per-Client

The default server can also be overridden globally using the WithServerURL option when initializing the SDK client instance. For example:

package main

import (
	"context"
	testryan3 "github.com/speakeasy-sdks/test-ryan-3"
	"github.com/speakeasy-sdks/test-ryan-3/models/components"
	"github.com/speakeasy-sdks/test-ryan-3/models/operations"
	"log"
)

func main() {
	s := testryan3.New(
		testryan3.WithServerURL("https://speakeasy.bar"),
		testryan3.WithSecurity(components.Security{
			APIKey: testryan3.String("<YOUR_API_KEY>"),
		}),
	)

	ctx := context.Background()
	res, err := s.Ingredients.ListIngredients(ctx, operations.ListIngredientsRequest{})
	if err != nil {
		log.Fatal(err)
	}
	if res.Classes != nil {
		// handle response
	}
}

Override Server URL Per-Operation

The server URL can also be overridden on a per-operation basis, provided a server list was specified for the operation. For example:

package main

import (
	"context"
	testryan3 "github.com/speakeasy-sdks/test-ryan-3"
	"github.com/speakeasy-sdks/test-ryan-3/models/components"
	"github.com/speakeasy-sdks/test-ryan-3/models/operations"
	"log"
)

func main() {
	s := testryan3.New(
		testryan3.WithSecurity(components.Security{
			APIKey: testryan3.String("<YOUR_API_KEY>"),
		}),
	)

	ctx := context.Background()
	res, err := s.Drinks.ListDrinks(ctx, operations.ListDrinksRequest{}, operations.WithServerURL("https://speakeasy.bar"))
	if err != nil {
		log.Fatal(err)
	}
	if res.Classes != nil {
		// handle response
	}
}

Custom HTTP Client

The Go SDK makes API calls that wrap an internal HTTP client. The requirements for the HTTP client are very simple. It must match this interface:

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

The built-in net/http client satisfies this interface and a default client based on the built-in is provided by default. To replace this default with a client of your own, you can implement this interface yourself or provide your own client configured as desired. Here's a simple example, which adds a client with a 30 second timeout.

import (
	"net/http"
	"time"
	"github.com/myorg/your-go-sdk"
)

var (
	httpClient = &http.Client{Timeout: 30 * time.Second}
	sdkClient  = sdk.New(sdk.WithClient(httpClient))
)

This can be a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration.

Authentication

Per-Client Security Schemes

This SDK supports the following security schemes globally:

Name Type Scheme
APIKey apiKey API key
BearerAuth http HTTP Bearer

You can set the security parameters through the WithSecurity option when initializing the SDK client instance. The selected scheme will be used by default to authenticate with the API for all operations that support it. For example:

package main

import (
	"context"
	testryan3 "github.com/speakeasy-sdks/test-ryan-3"
	"github.com/speakeasy-sdks/test-ryan-3/models/components"
	"github.com/speakeasy-sdks/test-ryan-3/models/operations"
	"log"
)

func main() {
	s := testryan3.New(
		testryan3.WithSecurity(components.Security{
			APIKey: testryan3.String("<YOUR_API_KEY>"),
		}),
	)

	ctx := context.Background()
	res, err := s.Ingredients.ListIngredients(ctx, operations.ListIngredientsRequest{})
	if err != nil {
		log.Fatal(err)
	}
	if res.Classes != nil {
		// handle response
	}
}

Per-Operation Security Schemes

Some operations in this SDK require the security scheme to be specified at the request level. For example:

package main

import (
	"context"
	testryan3 "github.com/speakeasy-sdks/test-ryan-3"
	"github.com/speakeasy-sdks/test-ryan-3/models/operations"
	"log"
)

func main() {
	s := testryan3.New()

	operationSecurity := operations.LoginSecurity{
		Password: "<PASSWORD>",
		Username: "<USERNAME>",
	}

	ctx := context.Background()
	res, err := s.Authentication.Login(ctx, operations.LoginRequestBody{
		Type: operations.TypeAPIKey,
	}, operationSecurity)
	if err != nil {
		log.Fatal(err)
	}
	if res.Object != nil {
		// handle response
	}
}

Special Types

Retries

Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.

To change the default retry strategy for a single API call, simply provide a RetryConfig object to the call by using the WithRetries option:

package main

import (
	"context"
	testryan3 "github.com/speakeasy-sdks/test-ryan-3"
	"github.com/speakeasy-sdks/test-ryan-3/internal/utils"
	"github.com/speakeasy-sdks/test-ryan-3/models/components"
	"github.com/speakeasy-sdks/test-ryan-3/models/operations"
	"log"
	"models/operations"
)

func main() {
	s := testryan3.New(
		testryan3.WithSecurity(components.Security{
			APIKey: testryan3.String("<YOUR_API_KEY>"),
		}),
	)

	ctx := context.Background()
	res, err := s.Config.SubscribeToWebhooks(ctx, []operations.RequestBody{
		operations.RequestBody{},
	}, operations.WithRetries(
		utils.RetryConfig{
			Strategy: "backoff",
			Backoff: &utils.BackoffStrategy{
				InitialInterval: 1,
				MaxInterval:     50,
				Exponent:        1.1,
				MaxElapsedTime:  100,
			},
			RetryConnectionErrors: false,
		}))
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response
	}
}

If you'd like to override the default retry strategy for all operations that support retries, you can use the WithRetryConfig option at SDK initialization:

package main

import (
	"context"
	testryan3 "github.com/speakeasy-sdks/test-ryan-3"
	"github.com/speakeasy-sdks/test-ryan-3/internal/utils"
	"github.com/speakeasy-sdks/test-ryan-3/models/components"
	"github.com/speakeasy-sdks/test-ryan-3/models/operations"
	"log"
)

func main() {
	s := testryan3.New(
		testryan3.WithRetryConfig(
			utils.RetryConfig{
				Strategy: "backoff",
				Backoff: &utils.BackoffStrategy{
					InitialInterval: 1,
					MaxInterval:     50,
					Exponent:        1.1,
					MaxElapsedTime:  100,
				},
				RetryConnectionErrors: false,
			}),
		testryan3.WithSecurity(components.Security{
			APIKey: testryan3.String("<YOUR_API_KEY>"),
		}),
	)

	ctx := context.Background()
	res, err := s.Config.SubscribeToWebhooks(ctx, []operations.RequestBody{
		operations.RequestBody{},
	})
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response
	}
}

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. Feel free to open a PR or a Github issue as a proof of concept and we'll do our best to include it in a future release !

SDK Created by Speakeasy

About

A go SDK for accessing the The-Speakeasy-Bar API.

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Packages

No packages published

Languages