Documentation ¶
Overview ¶
Package voki provides a strongly-typed JSON HTTP API request-response framework
It is lightweight and built directly on top of the Go standard library
Index ¶
- type Completer
- type Requester
- type Responder
- type Response
- type ResponseWithError
- type Voki
- func (v *Voki) Call(method, route string, requestBody io.Reader, useKey bool) (*http.Response, error)
- func (v *Voki) CloseIdleConnections()
- func (v *Voki) Complete() bool
- func (v *Voki) Unmarshal(method, route string, requestBody io.Reader, useKey bool, destination any) error
- func (v *Voki) UnmarshalIfComplete(method, route string, requestBody io.Reader, useKey bool, destination any) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Completer ¶
type Completer interface {
Complete() bool
}
Completer is a single-method interface to allow structs to declare themselves as `Complete` by returning an boolean.
It is used by `Voki.UnmarshalIfComplete` by checking some or all of their fields and returning `True` if they consider themselves ready to be sent to an API server, or `False` if not.
type Responder ¶
type Responder interface {
Respond() Responder
}
Responder is used to define objects that can be unmarshalled into by (*voki.Voki).CallResponder.
type Response ¶
Response declares fields used by most of my API servers.
They will probably not apply to your environment, if so declare structs that implement the Responder interface, so that you can use them with (*voki.Voki).CallResponder.
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 // MyResponse must match some or all of the fields returned by the API // endpoint type MyResponse struct { response.Response Data struct { MyString string } } var myResponse MyResponse // Replace "/v1/myresource" by the API endpoint you are trying to reach // relative to the connection informations defined in voki.New(...). // // Note that in this case, calling v.CallResponder and v.Unmarshal // accomplishes the same goal, but only because MyResponse implements // response.Responder. if err := v.CallResponder(http.MethodGet, "/v1/myresource", nil, false, &myResponse); err != nil { log.Fatal(err) } fmt.Println(myResponse.StatusCode) // Output: 200
type ResponseWithError ¶
type Voki ¶
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 (*Voki) Call ¶
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) CloseIdleConnections ¶
func (v *Voki) CloseIdleConnections()
CloseIdleConnections calls the eponymous method on (*Voki).Client
func (*Voki) Complete ¶
Complete checks if the different fields of a `Voki` instance are ready for http requests
func (*Voki) Unmarshal ¶
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>