-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import requests | ||
import json | ||
import logging | ||
|
||
logging.basicConfig(filename='fetch_and_process_data.log', level=logging.ERROR) | ||
|
||
def download_json(url, file_path): | ||
try: | ||
response = requests.get(url) | ||
response.raise_for_status() # Raise an exception for HTTP errors | ||
with open(file_path, 'wb') as file: | ||
file.write(response.content) | ||
logging.info("JSON file downloaded successfully.") | ||
except requests.exceptions.RequestException as e: | ||
logging.error(f"Failed to download JSON file: {e}") | ||
|
||
class CredlyAPI: | ||
def __init__(self, base_url, authorization_token): | ||
self.base_url = base_url | ||
self.authorization_token = authorization_token | ||
self.headers = { | ||
'Accept': 'application/json', | ||
'Authorization': f'Bearer {authorization_token}', | ||
'Content-Type': 'application/json' | ||
} | ||
|
||
def make_request(self, method, endpoint, data=None): | ||
try: | ||
response = requests.request(method, f"{self.base_url}/{endpoint}", headers=self.headers, json=data) | ||
response.raise_for_status() # Raise an exception for HTTP errors | ||
return response.json() | ||
except requests.exceptions.RequestException as e: | ||
logging.error(f"Failed to make {method} request to {endpoint}: {e}") | ||
return None | ||
|
||
def get(self, endpoint): | ||
return self.make_request('GET', endpoint) | ||
|
||
def fetch_badges(self): | ||
# Fetch badges from Credly | ||
return self.get("badges") | ||
|
||
# Example usage: | ||
if __name__ == "__main__": | ||
base_url = "https://sandbox.credly.com/v1" # Sandbox environment | ||
authorization_token = "your_authorization_token_here" | ||
|
||
credly_api = CredlyAPI(base_url, authorization_token) | ||
badges_data = credly_api.fetch_badges() | ||
if badges_data: | ||
print("Badges Data:", badges_data) | ||
else: | ||
print("Failed to fetch badges data.") |