Skip to main content

An advanced REST client for the GitHub API

Project description

github3api

GitHub Workflow Status coverage complexity vulnerabilities PyPI version python

An advanced REST client for the GitHub API. It is a subclass of rest3client tailored for the GitHub API with special optional directives for GET requests that can return all pages from an endpoint or return a generator that can be iterated over (for paged requests). By default all requests will be retried if ratelimit request limit is reached.

Support for executing Graphql queries including paging; Graphql queries are also retried if Graphql rate limiting occurs.

Installation

pip install github3api

Example Usage

>>> from github3api import GitHubAPI

GitHubAPI instantiation

# instantiate using no-auth
>>> client = GitHubAPI()

# instantiate using a token
>>> client = GitHubAPI(bearer_token='****************')

GET request

# GET request - return JSON response
>>> client.get('/rate_limit')['resources']['core']
{'limit': 60, 'remaining': 37, 'reset': 1588898701}

# GET request - return raw resonse
>>> client.get('/rate_limit', raw_response=True)
<Response [200]>

POST request

>>> client.post('/user/repos', json={'name': 'test-repo1'})['full_name']
'soda480/test-repo1'

>>> client.post('/repos/soda480/test-repo1/labels', json={'name': 'label1'})['url']
'https://api.github.com/repos/soda480/test-repo1/labels/label1'

PATCH request

>>> client.patch('/repos/soda480/test-repo1/labels/label1', json={'description': 'my label'})['url']
'https://api.github.com/repos/soda480/test-repo1/labels/label1'

DELETE request

>>> client.delete('/repos/soda480/test-repo1')

GET all directive - Get all pages from an endpoint and return list containing only matching attributes

for repo in client.get('/orgs/edgexfoundry/repos', _get='all', _attributes=['full_name']):
    print(repo['full_name'])

GET page directive - Yield a page from endpoint

for page in client.get('/user/repos', _get='page'):
    for repo in page:
        print(repo['full_name'])

total - Get total number of resources at given endpoint

print(client.total('/user/repos'))

graphql - execute graphql query

query = """
  query($query:String!, $page_size:Int!) {
    search(query: $query, type: REPOSITORY, first: $page_size) {
      repositoryCount
      edges {
        node {
          ... on Repository {
            nameWithOwner
          }
        }
      }
    }
  }
"""
variables = {"query": "org:edgexfoundry", "page_size":100}
client.graphql(query, variables)

graphql paging - execute paged graphql query

query = """
  query ($query: String!, $page_size: Int!, $cursor: String!) {
    search(query: $query, type: REPOSITORY, first: $page_size, after: $cursor) {
      repositoryCount
      pageInfo {
        endCursor
        hasNextPage
      }
      edges {
        cursor
        node {
          ... on Repository {
            nameWithOwner
          }
        }
      }
    }
  }
"""
variables = {"query": "org:edgexfoundry", "page_size":100}
for page in client.graphql(query, variables, page=True, keys='data.search'):
    for repo in page:
        print(repo['node']['nameWithOwner'])

For Graphql paged queries:

  • the query should include the necessary pageInfo and cursor attributes
  • the keys method argument is a dot annotated string that is used to access the resulting dictionary response object
  • the query is retried every 60 seconds (for up to an hour) if a ratelimit occur

Projects using github3api

Development

Ensure the latest version of Docker is installed on your development server. Fork and clone the repository.

Build the Docker image:

docker image build \
--target build-image \
--build-arg http_proxy \
--build-arg https_proxy \
-t \
github3api:latest .

Run the Docker container:

docker container run \
--rm \
-it \
-e http_proxy \
-e https_proxy \
-v $PWD:/code \
github3api:latest \
bash

Execute the build:

pyb -X

NOTE: commands above assume working behind a proxy, if not then the proxy arguments to both the docker build and run commands can be removed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

github3api-0.3.2.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

github3api-0.3.2-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file github3api-0.3.2.tar.gz.

File metadata

  • Download URL: github3api-0.3.2.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for github3api-0.3.2.tar.gz
Algorithm Hash digest
SHA256 c3be609629cc28f0a19fe40102c5900b13407a7e63005356c5e586a838fabbc7
MD5 600e36de0b15394bf66493a999c9e0ba
BLAKE2b-256 7a535bc7f3f520552199237915dcf073d2972cf29928a3516a07b4d4ccb74abf

See more details on using hashes here.

File details

Details for the file github3api-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: github3api-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for github3api-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ea4d522c9d4d34a80ad757820dcb84ab1bdbc72384ec44b2c8c56d6a9243cfd5
MD5 72e297948d3d8d647b5a7c726f64c0bc
BLAKE2b-256 fd42f2b36fc75331485f19b07286a81a4beda8fd48428411a60f6061ca42472f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page