ovh

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2020 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Overview

Package ovh provides a HTTP wrapper for the OVH API.

Index

Constants

View Source
const (
	OvhEU        = "https://eu.api.ovh.com/1.0"
	OvhCA        = "https://ca.api.ovh.com/1.0"
	OvhUS        = "https://api.us.ovhcloud.com/1.0"
	KimsufiEU    = "https://eu.api.kimsufi.com/1.0"
	KimsufiCA    = "https://ca.api.kimsufi.com/1.0"
	SoyoustartEU = "https://eu.api.soyoustart.com/1.0"
	SoyoustartCA = "https://ca.api.soyoustart.com/1.0"
	RunaboveCA   = "https://api.runabove.com/1.0"
)

Endpoints

View Source
const DefaultTimeout = 180 * time.Second

DefaultTimeout api requests after 180s

Variables

View Source
var (
	ReadOnly      = []string{"GET"}
	ReadWrite     = []string{"GET", "POST", "PUT", "DELETE"}
	ReadWriteSafe = []string{"GET", "POST", "PUT"}
)

Map user friendly access level names to corresponding HTTP verbs

View Source
var Endpoints = map[string]string{
	"ovh-eu":        OvhEU,
	"ovh-ca":        OvhCA,
	"ovh-us":        OvhUS,
	"kimsufi-eu":    KimsufiEU,
	"kimsufi-ca":    KimsufiCA,
	"soyoustart-eu": SoyoustartEU,
	"soyoustart-ca": SoyoustartCA,
	"runabove-ca":   RunaboveCA,
}

Endpoints conveniently maps endpoints names to their URI for external configuration

View Source
var (
	ErrAPIDown = errors.New("go-vh: the OVH API is down, it does't respond to /time anymore")
)

Errors

Functions

This section is empty.

Types

type APIError

type APIError struct {
	// Error message.
	Message string
	// HTTP code.
	Code int
	// ID of the request
	QueryID string
}

APIError represents an error that can occurred while calling the API.

func (*APIError) Error

func (err *APIError) Error() string

type AccessRule

type AccessRule struct {
	// Allowed HTTP Method for the requested AccessRule.
	// Can be set to GET/POST/PUT/DELETE.
	Method string `json:"method"`
	// Allowed path.
	// Can be an exact string or a string with '*' char.
	// Example :
	// 		/me : only /me is authorized
	//		/* : all calls are authorized
	Path string `json:"path"`
}

AccessRule represents a method allowed for a path

type CkRequest

type CkRequest struct {
	AccessRules []AccessRule `json:"accessRules"`
	Redirection string       `json:"redirection,omitempty"`
	// contains filtered or unexported fields
}

CkRequest represents the parameters to fill in order to ask a new consumerKey.

func (*CkRequest) AddRecursiveRules

func (ck *CkRequest) AddRecursiveRules(methods []string, path string)

AddRecursiveRules adds grant requests on "path" and "path/*", for all methods "ReadOnly", "ReadWrite" and "ReadWriteSafe" should be used for "methods" unless specific access are required.

func (*CkRequest) AddRule

func (ck *CkRequest) AddRule(method, path string)

AddRule adds a new rule to the ckRequest

func (*CkRequest) AddRules

func (ck *CkRequest) AddRules(methods []string, path string)

AddRules adds grant requests on "path" for all methods. "ReadOnly", "ReadWrite" and "ReadWriteSafe" should be used for "methods" unless specific access are required.

func (*CkRequest) Do

func (ck *CkRequest) Do() (*CkValidationState, error)

Do executes the request. On success, set the consumer key in the client and return the URL the user needs to visit to validate the key

type CkValidationState

type CkValidationState struct {
	// Consumer key, which need to be validated by customer.
	ConsumerKey string `json:"consumerKey"`
	// Current status, should be always "pendingValidation".
	State string `json:"state"`
	// URL to redirect user in order to log in.
	ValidationURL string `json:"validationUrl"`
}

CkValidationState represents the response when asking a new consumerKey.

func (*CkValidationState) String

func (ck *CkValidationState) String() string

type Client

type Client struct {
	// Self generated tokens. Create one by visiting
	// https://eu.api.ovh.com/createApp/
	// AppKey holds the Application key
	AppKey string

	// AppSecret holds the Application secret key
	AppSecret string

	// ConsumerKey holds the user/app specific token. It must have been validated before use.
	ConsumerKey string

	// Client is the underlying HTTP client used to run the requests. It may be overloaded but a default one is instanciated in “NewClient“ by default.
	Client *http.Client

	// Logger is used to log HTTP requests and responses.
	Logger Logger

	Timeout time.Duration
	// contains filtered or unexported fields
}

Client represents a client to call the OVH API

func NewClient

func NewClient(endpoint, appKey, appSecret, consumerKey string) (*Client, error)

NewClient represents a new client to call the API

func NewDefaultClient

func NewDefaultClient() (*Client, error)

NewDefaultClient will load all it's parameter from environment or configuration files

func NewEndpointClient

func NewEndpointClient(endpoint string) (*Client, error)

NewEndpointClient will create an API client for specified endpoint and load all credentials from environment or configuration files

func (*Client) CallAPI

func (c *Client) CallAPI(method, path string, reqBody, resType interface{}, needAuth bool) error

CallAPI is the lowest level call helper. If needAuth is true, inject authentication headers and sign the request.

Request signature is a sha1 hash on following fields, joined by '+': - applicationSecret (from Client instance) - consumerKey (from Client instance) - capitalized method (from arguments) - full request url, including any query string argument - full serialized request body - server current time (takes time delta into account)

