This commit is contained in:
Zamil Majdy
2024-11-06 11:41:42 +07:00
parent 4908f4633d
commit 31675c9fb7
11 changed files with 21 additions and 67 deletions

View File

@@ -217,7 +217,6 @@ class AIShortformVideoCreatorBlock(Block):
url = "https://webhook.site/token"
headers = {"Accept": "application/json", "Content-Type": "application/json"}
response = requests.post(url, headers=headers)
response.raise_for_status()
webhook_data = response.json()
return webhook_data["uuid"], f"https://webhook.site/{webhook_data['uuid']}"
@@ -228,14 +227,12 @@ class AIShortformVideoCreatorBlock(Block):
logger.debug(
f"API Response Status Code: {response.status_code}, Content: {response.text}"
)
response.raise_for_status()
return response.json()
def check_video_status(self, api_key: SecretStr, pid: str) -> dict:
url = f"https://www.revid.ai/api/public/v2/status?pid={pid}"
headers = {"key": api_key.get_secret_value()}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
def wait_for_video(

View File

@@ -355,8 +355,7 @@ class GithubAddLabelBlock(Block):
api = get_api(credentials)
data = {"labels": [label]}
labels_url = issue_url + "/labels"
response = api.post(labels_url, json=data)
response.raise_for_status()
api.post(labels_url, json=data)
return "Label added successfully"
def run(
@@ -415,8 +414,7 @@ class GithubRemoveLabelBlock(Block):
def remove_label(credentials: GithubCredentials, issue_url: str, label: str) -> str:
api = get_api(credentials)
label_url = issue_url + f"/labels/{label}"
response = api.delete(label_url)
response.raise_for_status()
api.delete(label_url)
return "Label removed successfully"
def run(
@@ -482,8 +480,7 @@ class GithubAssignIssueBlock(Block):
api = get_api(credentials)
assignees_url = issue_url + "/assignees"
data = {"assignees": [assignee]}
response = api.post(assignees_url, json=data)
response.raise_for_status()
api.post(assignees_url, json=data)
return "Issue assigned successfully"
def run(
@@ -549,8 +546,7 @@ class GithubUnassignIssueBlock(Block):
api = get_api(credentials)
assignees_url = issue_url + "/assignees"
data = {"assignees": [assignee]}
response = api.delete(assignees_url, json=data)
response.raise_for_status()
api.delete(assignees_url, json=data)
return "Issue unassigned successfully"
def run(

View File

@@ -243,7 +243,6 @@ class IdeogramModelBlock(Block):
try:
response = requests.post(url, json=data, headers=headers)
response.raise_for_status()
return response.json()["data"][0]["url"]
except RequestException as e:
raise Exception(f"Failed to fetch image: {str(e)}")
@@ -257,7 +256,6 @@ class IdeogramModelBlock(Block):
try:
# Step 1: Download the image from the provided URL
image_response = requests.get(image_url)
image_response.raise_for_status()
# Step 2: Send the downloaded image to the upscale API
files = {
@@ -273,7 +271,6 @@ class IdeogramModelBlock(Block):
files=files,
)
response.raise_for_status()
return response.json()["data"][0]["url"]
except RequestException as e:

View File

@@ -56,7 +56,6 @@ class JinaChunkingBlock(Block):
}
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
result = response.json()
all_chunks.extend(result.get("chunks", []))

View File

@@ -1,7 +1,5 @@
import ipaddress
import socket
from typing import Any, Literal
from urllib.parse import quote, urlparse
from urllib.parse import quote
from autogpt_libs.supabase_integration_credentials_store.types import APIKeyCredentials
from pydantic import SecretStr
@@ -9,49 +7,14 @@ from pydantic import SecretStr
from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema
from backend.data.model import CredentialsField, CredentialsMetaInput, SchemaField
from backend.util.request import requests
from backend.util.settings import Config
class GetRequest:
@classmethod
def get_request(cls, url: str, json=False) -> Any:
validated_url = cls().validate_url(url)
response = requests.get(validated_url, allow_redirects=False)
response.raise_for_status()
response = requests.get(url)
return response.json() if json else response.text
@classmethod
def validate_url(cls, url: str) -> str:
"""
To avoid SSRF attacks, the URL should not be a private IP address
unless it is whitelisted in TRUST_ENDPOINTS_FOR_REQUESTS config.
"""
if any(
url.startswith(origin) for origin in Config().trust_endpoints_for_requests
):
return url
parsed_url = urlparse(url)
hostname = parsed_url.hostname
if not hostname:
raise ValueError(f"Invalid URL: Unable to determine hostname from {url}")
try:
host = socket.gethostbyname_ex(hostname)
for ip in host[2]:
ip_addr = ipaddress.ip_address(ip)
if ip_addr.is_global:
return url
raise ValueError(
f"Access to private or untrusted IP address at {hostname} is not allowed."
)
except ValueError:
raise
except Exception as e:
raise ValueError(f"Invalid or unresolvable URL: {url}") from e
class GetWikipediaSummaryBlock(Block, GetRequest):
class Input(BlockSchema):

View File

@@ -118,7 +118,6 @@ class CreateTalkingAvatarVideoBlock(Block):
"authorization": f"Basic {api_key.get_secret_value()}",
}
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
return response.json()
def get_clip_status(self, api_key: SecretStr, clip_id: str) -> dict:
@@ -128,7 +127,6 @@ class CreateTalkingAvatarVideoBlock(Block):
"authorization": f"Basic {api_key.get_secret_value()}",
}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
def run(

View File

@@ -86,7 +86,6 @@ class UnrealTextToSpeechBlock(Block):
}
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
return response.json()
def run(

View File

@@ -56,13 +56,12 @@ class GitHubOAuthHandler(BaseOAuthHandler):
"X-GitHub-Api-Version": "2022-11-28",
}
response = requests.delete(
requests.delete(
url=self.revoke_url.format(client_id=self.client_id),
auth=(self.client_id, self.client_secret),
headers=headers,
json={"access_token": credentials.access_token.get_secret_value()},
)
response.raise_for_status()
return True
def _refresh_tokens(self, credentials: OAuth2Credentials) -> OAuth2Credentials:
@@ -88,7 +87,6 @@ class GitHubOAuthHandler(BaseOAuthHandler):
}
headers = {"Accept": "application/json"}
response = requests.post(self.token_url, data=request_body, headers=headers)
response.raise_for_status()
token_data: dict = response.json()
username = self._request_username(token_data["access_token"])

View File

@@ -103,12 +103,11 @@ class GoogleOAuthHandler(BaseOAuthHandler):
def revoke_tokens(self, credentials: OAuth2Credentials) -> bool:
session = AuthorizedSession(credentials)
response = session.post(
session.post(
self.revoke_uri,
params={"token": credentials.access_token.get_secret_value()},
headers={"content-type": "application/x-www-form-urlencoded"},
)
response.raise_for_status()
return True
def _request_email(

View File

@@ -49,7 +49,6 @@ class NotionOAuthHandler(BaseOAuthHandler):
"Accept": "application/json",
}
response = requests.post(self.token_url, json=request_body, headers=headers)
response.raise_for_status()
token_data = response.json()
# Email is only available for non-bot users
email = (

View File

@@ -50,12 +50,14 @@ class Requests:
def __init__(
self,
trusted_origins: list[str],
trusted_origins: list[str] | None = None,
allow_redirects: bool = False,
raise_for_status: bool = True,
extra_url_validator: Callable[[str], str] | None = None,
extra_headers: dict[str, str] | None = None,
):
self.trusted_origins = trusted_origins
self.trusted_origins = trusted_origins or []
self.allow_redirects = allow_redirects
self.raise_for_status = raise_for_status
self.extra_url_validator = extra_url_validator
self.extra_headers = extra_headers
@@ -68,7 +70,14 @@ class Requests:
url = self.extra_url_validator(url)
url = validate_url(url, self.trusted_origins)
response = req.request(method, url, headers=headers, *args, **kwargs)
response = req.request(
method,
url,
headers=headers,
allow_redirects=self.allow_redirects,
*args,
**kwargs,
)
if self.raise_for_status:
response.raise_for_status()
@@ -96,4 +105,4 @@ class Requests:
return self.request("PATCH", url, *args, **kwargs)
requests = Requests(Config().trust_endpoints_for_requests)
requests = Requests(trusted_origins=Config().trust_endpoints_for_requests)