Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Add custom query and generic user query
Browse files Browse the repository at this point in the history
  • Loading branch information
DillonB07 committed Jan 4, 2022
1 parent c5f5141 commit 0adfd2d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -4,6 +4,7 @@
setup(
name="pygitapi",
install_requires=[
'requests'
],
extras_require={
},
Expand Down
2 changes: 1 addition & 1 deletion src/__init__.py
@@ -1 +1 @@
from .dummy import dummy as hello_world
from .request import GitStats
98 changes: 98 additions & 0 deletions src/request.py
@@ -0,0 +1,98 @@
import requests
import json

URL = 'https://api.github.com/graphql'


class ArgumentError(Exception):
pass


class QueryFailError(Exception):
pass


class GitStats:
def __init__(self, git_token: str):
self.token = git_token
self.headers = {"Authorization": "Bearer " + self.token}

def get_query(self, query):

request = requests.post(
URL, json={'query': query}, headers=self.headers)
if request.status_code == 200:
return json.dumps(request.json(), indent=4, sort_keys=True)
else:
raise QueryFailError("Query failed to run by returning code of {}. {}".format(
request.status_code, query))

def custom_query(self, query):
"""Allows usage of custom GraphQL queries for the GitHub GrapQL API"""
return self.get_query(query)

def user_info(self, user: str):
"""Returns basic information about given user"""
query = '''
query user {
user(login: "''' + user + '''") {
url
login
name
twitterUsername
websiteUrl
email
company
location
status {
message
indicatesLimitedAvailability
}
avatarUrl
bio
isDeveloperProgramMember
isCampusExpert
starredRepositories {
totalCount
}
followers {
totalCount
}
following {
totalCount
}
issues {
totalCount
}
pullRequests {
totalCount
}
itemShowcase {
hasPinnedItems
items(first: 10) {
nodes {
... on Repository {
name
}
}
}
}
organizations(first: 10) {
totalCount
nodes {
name
}
}
repositories {
totalCount
}
}
}
'''
print(query)
response = self.get_query(query)
print(response)


stats = GitStats('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')
stats.user_info('DillonB07')

0 comments on commit 0adfd2d

Please sign in to comment.