├── README.md ├── add_ssh_key.py ├── do_get_account.py ├── do_ssh_keys.py └── github_list_repos.py /README.md: -------------------------------------------------------------------------------- 1 | # How To Use Web APIs in Python 3 2 | 3 | This is the completed code for the article [How To Use Web APIs in Python 3](https://www.digitalocean.com/community/tutorials/how-to-use-web-apis-in-python-3) by Brian King. 4 | 5 | 6 | -------------------------------------------------------------------------------- /add_ssh_key.py: -------------------------------------------------------------------------------- 1 | import json 2 | import requests 3 | 4 | api_token = 'your_token_goes_here' 5 | api_url_base = 'https://api.digitalocean.com/v2/' 6 | headers = {'Content-Type': 'application/json', 7 | 'Authorization': 'Bearer {0}'.format(api_token)} 8 | 9 | 10 | def add_ssh_key(name, filename): 11 | 12 | api_url = "{0}account/keys".format(api_url_base) 13 | 14 | with open(filename, 'r') as f: 15 | ssh_key = f.readline() 16 | 17 | ssh_key = {'name': name, 'public_key': ssh_key} 18 | 19 | response = requests.post(api_url, headers=headers, json=ssh_key) 20 | 21 | if response.status_code > 499: 22 | print("[!] [{0}] Server Error".format(response.status_code)) 23 | return None 24 | elif response.status_code == 404: 25 | print("[!] [{0}] URL not found: [{1}]".format(response.status_code, api_url)) 26 | return None 27 | elif response.status_code == 401: 28 | print("[!] [{0}] Authentication Failed".format(response.status_code)) 29 | return None 30 | elif response.status_code > 399: 31 | print("[!] [{0}] Bad Request".format(response.status_code)) 32 | print(ssh_key) 33 | print(response.content) 34 | return None 35 | elif response.status_code > 299: 36 | print("[!] [{0}] Unexpected redirect.".format(response.status_code)) 37 | return None 38 | elif response.status_code == 201: 39 | added_key = json.loads(response.content) 40 | return added_key 41 | else: 42 | print("[?] Unexpected Error: [HTTP {0}]: Content: {1}".format(response.status_code, response.content)) 43 | return None 44 | 45 | add_response = add_ssh_key('tutorial_key', '/Users/brianhogan/.ssh/sammy.pub') 46 | 47 | if add_response is not None: 48 | print("Your key was added: ") 49 | for k, v in add_response.items(): 50 | print(' {0}:{1}'.format(k, v)) 51 | else: 52 | print('[!] Request Failed') 53 | -------------------------------------------------------------------------------- /do_get_account.py: -------------------------------------------------------------------------------- 1 | import json 2 | import requests 3 | 4 | api_token = 'your_token_goes_here' 5 | api_url_base = 'https://api.digitalocean.com/v2/' 6 | 7 | headers = {'Content-Type': 'application/json', 8 | 'Authorization': 'Bearer {0}'.format(api_token)} 9 | 10 | 11 | def get_account_info(): 12 | 13 | api_url = '{0}account'.format(api_url_base) 14 | 15 | response = requests.get(api_url, headers=headers) 16 | 17 | if response.status_code == 200: 18 | return json.loads(response.content.decode('utf-8')) 19 | else: 20 | return None 21 | 22 | account_info = get_account_info() 23 | 24 | if account_info is not None: 25 | print("Here's your info: ") 26 | for k, v in account_info['account'].items(): 27 | print('{0}:{1}'.format(k, v)) 28 | 29 | else: 30 | print('[!] Request Failed') 31 | -------------------------------------------------------------------------------- /do_ssh_keys.py: -------------------------------------------------------------------------------- 1 | import json 2 | import requests 3 | 4 | api_token = 'your_token_goes_here' 5 | api_url_base = 'https://api.digitalocean.com/v2/' 6 | headers = {'Content-Type': 'application/json', 7 | 'Authorization': 'Bearer {0}'.format(api_token)} 8 | 9 | 10 | def get_ssh_keys(): 11 | 12 | api_url = "{0}account/keys".format(api_url_base) 13 | 14 | response = requests.get(api_url, headers=headers) 15 | 16 | if response.status_code >= 500: 17 | print("[!] [{0}] Server Error".format(response.status_code)) 18 | return None 19 | elif response.status_code == 404: 20 | print("[!] [{0}] URL not found: [{1}]".format(response.status_code, api_url)) 21 | return None 22 | elif response.status_code == 401: 23 | print("[!] [{0}] Authentication Failed".format(response.status_code)) 24 | return None 25 | elif response.status_code == 400: 26 | print("[!] [{0}] Bad Request".format(response.status_code)) 27 | return None 28 | elif response.status_code >= 300: 29 | print("[!] [{0}] Unexpected Redirect".format(response.status_code)) 30 | return None 31 | elif response.status_code == 200: 32 | ssh_keys = json.loads(response.content.decode('utf-8')) 33 | return ssh_keys 34 | else: 35 | print("[?] Unexpected Error: [HTTP {0}]: Content: {1}".format(response.status_code, response.content)) 36 | return None 37 | 38 | ssh_keys = get_ssh_keys() 39 | 40 | if ssh_keys is not None: 41 | print("Here are your keys: ") 42 | for key, details in enumerate(ssh_keys['ssh_keys']): 43 | print("Key {}:".format(key)) 44 | for k, v in details.items(): 45 | print(' {0}:{1}'.format(k, v)) 46 | else: 47 | print('[!] Request Failed') 48 | -------------------------------------------------------------------------------- /github_list_repos.py: -------------------------------------------------------------------------------- 1 | import json 2 | import requests 3 | 4 | api_url_base = 'https://api.github.com/' 5 | headers = {'Content-Type': 'application/json', 6 | 'User-Agent': 'Python Student', 7 | 'Accept': 'application/vnd.github.v3+json'} 8 | 9 | 10 | def get_repos(username): 11 | 12 | api_url = '{}orgs/{}/repos'.format(api_url_base, username) 13 | 14 | response = requests.get(api_url, headers=headers) 15 | 16 | if response.status_code == 200: 17 | repositories = json.loads(response.content.decode('utf-8')) 18 | return (repositories) 19 | else: 20 | print('[!] HTTP {0} calling [{1}]'.format(response.status_code, api_url)) 21 | return None 22 | 23 | repo_list = get_repos('octokit') 24 | 25 | if repo_list is not None: 26 | print(repo_list) 27 | else: 28 | print('No Repo List Found') 29 | --------------------------------------------------------------------------------