Giter VIP home page Giter VIP logo

github-api's Introduction

Github API bindings for Pharo

This package implements Github's REST API v3. You can find details on it in here:

https://developer.github.com/

Installation

Metacello new
	repository: 'github://guillep/github-api:v0.1';
	baseline: 'GithubAPI';
	load.

Setting up a connection and login

A new connection to github can be set up by doing:

connection := Github new.
  • Anonymous authentication (default if none specified)
connection anonymous.
  • User/password authentication
connection loginUser: 'YOUR_USER' password: 'YOUR_PASSWORD'.
  • Token authentication
connection loginToken: 'YOUR_TOKEN'.

Making a request

The protocol requests contains several already implemented requests such as:

  • get the pull requests of a given project
github getPullRequestsFromRepo: 'pharo' owner: 'pharo-project'. 
  • get the issues of a given project
response := github getIssuesFromRepo: 'pharo' owner: 'pharo-project'.
  • get a user
response := github getUser: 'guillep'
  • get the respositories of a suser
response := github getRepositoriesFromUser: 'guillep'.
  • get the organizations of a user
response := github getOrganizationsFromUser: 'guillep'.

Besides already implemented requests, you may use the #call: method to call other requests. The #call: method receives a request object, such as a GHGetRequest, and returns a GHResponse object. Using it in combination with the GHGenericGetRequest class, you can do:

response := github call: (GHGenericGetRequest url: 'https://api.github.com/users')

Notice that if something fails on the request and a failed response is obtained, the #call: method will fail with a GHRequestError exception. The GHRequestError contains the response that originated the error.

Accessing the response contents

Requests answer a GHResponse object. A GHResponse object contains inside the HTTP response plus some handy methods to parse the data and manage pagination.

To access a response's contents as string, you may do:

stringContents := response rawContents

And to access them as an already parsed JSON object:

json := response parseJSON

Pagination

By default all github requests are paginated with a max page number. For example, at the time of writing this comment, such max number was 30. To help accessing such paginated data, Github provides some meta-data links in response headers to the next, previous, last and first pages, if available. This is specified in the following url:

https://developer.github.com/v3/#pagination

A GHResponse provides access to the links in the headers.

links := response links.

Each link understands the messages #rel and #url to access the link's content. Moreover, a GHResponse provides a much higher-level API, avoiding the need of diving in the links. The folling methods provide access to the link data:

  • #hasLast: specifies if a response has a last link
  • #hasNext: specifies if a response has a next link
  • #nextLink: returns the next link
  • #nextUrl: returns the url of the next link

Moreover, the #next method makes a request to the next url link, using the same github connection as in the first request.

nextPageResponse :=  response next.

Pagination through iteration

As a second alternative, this package provides a request iterator. An iterator provides a high level API to access, select and collect data obtained from a paginated request, hiding the details of the page iteration behind.

 | iterator prs |	
	iterator := self github iteratorOn: (GHGetPullRequests new
		repositoryName: 'pharo';
		ownerName: 'pharo-project';
		state: 'closed';
		sort: 'updated';
		direction: 'desc';
		yourself).
	
	prs := iterator
		collect: [ :rq | rq parseJSON collect: [ :pr | GHPullRequest on: pr ] ];
		select: [ :pr | pr isMerged and: [ pr mergeDate between: to asZTimestamp and: from asZTimestamp ] ];
		until: [ :pr | pr updateDate < to asZTimestamp ];
		iterate.

github-api's People

Contributors

ducasse avatar guillep avatar stephaneggermont avatar tesonep avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

github-api's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.