client

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

client

The Go client for Edgescale's products. It's for your backend:

  • billing and OTP take your project's platform key
  • auth's server API takes a server app's secret, or the key with auth access
go get github.com/edgescaleDev/client

It needs Go 1.26 or newer. Versions are v0.x until the API is called stable, so a minor version may change it.

A first call

c, err := client.New("k3x9m2q7pa", client.WithKey(os.Getenv("EDGESCALE_KEY")))
if err != nil {
	return err
}
plans, err := client.Decode[[]billing.Plan](c.Billing.ListPlans(ctx, nil))

k3x9m2q7pa is the project's uid, from its Settings in the dashboard. Every call goes to the project's own host: {uid}.projects.apisdomain.dev for billing and OTP, and {uid}.auth.apisdomain.dev for auth.

Each product is a package generated from its public spec, in spec/: billing, otp and auth. Their calls return an *http.Response. client.Decode reads the answer into the type you name, or returns an *client.Error.

Keys and staging

  • A project is one environment. Build and test against a second project, such as staging, with its own uid and keys.
  • A test key, eds_ak_test_…, is for a product's test mode, such as OTP's test numbers. Billing has no test mode, and refuses one with a 403.

Auth's server API

c, err := client.New(uid, client.WithServerSecret(os.Getenv("EDGESCALE_SERVER_SECRET")))
user, err := client.Decode[auth.User](c.Auth.AdminGetUser(ctx, id))

A server app's secret, eds_as_…, is made in the project's Auth tab. The client sends it as the bearer on /v1/admin, and as Edgescale-Server-Secret on the routes your backend calls for its users, such as sign-in.

  • Say who you're calling for with the call's params. Auth records only what you send, never your server's own address:

    addr := realIP(r) // the person's address, as your server saw it
    tokens, err := client.Decode[auth.Tokens](c.Auth.SignIn(ctx, &auth.SignInParams{EdgescaleClientAddress: &addr}, body))
    
  • Or use the platform key, with auth access set in the dashboard. With no secret, the client sends the key in the secret's place:

    • read looks a user up, lists their sessions, and searches
    • write does the rest, signing a person in included
    • a test key, eds_ak_test_…, is refused with a 403: auth has no test mode
  • One client can hold both credentials: client.New(uid, client.WithKey(…), client.WithServerSecret(…)). Auth then gets the secret.

Errors

A failed call is an *client.Error, with the status and the answer's errors. Branch on an error's code, never its message:

var e *client.Error
if errors.As(err, &e) && e.Has(1007) {
	time.Sleep(e.RetryAfter) // too many: 429, with Retry-After
}
Code Status What
1000 500 our own failure
1001 401 a missing, wrong or revoked key or secret
1002 403 not allowed, such as a key without the access, or a test key where there's no test mode
1003 404 not found
1004 400 a request that can't be read
1005 422 an invalid field
1007 429 too many, with RetryAfter
1008 503 briefly unavailable, with RetryAfter

A failure from in front of the API, such as a proxy's 502, is an *client.Error with no errors.

The client doesn't retry. Pass your own http.Client with client.WithHTTPClient, with its timeouts and retries.

Notices and paging

client.Read returns the whole answer:

  • Result
  • Messages: notices that aren't failures, such as auth's 3000 when it didn't store a header you sent
  • Page: a list's paging
page := 1
a, err := client.Read[[]billing.Plan](c.Billing.ListPlans(ctx, &billing.ListPlansParams{Page: &page}))
// a.Page.TotalPages is the last page

Webhooks

Billing's webhooks to you are Standard Webhooks. Verify them with its Go library, github.com/standard-webhooks/standard-webhooks/libraries/go:

wh, err := standardwebhooks.NewWebhook(os.Getenv("EDGESCALE_WEBHOOK_SECRET"))
// in the handler:
body, _ := io.ReadAll(r.Body)
if err := wh.Verify(body, r.Header); err != nil {
	http.Error(w, "bad signature", http.StatusBadRequest)
	return
}

The specs

spec/ holds each product's public spec, in OpenAPI 3.0. The packages are generated from them with go generate, so you can generate a client in another language from the same files.

License

Apache 2.0. See LICENSE.

Documentation

Overview

Package client calls Edgescale's products from a backend: billing and OTP with the project's platform key, and auth's server API with a server app's secret or the key.

c, err := client.New("k3x9m2q7pa", client.WithKey(os.Getenv("EDGESCALE_KEY")))
plans, err := client.Decode[[]billing.Plan](c.Billing.ListPlans(ctx, nil))

Each product is a package generated from its public spec, in spec/.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode[T any](res *http.Response, err error) (T, error)

Decode reads one answer: its result as T on a success, or an *Error. It takes a generated call's two results as they come, and closes the body.

Types

type Answer

type Answer[T any] struct {
	Result   T
	Messages []Message // notices that aren't failures, such as auth's 3000 for a header it didn't store
	Page     *PageInfo // a list's result_info, or nil
}

Answer is a whole envelope, for when its notices or a list's paging matter.

func Read

func Read[T any](res *http.Response, err error) (Answer[T], error)

Read is Decode with the whole envelope: the result, its notices, and a list's paging.

type Client

type Client struct {
	Billing billing.ClientInterface // takes the key
	OTP     otp.ClientInterface     // takes the key
	Auth    auth.ClientInterface    // takes the server secret, or the key when there's no secret
}

Client is one project's products, each the generated client of its spec.

func New

func New(uid string, opts ...Option) (*Client, error)

New is a client for one project, by its uid. It needs a key, a server secret, or both.

type Error

type Error struct {
	Status     int
	Errors     []Message
	RetryAfter time.Duration // from a 429 or a 503 that says when to try again
}

Error is a failed call: its status, and the envelope's errors. A failure from in front of the API, such as a proxy's that isn't an envelope, has none.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Has

func (e *Error) Has(code int) bool

Has reports whether code is one of the errors, such as 1005 for an invalid field.

type Message

type Message struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

Message is one of an answer's errors or notices. Code is what to branch on, never Message.

type Option

type Option func(*options)

Option is one of New's settings.

func WithHTTPClient

func WithHTTPClient(hc *http.Client) Option

WithHTTPClient sends every call through hc, http.DefaultClient unless set; nil keeps the default. Its timeouts and retries are the caller's.

func WithKey

func WithKey(key string) Option

WithKey is the project's platform key, eds_ak_…, or eds_ak_test_… for a product's test mode.

func WithServerSecret

func WithServerSecret(secret string) Option

WithServerSecret is a server app's secret, eds_as_…, from the project's Auth tab.

func WithZone

func WithZone(zone string) Option

WithZone is the zone under the project's hosts: apisdomain.dev unless set, and dev.edgescale.dev for dev.

type PageInfo

type PageInfo struct {
	Page       int `json:"page"`
	PerPage    int `json:"per_page"`
	Count      int `json:"count"` // the length of the result
	TotalCount int `json:"total_count"`
	TotalPages int `json:"total_pages"`
}

PageInfo is a list's paging. Its last page is the one where Page reaches TotalPages.

Directories

Path Synopsis
Package auth provides primitives to interact with the openapi HTTP API.
Package auth provides primitives to interact with the openapi HTTP API.
Package billing provides primitives to interact with the openapi HTTP API.
Package billing provides primitives to interact with the openapi HTTP API.
Package otp provides primitives to interact with the openapi HTTP API.
Package otp provides primitives to interact with the openapi HTTP API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL