Skip to content

Go client for metronome generated from its OpenAPI spec

License

Notifications You must be signed in to change notification settings

tigrisdata/metronome-go-client

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Metronome Go client

Go client for Metronome generated from its OpenAPI spec

This is not an official client, and was generated for personal use from their OpenAPI spec.

Installation

By using this client you'll avoid a lot of boilerplate code to perform all the marshalling and unmarshalling into objects when using Metronome HTTP APIs.

Install in your project to have it as a dependency in go.mod:

go get github.com/tigrisdata/metronome-go-client@latest

Usage

Authentication

Metronome requires all HTTP requests to include a authorization header for authentication:

GET /v1/... HTTP/1.1
Host: api.metronome.com
Authorization: Bearer MY_TOKEN

Instead of adding authorization header to every request when using this go client, you can simply register a callback as:

authProvider, err := securityprovider.NewSecurityProviderBearerToken("MY_TOKEN")
if err != nil {
	panic(err)
}

client, err := metronome.NewClient("https://api.metronome.com/v1", metronome.WithRequestEditorFn(authProvider.Intercept))

Create customer example

Following is a complete example you can try in your project. Replace the REPLACE_ME with your own token generated from Metronome account.

package main

import (
	"context"
	"fmt"
	"net/http"

	"github.com/deepmap/oapi-codegen/pkg/securityprovider"

	"github.com/tigrisdata/metronome-go-client"
)

func main() {
	// API key based authentication. Add your metronome auth token here.
	// Learn more about auth: https://docs.metronome.com/using-the-api/authorization/
	authProvider, err := securityprovider.NewSecurityProviderBearerToken("REPLACE_ME")
	if err != nil {
		panic(err)
	}

	// Client will be used to perform all operations on metronome.
	// Auth provider generated above will implicitly add an authorization token to all requests.
	client, err := metronome.NewClient("https://api.metronome.com/v1", metronome.WithRequestEditorFn(authProvider.Intercept))
	if err != nil {
		panic(err)
	}

	// JSON request for CreateCustomer
	createCustomerBody := metronome.CreateCustomerJSONRequestBody{
		IngestAliases: &[]string{"my_customer_alias"},
		Name:          "my_customer_id",
	}

	// HTTP POST call to "/customers" endpoint
	resp, err := client.CreateCustomer(context.TODO(), createCustomerBody)
	if err != nil {
		panic(err)
	}

	// Checking if request succeeded
	if resp.StatusCode != http.StatusOK {
		panic(fmt.Errorf("metronome request failed: %s", resp.Status))
	}

	// Using built-in parsing to convert response to a native Go struct
	parsed, err := metronome.ParseCreateCustomerResponse(resp)
	if err != nil {
		panic(err)
	}

	// Sample output:
	// request succeeded: {ExternalId:my_customer_alias Id:7d8e8341-f271-4738-8a7f-cf7d6c2418f3 IngestAliases:[my_customer_alias] Name:my_customer_id}
	fmt.Printf("request succeeded: %+v", parsed.JSON200.Data)
}

Requesting updates

  • Since this is not official client, the API may be out of date
  • Please open an issue in the repo and we'll try to update the client to latest

About

Go client for metronome generated from its OpenAPI spec

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Shell 76.7%
  • Makefile 23.3%