Skip to content

FlatFilers/flatfile-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flatfile Go Library

fern shield go shield

The Flatfile Go library provides convenient access to the Flatfile API from Go.

Documentation

API reference documentation is available here.

Requirements

This module requires Go version >= 1.13.

Installation

Run the following command to use the Flatfile Go library in your module:

go get github.com/FlatFilers/flatfile-go

Usage

import flatfileclient "github.com/FlatFilers/flatfile-go/client"

client := flatfileclient.NewClient(flatfileclient.WithToken("<YOUR_AUTH_TOKEN>"))

Create Environment

import (
  flatfile       "github.com/FlatFilers/flatfile-go"
  flatfileclient "github.com/FlatFilers/flatfile-go/client"
)

client := flatfileclient.NewClient(flatfileclient.WithToken("<YOUR_AUTH_TOKEN>"))
response, err := client.Environments.Create(
  context.TODO(),
  &flatfile.EnvironmentConfigCreate{
    Name:   "development",
    IsProd: false,
  },
)

Timeouts

Setting a timeout for each individual request is as simple as using the standard context library. Setting a one second timeout for an individual API call looks like the following:

ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()

response, err := client.Environments.Create(
  context.TODO(),
  &flatfile.EnvironmentConfigCreate{
    Name:   "development",
    IsProd: false,
  },
)

Client Options

A variety of client options are included to adapt the behavior of the library, which includes configuring authorization tokens to be sent on every request, or providing your own instrumented *http.Client. Both of these options are shown below:

client := flatfileclient.NewClient(
  flatfileclient.WithToken("<YOUR_AUTH_TOKEN>"),
  flatfileclient.WithHTTPClient(
    &http.Client{
      Timeout: 5 * time.Second,
    },
  ),
)

Providing your own *http.Client is recommended. Otherwise, the http.DefaultClient will be used, and your client will wait indefinitely for a response (unless the per-request, context-based timeout is used).

Errors

Structured error types are returned from API calls that return non-success status codes. For example, you can check if the error was due to a bad request (i.e. status code 400) with the following:

response, err := client.Environments.GetEnvironmentEventToken(
  context.TODO(),
  &flatfile.GetEnvironmentEventTokenRequest{
    EnvironmentId: "invalid-id",
  },
)
if err != nil {
  if badRequestErr, ok := err.(*flatfile.BadRequestError);
    // Do something with the bad request ...
  }
  return err
}

These errors are also compatible with the errors.Is and errors.As APIs, so you can access the error like so:

response, err := client.Environments.GetEnvironmentEventToken(
  context.TODO(),
  &flatfile.GetEnvironmentEventTokenRequest{
    EnvironmentId: "invalid-id",
  },
)
if err != nil {
  var badRequestErr *flatfile.BadRequestError
  if errors.As(err, badRequestErr) {
    // Do something with the bad request ...
  }
  return err
}

If you'd like to wrap the errors with additional information and still retain the ability to access the type with errors.Is and errors.As, you can use the %w directive:

response, err := client.Environments.GetEnvironmentEventToken(
  context.TODO(),
  &flatfile.GetEnvironmentEventTokenRequest{
    EnvironmentId: "invalid-id",
  },
)
if err != nil {
  return fmt.Errorf("failed to generate response: %w", err)
}

Beta Status

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

Contributing

While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!