-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from ssghait007/examples
Add examples
- Loading branch information
Showing
5 changed files
with
121 additions
and
11 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
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,27 @@ | ||
import requests | ||
from trace_dkey import trace | ||
|
||
# GitHub API example | ||
def github_api_example(): | ||
# Get information about a GitHub repository | ||
repo = "Agent-Hellboy/trace-dkey" | ||
url = f"https://api.github.com/repos/{repo}" | ||
response = requests.get(url) | ||
data = response.json() | ||
|
||
# Find the path to the 'stargazers_count' key | ||
paths = trace(data, 'stargazers_count') | ||
print("Paths to 'stargazers_count':") | ||
for path in paths: | ||
print(" -> ".join(path)) | ||
print(f"Value: {data['stargazers_count']}") | ||
|
||
# Find the path to the 'login' key (owner's login) | ||
paths = trace(data, 'login') | ||
print("\nPaths to 'login':") | ||
for path in paths: | ||
print(" -> ".join(path)) | ||
print(f"Value: {data['owner']['login']}") | ||
|
||
if __name__ == "__main__": | ||
github_api_example() |
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,31 @@ | ||
import requests | ||
from trace_dkey import trace | ||
|
||
# JSONPlaceholder API example | ||
def jsonplaceholder_api_example(): | ||
# Get a sample post | ||
url = "https://jsonplaceholder.typicode.com/posts/1" | ||
response = requests.get(url) | ||
post_data = response.json() | ||
|
||
# Find the path to the 'title' key | ||
paths = trace(post_data, 'title') | ||
print("Paths to 'title':") | ||
for path in paths: | ||
print(" -> ".join(path)) | ||
print(f"Value: {post_data['title']}") | ||
|
||
# Get comments for the post | ||
url = "https://jsonplaceholder.typicode.com/posts/1/comments" | ||
response = requests.get(url) | ||
comments_data = response.json() | ||
|
||
# Find the path to the 'email' key in the first comment | ||
paths = trace(comments_data[0], 'email') | ||
print("\nPaths to 'email' in the first comment:") | ||
for path in paths: | ||
print(" -> ".join(path)) | ||
print(f"Value: {comments_data[0]['email']}") | ||
|
||
if __name__ == "__main__": | ||
jsonplaceholder_api_example() |
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,29 @@ | ||
import requests | ||
import os | ||
from trace_dkey import trace | ||
|
||
# OpenWeatherMap API example | ||
def openweathermap_api_example(): | ||
# You need to sign up for a free API key at https://openweathermap.org/ | ||
api_key = os.environ.get('OPENWEATHERMAP_API_KEY', 'your_api_key_here') | ||
city = "London" | ||
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}" | ||
response = requests.get(url) | ||
data = response.json() | ||
|
||
# Find the path to the 'temp' key (current temperature) | ||
paths = trace(data, 'temp') | ||
print("Paths to 'temp':") | ||
for path in paths: | ||
print(" -> ".join(path)) | ||
print(f"Value: {data['main']['temp']} K") | ||
|
||
# Find the path to the 'description' key (weather description) | ||
paths = trace(data, 'description') | ||
print("\nPaths to 'description':") | ||
for path in paths: | ||
print(" -> ".join(path)) | ||
print(f"Value: {data['weather'][0]['description']}") | ||
|
||
if __name__ == "__main__": | ||
openweathermap_api_example() |
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,2 @@ | ||
requests==2.26.0 | ||
trace-dkey==0.0.4 |