Skip to content

adilansari/metronome-go-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Reference rcard License

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/adilansari/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/adilansari/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.NewClientWithResponses(
		"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.CreateCustomerWithResponse(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.Body))
	}

	// Response available as a struct without explicit parsing
	customer := resp.JSON200.Data

	fmt.Printf("'id' of created customer: %s\n", customer.Id)
	fmt.Printf("'name' of created customer: %s\n", customer.Name)
	fmt.Printf("'aliases' for the created customer: %s\n", customer.IngestAliases)
}

// Sample output:
// 'id' of created customer: 1eeb3160-1d1a-40dc-9571-1a65fefbc975
// 'name' of created customer: my_customer_id
// 'aliases' for the created customer: [my_customer_alias]

Requesting updates

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