14Users
8.18
top
← prev up next →

github-apiπŸ”— i

eu90h

github-api is a wrapper for easily making requests to the GitHub api.

While this document contains usage examples, the functional tests found in the functional_tests directory of the github repository provide a good source of usage examples and patterns.

1Authentication & InitializationπŸ”— i

Before you begin making requests to the GitHub api, you must create an identity.

struct

(struct github-identity (typedata)
#:extra-constructor-namemake-github-identity)
type:symbol?
data:list?
This struct holds your GitHub identity.

type must be one of the following symbols: 'password'personal-token'oauth

'password authentication simply uses your GitHub username and password.

'personal-token authentication allows you to send your GitHub username and a personal access token (created on your GitHub settings page.)

'oauth uses an OAuth token for authorization.

For more information, see the github api documentation

The data field is a list whose contents are determined by user authentication method.

For 'password or 'personal-token types, your data will be of the form (list usernamepassword-or-token), where both username & password-or-token are strings.

For 'oauth, the data will simply be (list oauth-token), where oauth-token is a string.

procedure

( github-api id
[ #:endpointendpoint
#:user-agentuser-agent])github-api-req/c
endpoint:string? ="api.github.com"
user-agent:string? ="racket-github-api-@eu90h"
Once you’ve created an identity, apply it to this procedure to receive a function for making api requests.

The optional #:endpoint keyword sets the root endpoint for making api requests. If you have a GitHub enterprise account, you may wish to change the endpoint. See this for more information on root-endpoints.

If you change the user-agent string, be aware that GitHub has certain rules explicated here

struct

(struct github-response (codedata)
#:extra-constructor-namemake-github-response)
code:number?
data:jsexpr?
This is the data structure returned as the result of making a GitHub API request. The code field holds the HTTP status code and data holds the content of the response.

syntax

( github-api-req/c (->string?
[#:methodstring?
#:datastring?
#:media-typestring?]
github-response?))
This is a contract for the procedures returned by the function github-api . These functions are called with an api request and return a github-response struct.

Typically, one would not use this procedure directly but rather pass it along to another function.

The #:method keyword specifies what HTTP verb to use (I.e. "GET", "POST", "PATCH", etc.)

The #:data keyword specifies any information to send along with the request. This is almost always a JSON string.

Finally, #:media-type specifies the format in which you wish to receive data. Practically every github-* procedure has an optional keyword #:media-type that allows you to specify a media-type for a request.

For more information on media types see the GitHub api documentation.

2A Note on Identity SecurityπŸ”— i

According to the GitHub documentation, personal access tokens are equivalent to your password. Never give it out (and don’t accidently commit your identity!)

Read more about your options for authentication here

3ExampleπŸ”— i

(define personal-token"fs52knf535djbfk2je43b2436")
(define username"alice")
(define id(github-identity 'personal-token(list usernamepersonal-token)))
(define github(github-api id))
(github"/users/plt/repos")
Here we make a request to get the repositories created by the user plt.

4Working with JSON DataπŸ”— i

When making requests to the GitHub API, it is common to receive data encoded in the JSON format. This quick section introduces Racket’s JSON handling facilities.

Racket provides a library for working with JSON data, aptly called json.

This is used by the Racket github-api library to encode data before returning it.

Essentially, JSON expressions are represented as hashes. The JSON object

{"name":"billy bob"}

becomes the hash

(define jsexpr(make-hash (list (cons 'name"billy bob"))))

To get the value associated with the key ’name, use hash-ref like so:

(hash-ref jsexpr'name)

which should return "billy bob"

To learn more about working with hashes see the Racket guide and the Racket reference on hash-tables.

5Gist APIπŸ”— i

The Gist API will return up to 1 megabyte of content from a requested gist.

To see if a file was truncated, check whether or not the key truncated is "true".

To get the full contents of the file, make a request to the url referenced by the key raw_url.

For more information on truncation see the GitHub documentation

For additional information on the Gist API, check here

procedure

( github-create-gist api-req
files
[ #:descriptiondescription
#:publicpublic
#:media-typemedia-type])api-resonse/c
files:(listofpair? )
description:string? =""
public:boolean? =#f
media-type:string? ="application/vnd.github.v3+json"
Creates a gist containing files given as a list of pairs (filenamecontents). If the gist was created successfully, a jsexpr? is returned.

The optional keyword description provides a description of the gist. By default it is empty.

The optional keyword public determines whether or not the gist is public. By default this is #f.

procedure

( github-edit-gist api-req
gist-id
files
[ #:descriptiondescription
#:media-typemedia-type])api-resonse/c
gist-id:string?
files:(listofpair? )
description:string? =""
media-type:string? ="application/vnd.github.v3+json"
Updates a gist. See github-create-gist for more explanation of the arguments.

To delete a file from a gist, for example "file1.txt", add an entry to the files list like so: (cons "file1.txt"'delete).

procedure

( github-get-gist api-req
gist-id
[ #:media-typemedia-type])github-response?
gist-id:string?
media-type:string? ="application/vnd.github.v3+json"
Gets the gist, returning a jsexpr? on success.

procedure

gist-id
[ #:media-typemedia-type])
gist-id:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

( github-star-gist api-req
gist-id
[ #:media-typemedia-type])github-response?
gist-id:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

( github-unstar-gist api-req
gist-id
[ #:media-typemedia-type])github-response?
gist-id:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

gist-id
[ #:media-typemedia-type])boolean?
gist-id:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

( github-fork-gist api-req
gist-id
[ #:media-typemedia-type])github-response?
gist-id:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

gist-id
[ #:media-typemedia-type])
gist-id:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

( github-delete-gist api-req
gist-id
[ #:media-typemedia-type])github-response?
gist-id:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

gist-id
sha
[ #:media-typemedia-type])
gist-id:string?
sha:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

user
[ #:media-typemedia-type])
user:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

[ #:media-typemedia-type])
media-type:string? ="application/vnd.github.v3+json"

procedure

[ #:media-typemedia-type])
media-type:string? ="application/vnd.github.v3+json"

procedure

[ #:media-typemedia-type])
media-type:string? ="application/vnd.github.v3+json"

6Gist ExamplesπŸ”— i

(define new-gist-id
(let ([response(github-create-gist github-req
(list (cons "file1.txt""blah blah blah")
(cons "file2.txt""yadda yadda yadda")))])
(hash-ref (github-response-data response)'id)))
(github-edit-gist github-reqnew-gist-id
(list (cons "file2.txt"'delete)))
(github-star-gist github-reqnew-gist-id)
(github-gist-starred? github-reqnew-gist-id)
(github-unstar-gist github-reqnew-gist-id)
(github-gist-starred? github-reqnew-gist-id)
(github-fork-gist github-reqnew-gist-id)
(github-list-gist-forks github-reqnew-gist-id)
(github-get-user-gists github-requsername)

7EventsπŸ”— i

For more information on the Events API, see the GitHub documentation

procedure

( github-list-events api-req
repo-owner
repo
[ #:media-typemedia-type])github-response?
repo-owner:string?
repo:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

repo-owner
repo
[ #:media-typemedia-type])
repo-owner:string?
repo:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

org
[ #:media-typemedia-type])
org:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

user
[ #:media-typemedia-type])
user:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

( github-list-user-received-public-events
api-req
user
[ #:media-typemedia-type])
user:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

user
[ #:media-typemedia-type])
user:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

user
[ #:media-typemedia-type])
user:string?
media-type:string? ="application/vnd.github.v3+json"

8FeedsπŸ”— i

For more information about feeds, go here

procedure

( github-list-feeds api-req
[ #:media-typemedia-type])github-response?
media-type:string? ="application/vnd.github.v3+json"

procedure

[ #:media-typemedia-type])
media-type:string? ="application/vnd.github.v3+json"

9IssuesπŸ”— i

For more information about the Issues API, click here

Furthermore, the Issues API uses custom media types. See this section

procedure

[ #:media-typemedia-type])
media-type:string? ="application/vnd.github.v3+json"

procedure

[ #:media-typemedia-type])
media-type:string? ="application/vnd.github.v3+json"

procedure

organization
[ #:media-typemedia-type])
organization:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

( github-list-issues api-req
repo-owner
repo-name
[ #:media-typemedia-type])github-response?
repo-owner:string?
repo-name:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

repo-owner
repo-name
title
[ #:bodybody
#:assigneeassignee
#:milestonemilestone
#:labelslabel
#:media-typemedia-type])
repo-owner:string?
repo-name:string?
title:string?
body:string? =""
assignee:string? =""
milestone:string? =""
label:(listofstring? )=null
media-type:string? ="application/vnd.github.v3+json"

procedure

( github-edit-issue api-req
repo-owner
repo-name
[ #:titletitle
#:bodybody
#:assigneeassignee
#:milestonemilestone
#:labelslabel
#:media-typemedia-type])github-response?
repo-owner:string?
repo-name:string?
title:string? =""
body:string? =""
assignee:string? =""
milestone:string? =""
label:(listofstring? )=null
media-type:string? ="application/vnd.github.v3+json"

procedure

( github-get-issue api-req
repo-owner
repo-name
issue-number
[ #:media-typemedia-type])github-response?
repo-owner:string?
repo-name:string?
issue-number:(or/cnumber? string? )
media-type:string? ="application/vnd.github.v3+json"

procedure

repo-owner
repo-name
issue-number
[ #:media-typemedia-type])
repo-owner:string?
repo-name:string?
issue-number:(or/cnumber? string? )
media-type:string? ="application/vnd.github.v3+json"

procedure

repo-owner
repo-name
[ #:media-typemedia-type])
repo-owner:string?
repo-name:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

( github-get-comment api-req
repo-owner
repo-name
comment-id
[ #:media-typemedia-type])github-response?
repo-owner:string?
repo-name:string?
comment-id:(or/cnumber? string? )
media-type:string? ="application/vnd.github.v3+json"

procedure

repo-owner
repo-name
issue-number
comment-body
[ #:media-typemedia-type])
repo-owner:string?
repo-name:string?
issue-number:(or/cnumber? string? )
comment-body:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

repo-owner
repo-name
comment-id
comment-body
[ #:media-typemedia-type])
repo-owner:string?
repo-name:string?
comment-id:(or/cnumber? string? )
comment-body:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

repo-owner
repo-name
comment-id
[ #:media-typemedia-type])
repo-owner:string?
repo-name:string?
comment-id:(or/cnumber? string? )
media-type:string? ="application/vnd.github.v3+json"

10Issue ExamplesπŸ”— i

(github-create-issue github-req
"eu90h"
"racket-github-api"
"testing-issues-api"
#:body"this is a test of the issues api"
#:assignee"eu90h"
#:labels(list "woo!""test"))

11RepositoriesπŸ”— i

procedure

repo-owner
repo-name
[ #:media-typemedia-type])
repo-owner:string?
repo-name:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

repo-owner
repo-name
user
[ #:media-typemedia-type])
repo-owner:string?
repo-name:string?
user:string?
media-type:string? ="application/vnd.github.v3+json"

12Git DataπŸ”— i

Click here for more information on the Git Data API.

procedure

( github-get-blob api-req
repo-owner
repo-name
sha
[ #:media-typemedia-type])github-response?
repo-owner:string?
repo-name:string?
sha:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

( github-create-blob api-req
repo-owner
repo-name
content
[ encoding
#:media-typemedia-type])github-response?
repo-owner:string?
repo-name:string?
content:string?
encoding:string? ="utf-8"
media-type:string? ="application/vnd.github.v3+json"

procedure

( github-get-commit api-req
repo-owner
repo-name
sha
[ #:media-typemedia-type])github-response?
repo-owner:string?
repo-name:string?
sha:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

repo-owner
repo-name
message
tree
parents
[ #:media-typemedia-type])
repo-owner:string?
repo-name:string?
message:string?
tree:string?
parents:(listofstring? )
media-type:string? ="application/vnd.github.v3+json"

13OrganizationsπŸ”— i

For more on Organizations, go "https://developer.github.com/v3/orgs/""here"

procedure

( github-list-orgs api-req
[ #:media-typemedia-type])github-response?
media-type:string? ="application/vnd.github.v3+json"

procedure

[ #:media-typemedia-type])
media-type:string? ="application/vnd.github.v3+json"

procedure

user
[ #:media-typemedia-type])
user:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

( github-get-org api-req
org
[ #:media-typemedia-type])github-response?
org:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

org
[ #:media-typemedia-type])
org:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

repo-owner
repo
[ #:media-typemedia-type])
repo-owner:string?
repo:string?
media-type:string? ="application/vnd.github.v3+json"

14UsersπŸ”— i

procedure

( github-get-user api-req
user
[ #:media-typemedia-type])github-response?
user:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

[ #:media-typemedia-type])
media-type:string? ="application/vnd.github.v3+json"

procedure

[ #:media-typemedia-type])
media-type:string? ="application/vnd.github.v3+json"

15Webhooks & Service HooksπŸ”— i

Webhooks are a sort-of user defined callback in the form of a listening webserver that github sends a message to whenever a certain type of event occurs.

A service hook is a webhook whose type is anything except "web"

To read more, see the GitHub documentation

procedure

url
[ #:content-typecontent-type
#:secretsecret
#:insecure-sslinsecure-ssl])
url:string?
content-type:string? ="form"
secret:string? =""
insecure-ssl:string? ="0"

procedure

( github-hook-repo api-req
repo-owner
repo
type
config
[ #:eventsevents
#:activeactive])github-response?
repo-owner:string?
repo:string?
type:string?
config:jsexpr?
events:(listofstring? )='("push")
active:boolean? =#t
The type parameter must be the string "web" or a service name defined in this rather inconvenient JSON file.

Passing any other string results in an error response from the GitHub API.

Note: The type parameter is referred to in the GitHub documentation (misleadingly, I think) as the name of the webhook.

procedure

( github-get-hooks api-req
repo-owner
repo
[ #:media-typemedia-type])github-response?
repo-owner:string?
repo:string?
media-type:string? ="application/vnd.github.v3+json"

procedure

( github-get-hook api-req
repo-owner
repo
hook-id
[ #:media-typemedia-type])github-response?
repo-owner:string?
repo:string?
hook-id:(or/cstring? number? )
media-type:string? ="application/vnd.github.v3+json"

procedure

repo-owner
repo
hook-id)github-response?
repo-owner:string?
repo:string?
hook-id:(or/cstring? number? )

procedure

( github-ping-hook api-req
repo-owner
repo
hook-id)github-response?
repo-owner:string?
repo:string?
hook-id:(or/cstring? number? )

procedure

( github-delete-hook api-req
repo-owner
repo
hook-id)github-response?
repo-owner:string?
repo:string?
hook-id:(or/cstring? number? )

16Webhooks ExampleπŸ”— i

(define config(github-build-webhook-config "http://example.com"
#:insecure-ssl"0"
#:content-type"json"))
(define hook(github-hook-repo github-requsernamemy-repo"web"config))
(define hook-data(github-response-data hook))
(define del-hook(thunk(github-delete-hook github-requsernamemy-repo(hash-ref hook-data'id))))
...
(define delete-response(del-hook))
(if (= 204(github-response-code delete-response))
(displayln "successfully removed webhook")
(displayln "trouble removing webhook"))
top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /