lwApi

package
v1.6.4 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

go-lwApi

LiquidWeb API Golang client

GoDoc

Setting up Authentication

When creating an api client, it expects to be configured via a configuration struct. Here is an example of how to get an api client.

package main

import (
	"fmt"

	lwApi "github.com/liquidweb/go-lwApi"
)

func main() {
	config := lwApi.LWAPIConfig{
		Username: "ExampleUsername",
		Password: "ExamplePassword",
		Url:      "api.liquidweb.com",
	}
	apiClient, iErr := lwApi.New(&config)
}

Importing

import (
        lwApi "github.com/liquidweb/go-lwApi"
)

Calling a method

apiClient, iErr := lwApi.New(&config)
if iErr != nil {
  panic(iErr)
}
args := map[string]interface{}{
  "uniq_id": "2UPHPL",
}
got, gotErr := apiClient.Call("bleed/asset/details", args)
if gotErr != nil {
  panic(gotErr)
}
fmt.Printf("RETURNED:\n\n%+v\n\n", got)

As you can see, you don't need to prefix the params key, as that is handled in the Call() function for you.

Documentation

Overview

Package lwApi is a minimalist API client to LiquidWeb's (https://www.liquidweb.com) API:

https://cart.liquidweb.com/storm/api/docs/v1

https://cart.liquidweb.com/storm/api/docs/bleed

As you might have guessed from the above API documentation links, there are API versions: "v1" and "bleed". As the name suggests, if you always want the latest features and abilities, use "bleed". If you want long term compatibility (at the cost of being a little further behind sometimes), use "v1".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Headers http.Header
	// contains filtered or unexported fields
}

A Client holds the packages *LWAPIConfig and *http.Client. To get a *Client, call New.

func New

func New(config *LWAPIConfig) (*Client, error)

New takes a *LWAPIConfig, and gives you a *Client. If there's an error, it is returned. When using this package, this should be the first function you call. Below is an example that demonstrates creating the config and passing it to New.

Example:

username := "ExampleUsername"
password := "ExamplePassword"

config := lwApi.LWAPIConfig{
	Username: &username,
	Password: &password,
	Url:      "api.liquidweb.com",
}
apiClient, newErr := lwApi.New(&config)
if newErr != nil {
	panic(newErr)
}

func (*Client) Call

func (client *Client) Call(method string, params interface{}) (interface{}, error)

Call takes a path, such as "network/zone/details" and a params structure. It is recommended that the params be a map[string]interface{}, but you can use anything that serializes to the right json structure. A `interface{}` and an error are returned, in typical go fasion.

Example:

args := map[string]interface{}{
	"uniq_id": "ABC123",
}
got, gotErr := apiClient.Call("bleed/asset/details", args)
if gotErr != nil {
	panic(gotErr)
}

func (*Client) CallInto

func (client *Client) CallInto(method string, params interface{}, into LWAPIRes) error

CallInto is like call, but instead of returning an interface you pass it a struct which is filled, much like the json.Unmarshal function. The struct you pass must satisfy the LWAPIRes interface. If you embed the LWAPIError struct from this package into your struct, this will be taken care of for you.

Example:

type ZoneDetails struct {
	lwApi.LWAPIError
	AvlZone     string   `json:"availability_zone"`
	Desc        string   `json:"description"`
	GatewayDevs []string `json:"gateway_devices"`
	HvType      string   `json:"hv_type"`
	ID          int      `json:"id"`
	Legacy      int      `json:"legacy"`
	Name        string   `json:"name"`
	Status      string   `json:"status"`
	SourceHVs   []string `json:"valid_source_hvs"`
}
var zone ZoneDetails
err = apiClient.CallInto("network/zone/details", paramers, &zone)
if err != nil {
	log.Fatal(err)
}
fmt.Printf("Got struct %#v\n", zone)

func (*Client) CallIntoInterface

func (client *Client) CallIntoInterface(method string, params interface{}, into interface{}) error

Similar to CallInto(), but populates an interface without needing to satisfy the LWAPIRes interface. Example:

type ZoneDetails struct {
        AvlZone     string   `json:"availability_zone"`
        Desc        string   `json:"description"`
        GatewayDevs []string `json:"gateway_devices"`
        HvType      string   `json:"hv_type"`
        ID          int      `json:"id"`
        Legacy      int      `json:"legacy"`
        Name        string   `json:"name"`
        Status      string   `json:"status"`
        SourceHVs   []string `json:"valid_source_hvs"`
}
var zone ZoneDetails
err = apiClient.CallIntoInterface("network/zone/details", params, &zone)

func (*Client) CallRaw

func (client *Client) CallRaw(method string, params interface{}) ([]byte, error)

CallRaw is just like Call, except it returns the raw json as a byte slice. However, in contrast to Call, CallRaw does *not* check the API response for LiquidWeb specific exceptions as defined in the type LWAPIError. As such, if calling this function directly, you must check for LiquidWeb specific exceptions yourself.

Example:

args := map[string]interface{}{
	"uniq_id": "ABC123",
}
got, gotErr := apiClient.CallRaw("bleed/asset/details", args)
if gotErr != nil {
	panic(gotErr)
}
// Check got now for LiquidWeb specific exceptions, as described above.

type LWAPIConfig

type LWAPIConfig struct {
	Username *string
	Password *string
	Token    *string
	Url      string
	Timeout  uint
	Insecure bool
}

A LWAPIConfig holds the configuration details used to call the API with the client.

type LWAPIError

type LWAPIError struct {
	ErrorMsg     string `json:"error,omitempty"`
	ErrorClass   string `json:"error_class,omitempty"`
	ErrorFullMsg string `json:"full_message,omitempty"`
}

A LWAPIError is used to identify error responses when JSON unmarshalling json from a byte slice.

func (LWAPIError) Error

func (e LWAPIError) Error() string

Given a LWAPIError, returns a string containing the ErrorClass and ErrorFullMsg.

func (LWAPIError) HadError

func (e LWAPIError) HadError() bool

Given a LWAPIError, returns boolean if ErrorClass was present or not. You can use this function to determine if a LWAPIRes response indicates an error or not.

type LWAPIRes

type LWAPIRes interface {
	Error() string
	HadError() bool
}

LWAPIRes is a convenient interface used (for example) by CallInto to ensure a passed struct knows how to indicate whether or not it had an error.

Jump to

Keyboard shortcuts

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