Call will automatically assemble the target url from the endpoint configured in the client instance and the path argument. If the reqBody argument is not nil, it will also serialize it as json and inject the required Content-Type header.

If everything went fine, unmarshall response into resType and return nil otherwise, return the error

func (*Client) CallAPIWithContext

func (c *Client) CallAPIWithContext(ctx context.Context, method, path string, reqBody, resType interface{}, needAuth bool) error

CallAPIWithContext is the lowest level call helper. If needAuth is true, inject authentication headers and sign the request.

Request signature is a sha1 hash on following fields, joined by '+': - applicationSecret (from Client instance) - consumerKey (from Client instance) - capitalized method (from arguments) - full request url, including any query string argument - full serialized request body - server current time (takes time delta into account)

Context is used by http.Client to handle context cancelation

Call will automatically assemble the target url from the endpoint configured in the client instance and the path argument. If the reqBody argument is not nil, it will also serialize it as json and inject the required Content-Type header.

If everything went fine, unmarshall response into resType and return nil otherwise, return the error

func (*Client) Delete

func (c *Client) Delete(url string, resType interface{}) error

Delete is a wrapper for the DELETE method

func (*Client) DeleteUnAuth

func (c *Client) DeleteUnAuth(url string, resType interface{}) error

DeleteUnAuth is a wrapper for the unauthenticated DELETE method

func (*Client) DeleteUnAuthWithContext

func (c *Client) DeleteUnAuthWithContext(ctx context.Context, url string, resType interface{}) error

DeleteUnAuthWithContext is a wrapper for the unauthenticated DELETE method

func (*Client) DeleteWithContext

func (c *Client) DeleteWithContext(ctx context.Context, url string, resType interface{}) error

DeleteWithContext is a wrapper for the DELETE method

func (*Client) Do

func (c *Client) Do(req *http.Request) (*http.Response, error)

Do sends an HTTP request and returns an HTTP response

func (*Client) Get

func (c *Client) Get(url string, resType interface{}) error

Get is a wrapper for the GET method

func (*Client) GetUnAuth

func (c *Client) GetUnAuth(url string, resType interface{}) error

GetUnAuth is a wrapper for the unauthenticated GET method

func (*Client) GetUnAuthWithContext

func (c *Client) GetUnAuthWithContext(ctx context.Context, url string, resType interface{}) error

GetUnAuthWithContext is a wrapper for the unauthenticated GET method

func (*Client) GetWithContext

func (c *Client) GetWithContext(ctx context.Context, url string, resType interface{}) error

GetWithContext is a wrapper for the GET method

func (*Client) NewCkRequest

func (c *Client) NewCkRequest() *CkRequest

NewCkRequest helps create a new ck request

func (*Client) NewCkRequestWithRedirection

func (c *Client) NewCkRequestWithRedirection(redirection string) *CkRequest

NewCkRequestWithRedirection helps create a new ck request with a redirect URL

func (*Client) NewRequest

func (c *Client) NewRequest(method, path string, reqBody interface{}, needAuth bool) (*http.Request, error)

NewRequest returns a new HTTP request

func (*Client) Ping

func (c *Client) Ping() error

Ping performs a ping to OVH API. In fact, ping is just a /auth/time call, in order to check if API is up.

func (*Client) Post

func (c *Client) Post(url string, reqBody, resType interface{}) error

Post is a wrapper for the POST method

func (*Client) PostUnAuth

func (c *Client) PostUnAuth(url string, reqBody, resType interface{}) error

PostUnAuth is a wrapper for the unauthenticated POST method

func (*Client) PostUnAuthWithContext

func (c *Client) PostUnAuthWithContext(ctx context.Context, url string, reqBody, resType interface{}) error

PostUnAuthWithContext is a wrapper for the unauthenticated POST method

func (*Client) PostWithContext

func (c *Client) PostWithContext(ctx context.Context, url string, reqBody, resType interface{}) error

PostWithContext is a wrapper for the POST method

func (*Client) Put

func (c *Client) Put(url string, reqBody, resType interface{}) error

Put is a wrapper for the PUT method

func (*Client) PutUnAuth

func (c *Client) PutUnAuth(url string, reqBody, resType interface{}) error

PutUnAuth is a wrapper for the unauthenticated PUT method

func (*Client) PutUnAuthWithContext

func (c *Client) PutUnAuthWithContext(ctx context.Context, url string, reqBody, resType interface{}) error

PutUnAuthWithContext is a wrapper for the unauthenticated PUT method

func (*Client) PutWithContext

func (c *Client) PutWithContext(ctx context.Context, url string, reqBody, resType interface{}) error

PutWithContext is a wrapper for the PUT method

func (*Client) Time

func (c *Client) Time() (*time.Time, error)

Time returns time from the OVH API, by asking GET /auth/time.

func (*Client) TimeDelta

func (c *Client) TimeDelta() (time.Duration, error)

TimeDelta represents the delay between the machine that runs the code and the OVH API. The delay shouldn't change, let's do it only once.

func (*Client) UnmarshalResponse

func (c *Client) UnmarshalResponse(response *http.Response, resType interface{}) error

UnmarshalResponse checks the response and unmarshals it into the response type if needed Helper function, called from CallAPI

type Logger

type Logger interface {
	// LogRequest logs an HTTP request.
	LogRequest(*http.Request)

	// LogResponse logs an HTTP response.
	LogResponse(*http.Response)
}

Logger is the interface that should be implemented for loggers that wish to log HTTP requests and HTTP responses.

Jump to

Keyboard shortcuts

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