Skip to content

Commit d804679

Browse files
committed
add endpoint connection for feedback
1 parent c3ddf26 commit d804679

File tree

12 files changed

+111
-38
lines changed

12 files changed

+111
-38
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Ignore Python bytecode files
2+
__pycache__/
3+
*.pyc
4+
5+
# Ignore macOS system files
6+
.DS_Store
7+
8+
# Ignore distribution files
9+
dist/
10+
11+
# Ignore environment variable files
12+
.env
-2.71 KB
Binary file not shown.
-3.66 KB
Binary file not shown.

examples/credits_example.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
This script demonstrates how to use the credits function from the credits module
3+
to retrieve credits from the ScrapeGraph AI API.
4+
"""
5+
6+
import os
7+
from dotenv import load_dotenv
8+
from scrapegraphaiapisdk.credits import credits
9+
10+
# Load environment variables from a .env file
11+
load_dotenv()
12+
13+
def main():
14+
api_key = os.getenv("SCRAPEGRAPH_API_KEY")
15+
16+
response = credits(api_key)
17+
print("Response from the API:")
18+
print(response)
19+
20+
if __name__ == "__main__":
21+
main()

examples/status_example.py renamed to examples/feedback_example.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
from dotenv import load_dotenv
3-
from scrapegraphaiapisdk.status import status
3+
from scrapegraphaiapisdk.credits import status
4+
from scrapegraphaiapisdk.feedback import feedback
45

56
# Load environment variables from .env file
67
load_dotenv()
@@ -16,5 +17,10 @@ def main():
1617
except Exception as e:
1718
print(f"Error occurred: {e}")
1819

20+
# Example usage of feedback function
21+
feedback_message = "This is a test feedback message."
22+
feedback_response = feedback(api_key, feedback_message) # Call the feedback function
23+
print(f"Feedback Response: {feedback_response}") # Print the response
24+
1925
if __name__ == "__main__":
2026
main()
-175 Bytes
Binary file not shown.
-2.06 KB
Binary file not shown.

scrapegraphaiapisdk/credits.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
This module provides functionality to interact with the ScrapeGraph AI API.
3+
4+
It includes functions to retrieve credits and send feedback, handling responses and errors appropriately.
5+
"""
6+
7+
import requests
8+
import json
9+
10+
def credits(api_key: str) -> str:
11+
"""Retrieve credits from the API.
12+
13+
Args:
14+
api_key (str): Your ScrapeGraph AI API key.
15+
16+
Returns:
17+
str: Response from the API in JSON format.
18+
"""
19+
endpoint = "https://sgai-api.onrender.com/api/v1/credits"
20+
headers = {
21+
"accept": "application/json",
22+
"SGAI-API-KEY": api_key
23+
}
24+
25+
try:
26+
response = requests.get(endpoint, headers=headers)
27+
response.raise_for_status()
28+
except requests.exceptions.HTTPError as http_err:
29+
return json.dumps({"error": "HTTP error occurred", "message": str(http_err), "status_code": response.status_code})
30+
except requests.exceptions.RequestException as e:
31+
return json.dumps({"error": "An error occurred", "message": str(e)})
32+
33+
return response.text

scrapegraphaiapisdk/feedback.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
This module provides functionality to send feedback to the ScrapeGraph AI API.
3+
4+
It includes a function to send feedback messages along with the necessary API key
5+
and handles responses and errors appropriately.
6+
"""
7+
8+
import requests
9+
import json
10+
11+
def feedback(api_key: str, feedback: str) -> str:
12+
"""Send feedback to the API.
13+
14+
Args:
15+
api_key (str): Your ScrapeGraph AI API key.
16+
feedback (str): The feedback message to send.
17+
18+
Returns:
19+
str: Response from the API in JSON format.
20+
"""
21+
endpoint = "https://sgai-api.onrender.com/api/v1/feedback"
22+
headers = {
23+
"accept": "application/json",
24+
"SGAI-API-KEY": api_key,
25+
"Content-Type": "application/json"
26+
}
27+
28+
feedback_data = {"feedback": feedback} # Prepare the feedback data
29+
30+
try:
31+
response = requests.post(endpoint, headers=headers, json=feedback_data)
32+
response.raise_for_status()
33+
except requests.exceptions.HTTPError as http_err:
34+
return json.dumps({"error": "HTTP error occurred", "message": str(http_err), "status_code": response.status_code})
35+
except requests.exceptions.RequestException as e:
36+
return json.dumps({"error": "An error occurred", "message": str(e)})
37+
38+
return response.text

scrapegraphaiapisdk/scrape.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
from pydantic import BaseModel
22
import requests
3-
import argparse
43
from typing import Optional
54
import json
65

7-
class ExampleSchema(BaseModel):
8-
"""Define an example schema for the output structure, if needed."""
9-
name: str
10-
description: str
11-
126
def scrape(api_key: str, url: str, prompt: str, schema: Optional[BaseModel] = None) -> str:
137
"""Scrape and extract structured data from a webpage using ScrapeGraph AI.
148

0 commit comments

Comments
 (0)