Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Commit

Permalink
New API update (#14)
Browse files Browse the repository at this point in the history
* 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
naveen521kk authored Oct 16, 2020
1 parent e5613f1 commit 38dc56a
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 24 deletions.
15 changes: 14 additions & 1 deletion docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ Changelog

sxcu-v1.0.2
===========
[TOB]

* Introduced a logger class so that users can know what
really happened. User's can just set a logging handle and
it will automatically write logs as in any other library.

* Improved Interface with Requests. Instead of Directly
calling it now logs them and goes through ``__client__``.

* Moved version to ``__version__`` and added other meta data
to it.

* Now it handle's server response codes. Previously it was
rising JSON decode Error which was unexpected and could cause
problems.


sxcu-v1.0.1
Expand Down
1 change: 1 addition & 0 deletions docs/source/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Python API Reference

~SXCU
~og_properties
~__client__.RequestClient
73 changes: 73 additions & 0 deletions sxcu/__client__.py
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
9 changes: 7 additions & 2 deletions sxcu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
:license: Apache-2.0 , see LICENSE for details.
"""
from .__version__ import __author__ # noqa F401
from .__version__ import __copyright__ # noqa F401
from .__version__ import __description__ # noqa F401
from .__version__ import __license__ # noqa F401
from .__version__ import __title__ # noqa F401
from .__version__ import __url__ # noqa F401
from .__version__ import __version__ # noqa F401
from .sxcu import SXCU, og_properties # noqa F401

__version__ = "1.0.1"
10 changes: 10 additions & 0 deletions sxcu/__logger__.py
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)
7 changes: 7 additions & 0 deletions sxcu/__version__.py
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"
80 changes: 80 additions & 0 deletions sxcu/constants.py
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__}"}
Loading

0 comments on commit 38dc56a

Please sign in to comment.