Skip to content

cloudsmith-io/cloudsmith-go-v2

Repository files navigation

cloudsmith

Developer-friendly & type-safe Go SDK specifically catered to leverage cloudsmith API.

Built by Speakeasy License: Apache 2.0

Summary

Cloudsmith API: The API to the Cloudsmith Service

For more information about the API: Cloudsmith API Reference

Table of Contents

SDK Installation

To add the SDK as a dependency to your project:

go get github.com/cloudsmith-io/cloudsmith-go-v2

SDK Example Usage

Example

package main

import (
	"context"
	cloudsmith "github.com/cloudsmith-io/cloudsmith-go-v2"
	"github.com/cloudsmith-io/cloudsmith-go-v2/models/operations"
	"log"
)

func main() {
	ctx := context.Background()

	s := cloudsmith.New(
		cloudsmith.WithSecurity("<YOUR_API_KEY_HERE>"),
	)

	res, err := s.Metadata.MetadataPackagesList(ctx, operations.MetadataPackagesListRequest{
		PackageSlugPerm: "<value>",
	})
	if err != nil {
		log.Fatal(err)
	}
	if res.PaginatedArtifactMetadataReadList != nil {
		// handle response
	}
}

Authentication

Per-Client Security Schemes

This SDK supports the following security scheme globally:

Name Type Scheme
Apikey apiKey API key

You can configure it using the WithSecurity option when initializing the SDK client instance. For example:

package main

import (
	"context"
	cloudsmith "github.com/cloudsmith-io/cloudsmith-go-v2"
	"github.com/cloudsmith-io/cloudsmith-go-v2/models/operations"
	"log"
)

func main() {
	ctx := context.Background()

	s := cloudsmith.New(
		cloudsmith.WithSecurity("<YOUR_API_KEY_HERE>"),
	)

	res, err := s.Metadata.MetadataPackagesList(ctx, operations.MetadataPackagesListRequest{
		PackageSlugPerm: "<value>",
	})
	if err != nil {
		log.Fatal(err)
	}
	if res.PaginatedArtifactMetadataReadList != nil {
		// handle response
	}
}

Available Resources and Operations

Available methods

Before using this endpoint, the Policy to simulate must be created with the creation endpoint. To experiment with the policy, it is recommended to create the policy with disabled=True, so that it does not take effect outside the simulation endpoint.

This endpoint evaluates all packages in the workspace, generating a "decision log" for each evaluation. Each log contains:

  • Package metadata provided to the policy engine at runtime.
  • Output from the user-defined Rego policy.

No actions associated with the policy are executed. Instead, the endpoint reports what would happen to each package if the policy were active.

Pagination

Some of the endpoints in this SDK support pagination. To use pagination, you make your SDK calls as usual, but the returned response object will have a Next method that can be called to pull down the next group of results. If the return value of Next is nil, then there are no more pages to be fetched.

Here's an example of one such pagination call:

package main

import (
	"context"
	cloudsmith "github.com/cloudsmith-io/cloudsmith-go-v2"
	"github.com/cloudsmith-io/cloudsmith-go-v2/models/operations"
	"log"
)

func main() {
	ctx := context.Background()

	s := cloudsmith.New(
		cloudsmith.WithSecurity("<YOUR_API_KEY_HERE>"),
	)

	res, err := s.Workspaces.WorkspacesPoliciesList(ctx, operations.WorkspacesPoliciesListRequest{
		Page:      694333,
		Query:     cloudsmith.Pointer("name: foo OR name: bar"),
		Sort:      cloudsmith.Pointer("-version,updated_at"),
		Workspace: "<value>",
	})
	if err != nil {
		log.Fatal(err)
	}
	if res.PaginatedPolicyList != nil {
		for {
			// handle items

			res, err = res.Next()

			if err != nil {
				// handle error
			}

			if res == nil {
				break
			}
		}
	}
}

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 retry.Config object to the call by using the WithRetries option:

package main

import (
	"context"
	cloudsmith "github.com/cloudsmith-io/cloudsmith-go-v2"
	"github.com/cloudsmith-io/cloudsmith-go-v2/models/operations"
	"github.com/cloudsmith-io/cloudsmith-go-v2/retry"
	"log"
	"models/operations"
)

