client

package
v0.0.0-...-c4bda31 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2012 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package "client" implements a generic github api client.

It features Basic and OAuth2 authentication methods and abstract pagination on github api results.

A simple usage example of a bare github client:

        ghc, _ := ghclient.NewGithubClient("username", "password",
		                                   ghclient.AUTH_USER_PASSWORD)
       	req, _ := ghc.NewAPIRequest("GET", "user", nil)
        res, _ := ghc.RunRequest(req, new(http.Client))

        fmt.Println("JSON: %v\n", res.Json())

An example of paginated results:

req, _ := ghc.NewAPIRequest("GET", "gists/public", nil)
res, _ := ghc.RunRequest(req, new(http.Client))
fmt.Println("PAGE 1: %v\n", res.Json())
res, _ := res.GetNextPage();
fmt.Println("PAGE 2: %v\n", res.Json())

Index

Constants

View Source
const (
	AUTH_OAUTH2_TOKEN = iota
	AUTH_USER_PASSWORD
)

Variables

View Source
var (
	ErrInvalidAuthMethod = errors.New("Invalid Github Auth Method")
	ErrNoNextPageUrl     = errors.New("No next page url")
	ErrNoPrevPageUrl     = errors.New("No prev page url")
	ErrNoFirstPageUrl    = errors.New("No first page url")
	ErrNoLastPageUrl     = errors.New("No last page url")
)

Functions

This section is empty.

Types

type GithubClient

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

func NewGithubClient

func NewGithubClient(login string, tokenOrPassword string, authMode ghAuthModes) (ghc *GithubClient, err error)

func (*GithubClient) GetResourceFromUrl

func (ghc *GithubClient) GetResourceFromUrl(fullResourceUrl string) (res *GithubResult, err error)

TBD

func (*GithubClient) NewAPIRequest

func (ghc *GithubClient) NewAPIRequest(method, url string, body io.Reader) (req *http.Request, err error)

func (*GithubClient) RunRequest

func (ghc *GithubClient) RunRequest(req *http.Request, httpc *http.Client) (res *GithubResult, err error)

type GithubResult

type GithubResult struct {
	RawHttpResponse *http.Response

	RateLimitLimit     string
	RateLimitRemaining string
	// contains filtered or unexported fields
}

GithubResult abstracts a generic reply from the Github REST API, parse it internally and adds some helper (e.g. json data, paginated results, rate limits)

GithubResult objects are returned by GithubClient.RunRequest method.

func (*GithubResult) GetFirstPage

func (r *GithubResult) GetFirstPage() (res *GithubResult, err error)

request the first page and return a new GithubResult or error (ErrNoFirstPageUrl)

func (*GithubResult) GetLastPage

func (r *GithubResult) GetLastPage() (res *GithubResult, err error)

request the last page and return a new GithubResult or error (ErrNoLastPageUrl)

func (*GithubResult) GetNextPage

func (r *GithubResult) GetNextPage() (res *GithubResult, err error)

request the next page and return a new GithubResult or error (ErrNoNextPageUrl)

func (*GithubResult) GetPrevPage

func (r *GithubResult) GetPrevPage() (res *GithubResult, err error)

request the previous page and return a new GithubResult or error (ErrNoPrevPageUrl)

func (*GithubResult) HasFirstPage

func (r *GithubResult) HasFirstPage() (ok bool)

returns true if the result has a first page link

func (*GithubResult) HasLastPage

func (r *GithubResult) HasLastPage() (ok bool)

returns true if the result has a last page link

func (*GithubResult) HasNextPage

func (r *GithubResult) HasNextPage() (ok bool)

returns true if the result has a next page

func (*GithubResult) HasPrevPage

func (r *GithubResult) HasPrevPage() (ok bool)

returns true if the result has a previous page

func (*GithubResult) IsPaginated

func (r *GithubResult) IsPaginated() (ok bool)

returns true if the result is paginated

func (*GithubResult) IsSuccess

func (r *GithubResult) IsSuccess() bool

tests if server response status is 200

func (*GithubResult) Json

func (r *GithubResult) Json() (j interface{}, err error)

Lazily parse body as json and return unmarshalled results.

func (*GithubResult) JsonArray

func (r *GithubResult) JsonArray() (j JsonArray, err error)

TBD

func (*GithubResult) JsonMap

func (r *GithubResult) JsonMap() (j JsonMap, err error)

TBD

type JsonArray

type JsonArray []interface{}

func (JsonArray) GetArray

func (j JsonArray) GetArray(idx int) JsonArray

func (JsonArray) GetBool

func (j JsonArray) GetBool(idx int) bool

func (JsonArray) GetFloat

func (j JsonArray) GetFloat(idx int) float64

func (JsonArray) GetInt

func (j JsonArray) GetInt(idx int) int

func (JsonArray) GetObject

func (j JsonArray) GetObject(idx int) JsonMap

func (JsonArray) GetString

func (j JsonArray) GetString(idx int) string

type JsonMap

type JsonMap map[string]interface{}

JsonData contains some helpers to help developers accessing a generic parsed json.

As an example:

rawData := "{ \"attr1\": \"string1\", \"attr2\": 2, \"attr3\": 5.34, \"attr4\": { \"name\": \"obj1\" } }"
var jd JsonData = make(JsonData)

_ = json.Unmarshal(([]byte)(rawData), &jd)
jd.GetString("attr1")
jd.GetInt("attr2")
jd.GetFloat("attr3")
jd.GetObject("attr4").GetString("name")

NOTE: JsonData.Get* Function can raise an error due to incompatible interface conversions.

func (JsonMap) GetArray

func (j JsonMap) GetArray(attr string) JsonArray

func (JsonMap) GetBool

func (j JsonMap) GetBool(attr string) bool

func (JsonMap) GetFloat

func (j JsonMap) GetFloat(attr string) float64

func (JsonMap) GetInt

func (j JsonMap) GetInt(attr string) int

func (JsonMap) GetMap

func (j JsonMap) GetMap(attr string) JsonMap

func (JsonMap) GetString

func (j JsonMap) GetString(attr string) string

Jump to

Keyboard shortcuts

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