pebbleclient

package module
v0.0.0-...-cf00bc1 Latest Latest
Warning

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

Go to latest
Published: May 31, 2017 License: MIT Imports: 18 Imported by: 1

README

Pebble client

Go client library for interacting with Pebble-style apps.

Usage

Creating client

import (
  pc "github.com/t11e/go-pebbleclient"
)

func main() {
  builder, err := pc.NewHTTPClientBuilder(pc.Options{
    ServiceName: "central",
    ApiVersion: 1,
    Session: "uio3uio43uio4oui432",
    Host: "localhost",
  })
  if err != nil {
    log.Fatal(err)
  }
  client, err := builder.NewClient(pc.ClientOptions{})
  if err != nil {
    log.Fatal(err)
  }
  // ...
}

Or from request:

func myHandler(w http.ResponseWriter, req *http.Request) {
  client, err := builder.NewClient(pc.ClientOptions{}).FromHTTPRequest(req)
  // ...
}

GET requests

var result *Organization
err := client.Get("/posts/post.listing:endeavor.enclosure.missoulian$1178983", &pc.RequestOptions{
  Params: pc.Params{"raw": true},
}, &result)

HEAD requests

err := client.Head("/posts/post.listing:endeavor.enclosure.missoulian$1178983", nil)

POST requests

b, err := json.Marshal(organization)
var result *Organization
err := client.Post("/organizations", nil, bytes.NewReader(b)}, &result)

PUT requests

b, err := json.Marshal(organization)
var result *Organization
err := client.Put("/organizations/1", nil, bytes.NewReader(b)}, &result)

DELETE requests

err := client.Delete("/organizations/1", nil, nil, nil)

Contributions

Clone this repository into your GOPATH ($GOPATH/src/github.com/t11e/) and use Glide to install its dependencies.

brew install glide
go get github.com/t11e/go-pebbleclient
cd "$GOPATH"/src/github.com/t11e/go-pebbleclient
glide install --strip-vendor

You can then run the tests:

go test $(go list ./... | grep -v /vendor/)

There is no need to use go install as any project that requires this library can include it as a dependency like so:

cd my_other_project
glide get --strip-vendor github.com/t11e/go-pebbleclient

If you change any of the interfaces that have a mock in mocks/ directory be sure to execute go generate and check in the updated mock files.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func URIEscape

func URIEscape(path string) string

Types

type Client

type Client interface {
	// GetOptions returns a copy of the current options.
	GetOptions() Options

	// WithOptions returns a new client that has new default client options. Only
	// non-zero values in the options argument will override the client's options.
	WithOptions(opts Options) Client

	// Get performs a GET request and provides the decoded return value in
	// the result argument, unless nil.
	Get(path string, opts *RequestOptions, result interface{}) error

	// Head performs a HEAD request. Its only use is really to check that
	// the resource does not return an error.
	Head(path string, opts *RequestOptions) error

	// Delete performs a DELETE request and provides the decoded return
	// value in the result argument, unless nil.
	Delete(path string, opts *RequestOptions, result interface{}) error

	// Post performs a POST request. The body can be nil if no body is to be
	// sent. Provides the decoded return value in the result argument, unless
	// nil.
	Post(path string, opts *RequestOptions, body io.Reader, result interface{}) error

	// Put performs a PUT request. The body can be nil if no body is to be
	// sent. Provides the decoded return value in the result argument, unless
	// nil.
	Put(path string, opts *RequestOptions, body io.Reader, result interface{}) error

	// Do performs an HTTP request.
	Do(path string, opts *RequestOptions, method string, body io.Reader,
		result interface{}) error
}

type Connector

type Connector struct {
	// contains filtered or unexported fields
}

func NewConnectorFromConfig

func NewConnectorFromConfig(config RealmsConfig) (*Connector, error)

func (*Connector) Connect

func (connector *Connector) Connect(services ...Service) error

Connect finds one or more services. The input arguments must be pointers to variables which have the same interface types as those registered with Register().

func (*Connector) Register

func (connector *Connector) Register(intf interface{}, fn ServiceFactoryFunc)

Register registers a new service. The type must be a nil interface pointer; e.g. (*MyInterface)(nil). The function is a factory function that takes a client, and must return a new service instance.

func (*Connector) WithRealm

func (connector *Connector) WithRealm(name string) (*Connector, error)

WithRealm returns a new connector which inherits settings from a realm.

func (*Connector) WithRequest

func (connector *Connector) WithRequest(req *http.Request) (*Connector, error)

WithRequest returns a new connector which inherits settings from a request. See HTTPClient.FromHTTPRequest for more information.

type HTTPClient

type HTTPClient struct {
	// contains filtered or unexported fields
}

HTTPClient is a client for the Central API.

func NewHTTPClient

func NewHTTPClient(opts Options) (*HTTPClient, error)

