This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: improve interface with requests * refactor: more logging statements now checks for status_code before parsing to json major refactor * try and make test pass * docs: update changelog * lint: isort * Update reference.rst * lint: remove extra comment * doc: fix typo * fix formatting
- Loading branch information
1 parent
e5613f1
commit 38dc56a
Showing
9 changed files
with
278 additions
and
24 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 |
---|---|---|
|
@@ -14,3 +14,4 @@ Python API Reference | |
|
||
~SXCU | ||
~og_properties | ||
~__client__.RequestClient |
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,73 @@ | ||
import requests | ||
|
||
from .__logger__ import logger | ||
from .constants import HEADERS | ||
|
||
__all__ = ["RequestClient"] | ||
|
||
|
||
class RequestClient: | ||
"""``RequestClient`` is internally used to communicated with | ||
``Requests`` Library. | ||
""" | ||
|
||
def __init__(self, headers: dict = None) -> None: | ||
"""This initaite the handlers. | ||
Parameters | ||
========== | ||
headers : :class:`str`, optional | ||
The extra header needed to be added to the Request. | ||
""" | ||
if headers and isinstance(headers, dict): | ||
self.headers = headers | ||
else: | ||
self.headers = HEADERS | ||
logger.debug(f"Headers recieved are: {self.headers}") | ||
|
||
def post( | ||
self, url: str, headers: dict = None, **kwargs # noqa ANN003 | ||
) -> requests.models.Response: | ||
"""Pass all the parameter to :func:`requests.post`. | ||
Also, adding the neccessary headers. Also, the newly passed header | ||
would overide the default. | ||
Parameters | ||
========== | ||
headers : :class:`str`, optional | ||
The header needed to be added to the Request. | ||
.. important :: | ||
The header would overide the default header. | ||
""" | ||
logger.debug(f"Trying to do a Post Requests to {url}") | ||
headers = self.headers if headers is None else headers | ||
con = requests.post(url, headers=headers, **kwargs) | ||
logger.debug(f"Recieved Headers from {url}: {con.headers}") | ||
logger.debug(f"status_code returned was:{con.status_code}") | ||
response = con.text | ||
logger.info(f"Recieved Response: {response}") | ||
return con | ||
|
||
def get( | ||
self, url: str, headers: dict = None, **kwargs # noqa ANN003 | ||
) -> requests.models.Response: | ||
"""Pass all the parameter to :func:`requests.get`. | ||
Also, adding the neccessary headers. Also, the newly passed header | ||
would overide the default. | ||
Parameters | ||
========== | ||
headers : :class:`str`, optional | ||
The header needed to be added to the Request. | ||
.. important :: | ||
The header would overide the default header. | ||
""" | ||
logger.debug(f"Trying to do Get Requests to {url}") | ||
headers = self.headers if headers is None else headers | ||
con = requests.get(url=url, headers=headers, **kwargs) | ||
logger.debug(f"Recieved Headers from {url}: {con.headers}") | ||
# TODO: Don't use json instead implement a custom class here. | ||
response = con.text | ||
logger.info(f"Recieved Response: {response}") | ||
return con |
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,10 @@ | ||
import logging | ||
|
||
logger = logging.getLogger("sxcu") | ||
s_handler = logging.StreamHandler() | ||
s_handler.setLevel(logging.DEBUG) | ||
s_format = logging.Formatter( | ||
"%(asctime)s - %(name)s - %(levelname)s - %(message)s", | ||
) | ||
s_handler.setFormatter(s_format) | ||
logger.addHandler(s_handler) |
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,7 @@ | ||
__title__ = "sxcu" | ||
__description__ = "Python API wraper for sxcu.net" | ||
__url__ = "https://sxcu.syrusdark.website" | ||
__version__ = "1.0.1" | ||
__author__ = "Naveen M K" | ||
__license__ = "Apache 2.0" | ||
__copyright__ = "Copyright 2020 Naveen M K" |
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,80 @@ | ||
from .__version__ import __version__ | ||
|
||
status_code_upload_image = { | ||
"403": { | ||
"message": "Invalid upload token", | ||
"desc": "The specified upload token does not match the domain's upload token.", | ||
}, | ||
"404": { | ||
"message": "Collection not found", | ||
"desc": "The specified collection was not found.", | ||
}, | ||
"405": { | ||
"message": "Request is not POST", | ||
"desc": "The request method must be POST.", | ||
}, | ||
"406": { | ||
"message": "Upload error 101x", | ||
"desc": "An error occurred while handling the uploaded file.", | ||
}, | ||
"407": { | ||
"message": "Subdomain is private, a valid upload token is required", | ||
"desc": "The sub domain you tried to upload to is private, and requires a valid upload token in order to upload to it.", | ||
}, | ||
"409": { | ||
"message": "No file sent", | ||
"desc": "No binary file was sent in the 'image' field.", | ||
}, | ||
"410": { | ||
"message": "Collection is private but no collection token provided", | ||
"desc": "The collection you tried to upload to is set to private and requires a collection token in order to upload to it.", | ||
}, | ||
"412": { | ||
"message": "User-agent header not set", | ||
"desc": "The request did not contain a User-Agent header.", | ||
}, | ||
"413": { | ||
"message": "File is over the size limit", | ||
"desc": "Uploaded file is larger than 95 MB.", | ||
}, | ||
"415": { | ||
"message": "File type not allowed.", | ||
"desc": "The type of the uploaded file is not supported.", | ||
}, | ||
"416": { | ||
"message": "Invalid collection token", | ||
"desc": "The specified collection token does not match the collection's token.", | ||
}, | ||
"422": { | ||
"message": "Malformed JSON in OpenGraph properties", | ||
"desc": "The OpenGraph properties JSON array could not be properly parsed, and is most likely malformed.", | ||
}, | ||
"429": {"message": None, "desc": "The request exceeded the rate limit."}, | ||
"500": { | ||
"message": "The file was not uploaded due to an unknown error", | ||
"desc": "An unknown error has occurred while processing the file, try again later.", | ||
}, | ||
} | ||
status_code_upload_text = { | ||
"409": { | ||
"message": None, | ||
"desc": "The text POST param is missing.", | ||
}, | ||
"413": { | ||
"message": None, | ||
"desc": "Text is too long (8 MB).", | ||
}, | ||
"429": {"message": None, "desc": "The request exceeded the rate limit."}, | ||
} | ||
status_code_create_link = { | ||
"400": { | ||
"message": None, | ||
"desc": "The text POST param is missing or invalid URL", | ||
}, | ||
"429": {"message": None, "desc": "The request exceeded the rate limit."}, | ||
} | ||
|
||
status_code_general = { | ||
"429": {"message": None, "desc": "The request exceeded the rate limit."}, | ||
} | ||
HEADERS = {"User-Agent": f"python-sxcu-{__version__}"} |
Oops, something went wrong.