Developer-friendly & type-safe Go SDK specifically catered to leverage cloudsmith API.
Cloudsmith API: The API to the Cloudsmith Service
For more information about the API: Cloudsmith API Reference
To add the SDK as a dependency to your project:
go get github.com/cloudsmith-io/cloudsmith-go-v2package 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.Analytics.AnalyticsLogsPackageList(ctx, operations.AnalyticsLogsPackageListRequest{
Query: cloudsmith.Pointer("package:semver OR package_format:ruby"),
Workspace: "<value>",
})
if err != nil {
log.Fatal(err)
}
if res.PaginatedPackageLogsResponseList != nil {
// handle response
}
}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.Analytics.AnalyticsLogsPackageList(ctx, operations.AnalyticsLogsPackageListRequest{
Query: cloudsmith.Pointer("package:semver OR package_format:ruby"),
Workspace: "<value>",
})
if err != nil {
log.Fatal(err)
}
if res.PaginatedPackageLogsResponseList != nil {
// handle response
}
}Available methods
- AnalyticsLogsPackageList - Returns package log events, including uploads, copies, deletes, etc.
- MetadataPackagesList - List metadata for a package
- MetadataPackagesCreate - Attach metadata to a package
- MetadataPackagesRetrieve - Retrieve a package metadata entry
- MetadataPackagesPartialUpdate - Update a package metadata entry
- MetadataPackagesDestroy - Delete a package metadata entry
- MetadataValidateCreate - Validate a metadata payload
- PackagesVulnerabilitiesList - OSV advisory data for a specific package.
- PackagesVulnerabilitiesCountsList - OSV advisory counts for a specific package.
- WorkspacesDomainsList - List active custom and Cloudsmith-owned domains available to a workspace. Cloudsmith-owned domains are the shared hostnames every workspace can use; custom domains are listed when enabled and validated, regardless of subscription status. Workspace membership is required, including for service accounts. Discovering a domain does not grant repository access.
For client setup, pass both repository and domain_type, then take the first result as the preferred domain for that repository and purpose. Without repository, the response is a workspace-wide inventory: a REPOSITORY domain must only be used for the repository named in that entry. The endpoint returns results in preference order: REPOSITORY domains come first, then WORKSPACE domains, then GLOBAL domains. Within each custom domain scope, primary domains come before the rest, then the oldest domain comes first.
Responses contain a results array. Follow the Link header's next relation until absent to discover all domains; an empty array means no domains match the filters. A hostname can appear under multiple domain types. Native types are only listed for package formats enabled for the request. Clients should tolerate new domain types.
This endpoint discovers hostnames, not complete package-manager URLs. URL paths and authentication depend on the format and domain scope; custom domains are not necessarily a hostname-only replacement in an existing URL.
- WorkspacesPoliciesList - List policies.
- WorkspacesPoliciesCreate - Create a policy.
- WorkspacesPoliciesRetrieve - Retrieve a policy.
- WorkspacesPoliciesUpdate - Update a policy.
- WorkspacesPoliciesPartialUpdate - Partially update a policy.
- WorkspacesPoliciesDestroy - Delete a policy.
- WorkspacesPoliciesActionsList - List all actions for the policy.
- WorkspacesPoliciesActionsCreate - Create an action for a policy.
- WorkspacesPoliciesActionsRetrieve - Retrieve an action for a policy.
- WorkspacesPoliciesActionsUpdate - Update an action for a policy.
- WorkspacesPoliciesActionsPartialUpdate - Partially update an action for a policy.
- WorkspacesPoliciesActionsDestroy - Delete an action for a policy.
- WorkspacesPoliciesSimulateList - Simulates the execution of a Policy to verify its behavior without taking any actions.
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.
- WorkspacesPoliciesDecisionLogsV1List - List policy evaluation decision logs.
- WorkspacesPoliciesDecisionLogsV1Retrieve - Retrieve the full decision log JSON from S3.
- WorkspacesPoliciesValidate - Check that a Rego policy compiles, without creating or updating anything. Use this to catch syntax and type errors while authoring a policy, before submitting it to the create or update endpoints, which apply the same check.
Returns is_valid, plus a findings list describing each problem with a code, a message and, where known, the line it occurs on. A policy is valid when findings contains nothing of severity error. The submitted Rego is not stored.
- WorkspacesVulnerabilitiesRepoPackagesList - OSV advisory data for a specific repo.
- WorkspacesVulnerabilitiesPackagesList - OSV advisory data for a specific workspace.
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
}
}
}
}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.Analytics.AnalyticsLogsPackageList(ctx, operations.AnalyticsLogsPackageListRequest{
Query: cloudsmith.Pointer("package:semver OR package_format:ruby"),
Workspace: "<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.PaginatedPackageLogsResponseList != 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.Analytics.AnalyticsLogsPackageList(ctx, operations.AnalyticsLogsPackageListRequest{
Query: cloudsmith.Pointer("package:semver OR package_format:ruby"),
Workspace: "<value>",
})
if err != nil {
log.Fatal(err)
}
if res.PaginatedPackageLogsResponseList != nil {
// handle response
}
}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 | */* |
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())
}
}
}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.Analytics.AnalyticsLogsPackageList(ctx, operations.AnalyticsLogsPackageListRequest{
Query: cloudsmith.Pointer("package:semver OR package_format:ruby"),
Workspace: "<value>",
})
if err != nil {
log.Fatal(err)
}
if res.PaginatedPackageLogsResponseList != nil {
// handle response
}
}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.
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.
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.