gh_request: Send an HTTP request to the GitHub API

Description Usage Arguments Details Value Examples

View source: R/github-api.R

Description

This function can be used to make "GET", "POST", "PATCH", "PUT" or "DELETE" requests to the specified URL.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
gh_request(
  url,
  type,
  payload = NULL,
  headers = NULL,
  accept = "application/vnd.github.v3+json",
  token = getOption("github.token"),
  proxy = getOption("github.proxy"),
  ...
)

Arguments

url

(string) The address of the API endpoint.

type

(string) The type of HTTP request. Either "GET", "POST", "PATCH", "PUT" or "DELETE".

payload

(list, optional) The information to send to the API for "POST", "PATCH", "PUT" or "DELETE" requests. Default: NULL.

headers

(character, optional) Headers to add to the request. Default: NULL.

accept

(string, optional) The mime format to accept when making the call. Default: "application/vnd.github.v3+json".

token

(string or Token, optional) An authorisation token to include with the request. If NULL the OAuth process is triggered. Default: NULL.

proxy

(character, optional) The proxy server to use to connect to the github API. If NULL then no proxy is used. Can be set in the option github.proxy or the environment variable GITHUB_PROXY. Default: NULL.

...

Ignored.

Details

The response is parsed from either JSON or plain text, depending on the format received. When no response is received an empty list returned by the function. Details of the response are recorded as attributes.

For "POST", "PATCH", "PUT" and "DELETE" requests a payload can be supplied. It is parsed into a JSON format before being sent to the URL.

If an error is returned from the API then an error is thrown by this function, detailing the URL, the HTTP status code and a message from the API, if there is one.

Finally, an authorisation token can be supplied if it is required.

Value

A github list object consisting of the response, parsed into a list, with the attributes:

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
## Not run: 

  # Create a tag
  gh_request(
    url     = "https://api.github.com/repos/ChadGoymer/githapi/git/refs",
    type    = "POST",
    payload = list(
      ref = "test-tag",
      sha = "a4b6545671455234757313a42738e44c10b0ef37"
    )
  )

  # View a tag
  gh_request(
    url  = "https://api.github.com/repos/ChadGoymer/githapi/git/test-tag",
    type = "GET"
  )

  # Update a tag
  gh_request(
    url     = "https://api.github.com/repos/ChadGoymer/githapi/git/test-tag",
    type    = "PATCH",
    payload = list(sha = "a4b6545671455234757313a42738e44c10b0ef37")
  )

  # Delete a tag
  gh_request(
    url  = "https://api.github.com/repos/ChadGoymer/githapi/git/test-tag",
    type = "DELETE"
  )


## End(Not run)

ChadGoymer/githapi documentation built on Oct. 22, 2021, 10:56 a.m.