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
- Variables
- type GithubClient
- func (ghc *GithubClient) GetResourceFromUrl(fullResourceUrl string) (res *GithubResult, err error)
- func (ghc *GithubClient) NewAPIRequest(method, url string, body io.Reader) (req *http.Request, err error)
- func (ghc *GithubClient) RunRequest(req *http.Request, httpc *http.Client) (res *GithubResult, err error)
- type GithubResult
- func (r *GithubResult) GetFirstPage() (res *GithubResult, err error)
- func (r *GithubResult) GetLastPage() (res *GithubResult, err error)
- func (r *GithubResult) GetNextPage() (res *GithubResult, err error)
- func (r *GithubResult) GetPrevPage() (res *GithubResult, err error)
- func (r *GithubResult) HasFirstPage() (ok bool)
- func (r *GithubResult) HasLastPage() (ok bool)
- func (r *GithubResult) HasNextPage() (ok bool)
- func (r *GithubResult) HasPrevPage() (ok bool)
- func (r *GithubResult) IsPaginated() (ok bool)
- func (r *GithubResult) IsSuccess() bool
- func (r *GithubResult) Json() (j interface{}, err error)
- func (r *GithubResult) JsonArray() (j JsonArray, err error)
- func (r *GithubResult) JsonMap() (j JsonMap, err error)
- type JsonArray
- type JsonMap
Constants ¶
const ( AUTH_OAUTH2_TOKEN = iota AUTH_USER_PASSWORD )
Variables ¶
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 (*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.
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.