service

package module
v0.0.0-...-05dcbe7 Latest Latest
Warning

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

Go to latest
Published: May 9, 2017 License: MIT Imports: 6 Imported by: 46

README

Go Service

Build Status Coverage Status Go Report Card GoDoc

HTTP service gateway in go. A simple library to send requests to external services without marshaling JSON yourself.

Installation

go get github.com/ghmeier/go-service

Examples

Note All fields of the receiveing data type must be exported.

Sending one GET request:

package example

import "github.com/ghmeier/go-service"

type Foo struct {
    name string `json:"name"`
}

func Get() {
    s := service.New()

    var f Foo
    err := s.Send(&service.Request{
        Method: "GET",
        URL:    "http://some.url.com/foo",
    }, &f)
}

The default response will fill f with the object in the data field of the response and return an error if Response.success is false.

Sending one POST request:

package example

import "github.com/ghmeier/go-service"

func Post() {
    s := service.New()

    f := &Foo{}
    err := s.Send(&service.Request{
        Method: "POST",
        URL:    "http://some.url.com/foo",
        Data:   f,
    }, nil)
}

Check out examples.go for an example of implementing a service gateway using go-service.

Custom Response

If you want to handle responses from services with different repsponse types, implement the service.Responder and service.Response interfaces.

Example Custom Responder/Response:
type CustomResponder struct{}

//Marshal should return a new response based on an http response. Usually this Unmarshals the body.
func (c *CustomResponder) Marshal(r *http.Response) (service.Response, error) {
    res := &CustomResponse{}

    res.OK = r.StatusCode == 200
    res.Status = r.Status

    return res
}

type CustomResponse struct {
    Status string `json:"status"`
    OK     bool   `json:"ok"`
}

//Error should return an error based on the CustomResponse state
func (c *CustomResponse) Error() error {
    if !c.OK {
        return fmt.Errorf(c.Status)
    }

    return nil
}

//Body should return a []byte that can be marshalled into the second argument of Service.Send()
func (c *CustomResponse) Body() ([]byte, error) {
    return json.Marshal(c)
}

Using Custom Responder:

s := service.NewCustom(&CustomResponder{})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Request

type Request struct {
	Method  string
	Params  map[string]string
	Headers map[string]string
	Data    interface{}
}

Request contains the Method used in sending, the Url to request, and

any Data to be sent

type Responder

type Responder interface {
	Marshal(*h.Response) (Response, error)
}

Responder marshals an http.Response into a service.Response that will

return either a Body() or Error()

type Response

type Response interface {
	Error() error
	Body() ([]byte, error)
}

Response contains relevant data from http server responses It can be used in conjunction with the following methods like this:

data, _ := ServiceGet(url) var c []*models.Content err := json.Unmarshal(data, &c)

Since ServiceGet returns data as a []byte, we can unmarshal it to whatever is needed in the calling method. Here, its []*models.Content

type Service

type Service interface {
	Send(*Request, interface{}) error
	Copy(...string) Service
}

Service has a method that will send a Request and put the response into

the provided interface using json unmarshalling. The interface must be a
pointer type

func New

func New(url string) Service

New returns a Service with the default http Client based at the given url

func NewCustom

func NewCustom(url string, r Responder) Service

NewCustom returns a BaseService with a custom responder

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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