client

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2024 License: Apache-2.0 Imports: 22 Imported by: 3

README

Go CleverCloud API client

Go Reference

How to

Installation

Add this client as project dependency

go get -u go.clever-cloud.dev/client
Usage
Client instantiation
import "go.clever-cloud.dev/client"

cc := client.New(
    client.WithAutoOauthConfig(),
) 

Use the client
type Self struct {
    ID string `json:"id"`
}

res := client.Get[Self](context.Background(), cc, "/v2/self")
if res.HasError() {
    // handle res.Error()
}

fmt.Println(res.Payload().ID)

if the operation you want to do does not return anything, use:

res := client.Get[client.Nothing](context.Background(), cc, "/v2/self")

Documentation

Overview

Package client provide HTTP client to interract with Clever Cloud API

Index

Examples

Constants

View Source
const API_ENDPOINT = "https://api.clever-cloud.com"
View Source
const CLIENT_VERSION = "v0.0.1"
View Source
const CONFIG_DIR = "clever-cloud"

For XDG.

View Source
const CONFIG_FILE_NAME = "clever-tools.json"
View Source
const NONCE_SIZE = 48
View Source
const OAUTH_CONSUMER_KEY = "FuRiWZChVFuYaVh04yCHYOnFRtzfHO"
View Source
const OAUTH_CONSUMER_SECRET = "J9k249yV0UqIzsGJWAVD4p1HLj8NyE" // #nosec G101

Variables

This section is empty.

Functions

func WithAutoOauthConfig

func WithAutoOauthConfig() func(*Client)

Set OAuth1 credentials from environment, default: none.

func WithEndpoint

func WithEndpoint(endpoint string) func(*Client)

Set API endoint, default: API_ENDPOINT.

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) func(*Client)

Set custom http client, default: http.DefaultClient.

func WithLogger

func WithLogger(logger logrus.FieldLogger) func(*Client)

Set a logger, default: discard.

func WithOauthConfig

func WithOauthConfig(consumerKey, consumerSecret, accessToken, accessSecret string) func(*Client)

Set OAuth1 credentials, default: none.

func WithUserOauthConfig

func WithUserOauthConfig(accessToken, accessSecret string) func(*Client)

Set OAuth1 user credentials, default: none.

Types

type Authenticator

type Authenticator interface {
	// Add authentication stuff on an HTTP request
	Sign(req *http.Request)

	// Return current user credentials (oauth1 user token, oauth1 user secret)
	Oauth1UserCredentials() (string, string)
}

Authenticator is called to authenticate an HTTP request.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a wrapped HTTP client used to contact CleverCloud API.

func New

func New(options ...func(*Client)) *Client

New instantiate a new CleverCloud client with options.

func (*Client) Oauth1UserCredentials added in v0.0.7

func (c *Client) Oauth1UserCredentials() (string, string)

type Nothing added in v0.0.6

type Nothing struct{}

type OAuth1Config

type OAuth1Config struct {
	ConsumerKey    string `json:"-"`
	ConsumerSecret string `json:"-"`
	AccessToken    string `json:"token"`
	AccessSecret   string `json:"secret"`
}

OAuth1Config own credentials to contact CleverCloud API.

func (OAuth1Config) Oauth1UserCredentials added in v0.0.7

func (auth OAuth1Config) Oauth1UserCredentials() (string, string)

func (*OAuth1Config) Sign

func (auth *OAuth1Config) Sign(req *http.Request)

Sign an HTTP request with the given OAuth1 signature.

type Response

type Response[T any] interface {
	Error() error
	HasError() bool

	StatusCode() int
	IsNotFoundError() bool

	SozuID() string

	Equal(anotherResponse Response[T]) bool

	Payload() *T
}

func Delete

func Delete[T any](ctx context.Context, c *Client, path string) Response[T]

Perform a DELETE request.

func Get

func Get[T any](ctx context.Context, c *Client, path string) Response[T]

Perform a GET request.

Example

Simple Get.

package main

import (
	"context"
	"fmt"

	"go.clever-cloud.dev/client"
)

func main() {
	cc := client.New(client.WithAutoOauthConfig())

	res := client.Get[map[string]interface{}](context.Background(), cc, "/v2/self")
	if res.HasError() {
		panic(res.Error())
	}

	fmt.Printf("%+v\n", res.Payload())
}
Output:

func Patch

func Patch[T any](ctx context.Context, c *Client, path string, payload interface{}) Response[T]

Perform a PATCH request.

func Post

func Post[T any](ctx context.Context, c *Client, path string, payload interface{}) Response[T]

Perform a POST request.

func Put

func Put[T any](ctx context.Context, c *Client, path string, payload interface{}) Response[T]

Perform a PUT request.

type StreamEvent

type StreamEvent[T any] struct {
	Event string
	Data  []byte
	ID    []byte
	Retry int64
}

https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format

func (*StreamEvent[T]) String

func (se *StreamEvent[T]) String() string

type StreamResponse

type StreamResponse[T any] interface {
	Error() error
	HasError() bool

	StatusCode() int
	IsNotFoundError() bool

	SozuID() string

	Equal(anotherResponse StreamResponse[T]) bool

	Close()
	Payload() <-chan *StreamEvent[T]
}

func Stream

func Stream[T any](ctx context.Context, c *Client, path string) StreamResponse[T]

Perform an SSE request.

Example

SSE.

package main

import (
	"context"
	"fmt"

	"go.clever-cloud.dev/client"
)

func main() {
	type logEntry map[string]interface{}

	cc := client.New(client.WithAutoOauthConfig())

	res := client.Stream[logEntry](context.Background(), cc, "/v4/logs")
	defer res.Close()

	for msg := range res.Payload() {
		fmt.Printf("%+v\n", msg)
	}
}
Output:

Jump to

Keyboard shortcuts

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