This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom query and generic user query
- Loading branch information
Showing
3 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
setup( | ||
name="pygitapi", | ||
install_requires=[ | ||
'requests' | ||
], | ||
extras_require={ | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .dummy import dummy as hello_world | ||
from .request import GitStats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
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') |