voki

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: MIT Imports: 5 Imported by: 1

README

voki

vlbeaudoin/voki

JSON API caller and response unmarshaller distributed under the MIT license

Resources

Logo attributions

The voki logo was made using caro-asercion/rotary-phone which is licensed under CC BY 3.0.

Documentation

Overview

Package voki provides a JSON API client with reuseable logic to make requests against a JSON API server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Complete added in v1.5.0

type Complete interface {
	Complete() bool
}

type Voki

type Voki struct {
	Client   *http.Client
	Host     string
	Key      string
	Port     int
	Protocol string
}

Voki is the base of the project, and contains a reuseable *http.Client for API requests, along with the connection information for the API server to be queried.

It also implements methods to make calls against that API server with (*Voki).Call, as well as unmarshalling the result into a struct with (*Voki).Unmarshal.

It is recommended to use New() to instanciate a pointer to a new Voki client.

func New

func New(client *http.Client, host, key string, port int, protocol string) *Voki

func (*Voki) Call added in v0.4.0

func (v *Voki) Call(method, route string, requestBody io.Reader, useKey bool) (*http.Response, error)

(*Voki).Call returns a *http.Response and optionally an error, after requesting an API resource.

This is used by (*Voki).Unmarshal to make the actual request, and that method should generally be used over (*Voki).Call.

However, should you need access to the underlying *http.Response, you may use this method yourself instead.

func (*Voki) CallResponder deprecated added in v0.5.0

func (v *Voki) CallResponder(method, route string, requestBody io.Reader, useKey bool, responder response.Responder) error

Deprecated: Please switch to (*Voki).Unmarshal. This method will be removed in a future version.

Makes Voki.Call() returning only an error. The Call data is put into the responder parameter's reference

Example:

// Replace ... in parentheses by your environment's values
v := voki.New(...)

type MyResponse struct {
  responses.Response
  Data struct {
      MyString string
  }
}

var myResponse MyResponse

// Replace "/v1/myresource" by the API endpoint you are trying to reach
if err := v.CallResponder(http.MethodGet, "/v1/myresource", nil, false, &myResponse); err != nil {
  log.Fatal(err)
}

fmt.Println(myResponse.StatusCode) // Output: 200

func (*Voki) CloseIdleConnections added in v1.4.0

func (v *Voki) CloseIdleConnections()

CloseIdleConnections calls the eponymous method on (*Voki).Client

func (*Voki) Complete added in v1.5.0

func (v *Voki) Complete() bool

func (*Voki) Unmarshal added in v1.1.0

func (v *Voki) Unmarshal(method, route string, requestBody io.Reader, useKey bool, destination any) error

Unmarshal makes a (*Voki).Call() returning only an error. The Call data is put into the `destination` parameter.

Example:

// Instantiate a new *http.Client
client := http.DefaultClient
defer client.CloseIdleConnections()

// Instantiate a new *voki.Voki.
v := voki.New(client, ...) // Replace ... with your API server details

// MyStruct must match some or all of the fields returned by the API
// endpoint
type MyStruct struct {
    MyInt    int
    MyString string
}

var destinationStruct MyStruct

// Replace "/v1/myresource" by the API endpoint you are trying to reach
// relative to the connection informations defined in voki.New(...)
if err := v.Unmarshal(http.MethodGet, "/v1/myresource", nil, false, &destinationStruct); err != nil {
  log.Fatal(err)
}

fmt.Println(MyStruct) // Output: <content of destinationStruct>

func (*Voki) UnmarshalIfComplete added in v1.5.0

func (v *Voki) UnmarshalIfComplete(method, route string, requestBody io.Reader, useKey bool, destination any) error

Directories

Path Synopsis
Package response provides the Responder interface for (*voki.Voki).CallResponder
Package response provides the Responder interface for (*voki.Voki).CallResponder

Jump to

Keyboard shortcuts

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