From cee2f43f064afe4f775b8ea50e3c95aa485a16a2 Mon Sep 17 00:00:00 2001 From: Justaus3r <72691864+Justaus3r@users.noreply.github.com> Date: Fri, 19 Aug 2022 16:17:43 +0500 Subject: [PATCH] feat(post): now return html url for newly created gist. --- ceg/api.py | 19 ++++++++++++------- ceg/ceg.py | 24 ++++++++++++++++++------ ceg/misc.py | 2 +- docs/ceg/api.html | 4 +--- pyproject.toml | 2 +- 5 files changed, 33 insertions(+), 18 deletions(-) diff --git a/ceg/api.py b/ceg/api.py index 18921e0..0dbd4bc 100755 --- a/ceg/api.py +++ b/ceg/api.py @@ -36,9 +36,7 @@ ``` For creating a gist: ``` -response_str = cgi.post("file1.py", "file2.py", "dirty_secrets.verysecurefile", is_private=False, gist_description="bla") -# or simply -cgi.post(...) +gist_url = cgi.post("file1.py", "file2.py", "dirty_secrets.verysecurefile", is_private=False, gist_description="bla") # note that if a file provided as argument does not exist,ceg will automatically create it and open # it in your default file editor. ``` @@ -126,7 +124,12 @@ def get(self, *args: str) -> str: self.ceg_instance.get() return self.ceg_instance.response_status_str - def post(self, *args: str, is_private: bool = False, gist_description: str) -> str: + def post( + self, + *args: str, + is_private: bool = False, + gist_description: Optional[str] = None + ) -> str: """Create arbitrary number of gists. Args: @@ -135,14 +138,16 @@ def post(self, *args: str, is_private: bool = False, gist_description: str) -> s gist_description: Description for the gist. Returns: - Returns HTTP call response status in string format. + Returns HTML url for newly created gist. """ self.ceg_instance.http_operation = "post" self.ceg_instance.arg_val = args self.ceg_instance.gist_no_public = is_private self.ceg_instance.gist_description = gist_description - self.ceg_instance.post() - return self.ceg_instance.response_status_str + # type casting because of distinct variable types(i.e Optional[str] and str) + # and so so mypy will complain if not type casted + gist_html_url: str = str(self.ceg_instance.post()) + return gist_html_url def patch( self, *args: str, gist_id: str, gist_description: Optional[str] = None diff --git a/ceg/ceg.py b/ceg/ceg.py index 4db8081..63cf103 100755 --- a/ceg/ceg.py +++ b/ceg/ceg.py @@ -225,10 +225,13 @@ def __send_http_request( return_var: Union[ int, List[Dict[str, Union[str, Dict[str, str], bool, int, None]]] ] - if self.http_operation == "get": - response_str: str = response.content.decode("utf-8") - return_var = json.loads(response_str) - elif self.http_operation in ["post", "patch", "delete"]: + if self.http_operation in ["get", "post"]: + response_hashtable = json.loads(response.content.decode("utf-8")) + if self.http_operation == "get": + return_var = response_hashtable + else: + return_var = response_hashtable.get("html_url") + elif self.http_operation in ["patch", "delete"]: return_var = response.status_code return return_var @@ -402,13 +405,16 @@ def get(self, **kwargs) -> None: ) self.logger.info("Sucessfully downloaded the gist!", send_log=do_logging) - def post(self, **kwargs) -> None: + def post(self, **kwargs) -> Optional[str]: """Create gists. Creates arbitrary number of gists depending on files given on cli. Args: **kwargs: Auxiliary arbitrary keyword arguments. + + Returns: + (Optionally) return html url of the newly created gist """ is_patch: Optional[bool] new_filenames: Optional[Dict[str, str]] @@ -454,8 +460,14 @@ def post(self, **kwargs) -> None: self.payload.update({"public": not self.gist_no_public}) if self.gist_description: self.payload.update({"description": self.gist_description}) - self.__send_http_request(params=json.dumps(self.payload)) + gist_html_url = self.__send_http_request(params=json.dumps(self.payload)) self.logger.info(f"Sucessfully {op_success_msg[self.http_operation]} the gist!") + if self.http_operation == "post": + if self.to_stdout: + self.logger.info(f"Gist Url: {gist_html_url}") + else: + return gist_html_url # type: ignore + return None def patch(self) -> None: """Modify an existing gist.""" diff --git a/ceg/misc.py b/ceg/misc.py index 8e54fc6..810dc26 100755 --- a/ceg/misc.py +++ b/ceg/misc.py @@ -59,7 +59,7 @@ class UtilInfo: DESCRIPTION: str = "A simple gist crud utility." # Caution(message to myself): Be careful when updating the version because # wrong updates can be a mess. - VERSION: str = "0.2.0" + VERSION: str = "0.3.0" def exception_executioner(exception_obj) -> None: diff --git a/docs/ceg/api.html b/docs/ceg/api.html index 183de2a..219a095 100644 --- a/docs/ceg/api.html +++ b/docs/ceg/api.html @@ -99,9 +99,7 @@

Typical usage example

For creating a gist:

-
response_str = cgi.post("file1.py", "file2.py", "dirty_secrets.verysecurefile", is_private=False, gist_description="bla")
-# or simply
-cgi.post(...)
+
gist_html_url = cgi.post("file1.py", "file2.py", "dirty_secrets.verysecurefile", is_private=True, gist_description="bla")
 # note that if a file provided as argument does not exist,ceg will automatically create it and open
 # it in your default file editor.
 
diff --git a/pyproject.toml b/pyproject.toml index fa97369..cdb9d7a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ceg" -version = "0.2.0" +version = "0.3.0" description = "A simple gist crud utility." license = "GPL-3.0-or-later" authors = ["Justaus3r "]