NewHTTPClient constructs a new client.

func (*HTTPClient) Delete

func (client *HTTPClient) Delete(path string, opts *RequestOptions, result interface{}) error

func (*HTTPClient) Do

func (client *HTTPClient) Do(
	path string,
	opts *RequestOptions,
	method string,
	body io.Reader,
	result interface{}) error

func (*HTTPClient) FromHTTPRequest

func (client *HTTPClient) FromHTTPRequest(req *http.Request) (*HTTPClient, error)

FromHTTPRequest constructs a new client that inherits the host name, protocol, session and request ID from an HTTP request. Any options specified will override inferred from the request.

func (*HTTPClient) Get

func (client *HTTPClient) Get(path string, opts *RequestOptions, result interface{}) error

func (*HTTPClient) GetOptions

func (client *HTTPClient) GetOptions() Options

func (*HTTPClient) Head

func (client *HTTPClient) Head(path string, opts *RequestOptions) error

func (*HTTPClient) Post

func (client *HTTPClient) Post(path string, opts *RequestOptions, body io.Reader, result interface{}) error

func (*HTTPClient) Put

func (client *HTTPClient) Put(path string, opts *RequestOptions, body io.Reader, result interface{}) error

func (*HTTPClient) WithOptions

func (client *HTTPClient) WithOptions(opts Options) Client

type MissingParameter

type MissingParameter struct {
	Key string
}

func (*MissingParameter) Error

func (err *MissingParameter) Error() string

type NoHostConfigError

type NoHostConfigError struct {
	Host string
}

func (*NoHostConfigError) Error

func (err *NoHostConfigError) Error() string

type NoRealmConfigError

type NoRealmConfigError struct {
	Realm string
}

func (*NoRealmConfigError) Error

func (err *NoRealmConfigError) Error() string

type Options

type Options struct {
	// ServiceName of target application.
	ServiceName string

	// APIVersion of target application. Defaults to 1.
	APIVersion int

	// Host is the host name, optionally including the port, to connect to.
	Host string

	// Protocol is the HTTP protocol. Defaults to "http".
	Protocol string

	// HTTPClient is an optional HTTP client instance, which will be used
	// instead of the default.
	HTTPClient *http.Client

	// Session is an optional Checkpoint session key.
	Session string

	// RequestID is an optional request ID that can be passed on to the service.
	RequestID string

	// Logger is an optional interface to permit instrumentation. Ignored if
	// passing in a custom HTTP client.
	Logger httplogger.HTTPLogger

	// Ctx is an optional context.
	Ctx context.Context
}

Options contains options for the client.

type Params

type Params map[string]interface{}

Params is a map of query parameters.

func (Params) ToValues

func (p Params) ToValues() url.Values

ToValues is a convenience function to generate url.Values from params. Conversion to string is done like so:

- If value is nil, use empty string.

- If value is a string, use that.

- If value implements fmt.Stringer, use that.

- Otherwise, use fmt.Sprintf("%v", v).

type RealmConfig

type RealmConfig struct {
	// Host for this realm.
	Host string `json:"host" yaml:"host"`

	// Aliases valid for this realm.
	Aliases []string `json:"aliases" yaml:"aliases"`

	// Session key.
	Session string `json:"session" yaml:"session"`
}

func (*RealmConfig) ClientOptions

func (config *RealmConfig) ClientOptions() Options

type RealmsConfig

type RealmsConfig map[string]*RealmConfig

func (RealmsConfig) FindByHost

func (c RealmsConfig) FindByHost(host string) *RealmConfig

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

func NewRegistry

func NewRegistry() *Registry

func (*Registry) GetFactoryFunc

func (registry *Registry) GetFactoryFunc(ptr interface{}) (ServiceFactoryFunc, error)

func (*Registry) Register

func (registry *Registry) Register(intf reflect.Type, fn ServiceFactoryFunc)

type RequestError

type RequestError struct {
	Options     *RequestOptions
	Req         *http.Request
	Resp        *http.Response
	PartialBody []byte
	// contains filtered or unexported fields
}

func (*RequestError) Error

func (err *RequestError) Error() string

type RequestOptions

type RequestOptions struct {
	// Params is an optional map of query parameters.
	Params Params
}

RequestOptions is a set of options that can be applied to a request.

type Service

type Service interface{}

type ServiceFactoryFunc

type ServiceFactoryFunc func(client Client) (Service, error)

type UID

type UID string

func (UID) Class

func (uid UID) Class() (string, error)

func (UID) NUID

func (uid UID) NUID() (int, error)

func (UID) Path

func (uid UID) Path() (string, error)

func (UID) Realm

func (uid UID) Realm() (string, error)

Realm returns the realm part of the path.

func (UID) String

func (uid UID) String() string

String implements Stringer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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