Skip to content

firehydrant/firehydrant-go-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Manage incidents from ring to retro

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

Summary

FireHydrant API: The FireHydrant API is based around REST. It uses Bearer token authentication and returns JSON responses. You can use the FireHydrant API to configure integrations, define incidents, and set up webhooks--anything you can do on the FireHydrant UI.

Base API endpoint

https://api.firehydrant.io/v1

Current version

v1

Authentication

All requests to the FireHydrant API require an Authorization header with the value set to Bearer {token}. FireHydrant supports bot tokens to act on behalf of a computer instead of a user's account. This prevents integrations from breaking when people leave your organization or their token is revoked. See the Bot tokens section (below) for more information on this.

An example of a header to authenticate against FireHydrant would look like:

Authorization: Bearer fhb-thisismytoken

Bot tokens

To access the FireHydrant API, you must authenticate with a bot token. (You must have owner permissions on your organization to see bot tokens.) Bot users allow you to interact with the FireHydrant API by using token-based authentication. To create bot tokens, log in to your organization and refer to the Bot users page.

Bot tokens enable you to create a bot that has no ties to any user. Normally, all actions associated with an API token are associated with the user who created it. Bot tokens attribute all actions to the bot user itself. This way, all data associated with the token actions can be performed against the FireHydrant API without a user.

Every request to the API is authenticated unless specified otherwise.

Rate Limiting

Currently, requests made with bot tokens are rate limited on a per-account level. If your account has multiple bot token then the rate limit is shared across all of them. As of February 7th, 2023, the rate limit is at least 50 requests per account every 10 seconds, or 300 requests per minute.

Rate limited responses will be served with a 429 status code and a JSON body of:

{"error": "rate limit exceeded"}

and headers of:

"RateLimit-Limit" -> the maximum number of requests in the rate limit pool
"Retry-After" -> the number of seconds to wait before trying again

How lists are returned

API lists are returned as arrays. A paginated entity in FireHydrant will return two top-level keys in the response object: a data key and a pagination key.

Paginated requests

The data key is returned as an array. Each item in the array includes all of the entity data specified in the API endpoint. (The per-page default for the array is 20 items.)

Pagination is the second key (pagination) returned in the overall response body. It includes medtadata around the current page, total count of items, and options to go to the next and previous page. All of the specifications returned in the pagination object are available as URL parameters. So if you want to specify, for example, going to the second page of a response, you can send a request to the same endpoint but pass the URL parameter page=2.

For example, you might request https://api.firehydrant.io/v1/environments/ to retrieve environments data. The JSON returned contains the above-mentioned data section and pagination section. The data section includes various details about an incident, such as the environment name, description, and when it was created.

{
  "data": [
    {
      "id": "f8125cf4-b3a7-4f88-b5ab-57a60b9ed89b",
      "name": "Production - GCP",
      "description": "",
      "created_at": "2021-02-17T20:02:10.679Z"
    },
    {
      "id": "a69f1f58-af77-4708-802d-7e73c0bf261c",
      "name": "Staging",
      "description": "",
      "created_at": "2021-04-16T13:41:59.418Z"
    }
  ],
  "pagination": {
    "count": 2,
    "page": 1,
    "items": 2,
    "pages": 1,
    "last": 1,
    "prev": null,
    "next": null
  }
}

To request the second page, you'd request the same endpoint with the additional query parameter of page in the URL:

GET https://api.firehydrant.io/v1/environments?page=2

If you need to modify the number of records coming back from FireHydrant, you can use the per_page parameter (max is 200):

GET https://api.firehydrant.io/v1/environments?per_page=50

Table of Contents

SDK Installation

To add the SDK as a dependency to your project:

go get github.com/firehydrant/firehydrant-go-sdk

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"
	"firehydrant"
	"firehydrant/models/components"
	"log"
)

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

	s := firehydrant.New(
		firehydrant.WithSecurity(components.Security{
			APIKey: "<YOUR_API_KEY_HERE>",
		}),
	)

	res, err := s.AccountSettings.Ping(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response
	}
}

SDK Example Usage

Example

package main

import (
	"context"
	"firehydrant"
	"firehydrant/models/components"
	"log"
)

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

	s := firehydrant.New(
		firehydrant.WithSecurity(components.Security{
			APIKey: "<YOUR_API_KEY_HERE>",
		}),
	)

	res, err := s.AccountSettings.Ping(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response
	}
}

Available Resources and Operations

Available methods

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"
	"firehydrant"
	"firehydrant/models/components"
	"firehydrant/retry"
	"log"
	"models/operations"
)

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

	s := firehydrant.New(
		firehydrant.WithSecurity(components.Security{
			APIKey: "<YOUR_API_KEY_HERE>",
		}),
	)

	res, err := s.AccountSettings.Ping(ctx, 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 != 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"
	"firehydrant"
	"firehydrant/models/components"
	"firehydrant/retry"
	"log"
)

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

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

	res, err := s.AccountSettings.Ping(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res != 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 sdkerrors.SDKError. 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 CreateService function may return the following errors:

Error Type Status Code Content Type
sdkerrors.ErrorEntity 400 application/json
sdkerrors.SDKError 4XX, 5XX */*

Example

package main

import (
	"context"
	"errors"
	"firehydrant"
	"firehydrant/models/components"
	"firehydrant/models/sdkerrors"
	"log"
)

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

	s := firehydrant.New(
		firehydrant.WithSecurity(components.Security{
			APIKey: "<YOUR_API_KEY_HERE>",
		}),
	)

	res, err := s.CatalogEntries.CreateService(ctx, components.CreateService{
		Name: "<value>",
	})
	if err != nil {

		var e *sdkerrors.ErrorEntity
		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

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"
	"firehydrant"
	"firehydrant/models/components"
	"log"
)

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

	s := firehydrant.New(
		firehydrant.WithServerURL("https://api.firehydrant.io/"),
		firehydrant.WithSecurity(components.Security{
			APIKey: "<YOUR_API_KEY_HERE>",
		}),
	)

	res, err := s.AccountSettings.Ping(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res != 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.

Special Types

This SDK defines the following custom types to assist with marshalling and unmarshalling data.

Date

types.Date is a wrapper around time.Time that allows for JSON marshaling a date string formatted as "2006-01-02".

Usage

d1 := types.NewDate(time.Now()) // returns *types.Date

d2 := types.DateFromTime(time.Now()) // returns types.Date

d3, err := types.NewDateFromString("2019-01-01") // returns *types.Date, error

d4, err := types.DateFromString("2019-01-01") // returns types.Date, error

d5 := types.MustNewDateFromString("2019-01-01") // returns *types.Date and panics on error

d6 := types.MustDateFromString("2019-01-01") // returns types.Date and panics on error

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

Stars

Watchers

Forks

Packages

No packages published

Contributors 6

Languages