response

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2023 License: MIT Imports: 0 Imported by: 1

Documentation

Overview

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

Custom response types should be declared for specific API endpoints.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Responder

type Responder interface {
	Respond() Responder
}

Responder is used to define objects that can be unmarshalled into by (*voki.Voki).CallResponder.

type Response

type Response struct {
	StatusCode int
	Message    string
}

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

func (Response) Respond

func (r Response) Respond() Responder

Respond implements Responder interface for Response

type ResponseWithError added in v1.2.0

type ResponseWithError struct {
	Response
	Error string
}

Jump to

Keyboard shortcuts

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