GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — python-level-2/Examples/example_12_graphql_api.py at main · ariannedee/python-level-2

Expand file tree

/

example_12_graphql_api.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
56 lines (48 loc) · 1.61 KB
/

example_12_graphql_api.py

Copy path

File metadata and controls

56 lines (48 loc) · 1.61 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Get repository data from Github's GraphQL API.
V4 GraphQL API documentation: https://developer.github.com/v4/
To generate an authentication token for your user, follow: https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line
"""
import requests
import sys
API_URL = "https://api.github.com/graphql"
headers = {
'Content-Type': 'application/json',
'Authorization': 'token {Your token here}', # See https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line
}
# Specify the data you want from the API, test at https://developer.github.com/v4/explorer
query = """
query {
viewer {
repositories(first:100) {
totalCount
nodes {
nameWithOwner
}
}
}
}
"""
# Send the query in the right format for GraphQL
data = {"query": query}
response = requests.post(API_URL, headers=headers, json=data)
# Handle bad requests (e.g. not authenticated)
if response.status_code != 200:
print(response.status_code)
print(response.text)
sys.exit()
# Turn the response string into JSON data. Data is now a list of dictionaries representing repositories
response = response.json()
# Handle bad queries (e.g. improperly formatted query string)
if 'errors' in response:
print(response['errors'])
sys.exit()
data = response['data']
print(data)
# Now do what you want with the data
repos = data['viewer']['repositories']['nodes']
if len(repos) > 0:
# I want to list each repository name
print("\nRepository names")
for repo in repos:
print(' ' + repo['nameWithOwner'])
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.