func main() {
	ctx := context.Background()

	s := cloudsmith.New(
		cloudsmith.WithSecurity("<YOUR_API_KEY_HERE>"),
	)

	res, err := s.Metadata.MetadataPackagesList(ctx, operations.MetadataPackagesListRequest{
		PackageSlugPerm: "<value>",
	}, operations.WithRetries(
		retry.Config{
			Strategy: "backoff",
			Backoff: &retry.BackoffStrategy{
				InitialInterval: 1,
				MaxInterval:     50,
				Exponent:        1.1,
				MaxElapsedTime:  100,
			},
			RetryConnectionErrors: false,
		}))
	if err != nil {
		log.Fatal(err)
	}
	if res.PaginatedArtifactMetadataReadList != 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"
	cloudsmith "github.com/cloudsmith-io/cloudsmith-go-v2"
	"github.com/cloudsmith-io/cloudsmith-go-v2/models/operations"
	"github.com/cloudsmith-io/cloudsmith-go-v2/retry"
	"log"
)

func main() {
	ctx := context.Background()

	s := cloudsmith.New(
		cloudsmith.WithRetryConfig(
			retry.Config{
				Strategy: "backoff",
				Backoff: &retry.BackoffStrategy{
					InitialInterval: 1,
					MaxInterval:     50,
					Exponent:        1.1,
					MaxElapsedTime:  100,
				},
				RetryConnectionErrors: false,
			}),
		cloudsmith.WithSecurity("<YOUR_API_KEY_HERE>"),
	)

	res, err := s.Metadata.MetadataPackagesList(ctx, operations.MetadataPackagesListRequest{
		PackageSlugPerm: "<value>",
	})
	if err != nil {
		log.Fatal(err)
	}
	if res.PaginatedArtifactMetadataReadList != nil {
		// handle response
	}
}

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.

By Default, an API error will return apierrors.APIError. When custom error responses are specified for an operation, the SDK may also return their associated error. You can refer to respective Errors tables in SDK docs for more details on possible error types for each operation.

For example, the MetadataPackagesList function may return the following errors:

Error Type Status Code Content Type
apierrors.ErrorDetail 401, 403, 404, 422 application/json
apierrors.APIError 4XX, 5XX */*

Example

package main

import (
	"context"
	"errors"
	cloudsmith "github.com/cloudsmith-io/cloudsmith-go-v2"
	"github.com/cloudsmith-io/cloudsmith-go-v2/models/apierrors"
	"github.com/cloudsmith-io/cloudsmith-go-v2/models/operations"
	"log"
)

func main() {
	ctx := context.Background()

	s := cloudsmith.New(
		cloudsmith.WithSecurity("<YOUR_API_KEY_HERE>"),
	)

	res, err := s.Metadata.MetadataPackagesList(ctx, operations.MetadataPackagesListRequest{
		PackageSlugPerm: "<value>",
	})
	if err != nil {

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

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

Server Selection

Override Server URL Per-Client

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

package main

import (
	"context"
	cloudsmith "github.com/cloudsmith-io/cloudsmith-go-v2"
	"github.com/cloudsmith-io/cloudsmith-go-v2/models/operations"
	"log"
)

func main() {
	ctx := context.Background()

	s := cloudsmith.New(
		cloudsmith.WithServerURL("https://api.cloudsmith.io/v2/"),
		cloudsmith.WithSecurity("<YOUR_API_KEY_HERE>"),
	)

	res, err := s.Metadata.MetadataPackagesList(ctx, operations.MetadataPackagesListRequest{
		PackageSlugPerm: "<value>",
	})
	if err != nil {
		log.Fatal(err)
	}
	if res.PaginatedArtifactMetadataReadList != 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/cloudsmith-io/cloudsmith-go-v2"
)

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

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

Development

Maturity

This SDK is generally available (GA) and ready for production use. We follow semantic versioning, so you can expect backwards-compatible changes within a major 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

Cloudsmith SDK v2 in Go

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages