From be973ee183edfc524003306dec7923bb67743b6e Mon Sep 17 00:00:00 2001 From: Sepand Haghighi Date: Mon, 27 Mar 2023 01:09:43 +0330 Subject: [PATCH] Timeout (#182) * fix : nft_storage_upload function updated * doc : CHANGELOG updated * fix : autopep8 * doc : README updated * doc : minor edit in notebook flags * fix : minor edit in style --- CHANGELOG.md | 2 ++ README.md | 6 ++++-- examples/demo.ipynb | 21 ++++++++++++++++++--- samila/functions.py | 7 +++++-- samila/genimage.py | 16 ++++++++++++---- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fd10bb..40b0864 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - `rotate` function ### Changed - `rotation` parameter added to `plot` method +- `timeout` parameter added to `nft_storage` method - `load_config` function modified +- `nft_storage_upload` function modified - Random mode modified - `RANDOM_EQUATION_GEN_COMPLEXITY` parameter renamed to `RANDOM_EQUATION_MAX_COMPLEXITY` - `README.md` updated diff --git a/README.md b/README.md index f1e1863..3b9ad17 100644 --- a/README.md +++ b/README.md @@ -179,7 +179,7 @@ You can even rotate your art by using `rotation` parameter. Enter your desired r >>> g.plot(rotation=45) ``` -* Default rotation is 0. +* Default rotation is 0 ### Range ```pycon @@ -253,7 +253,7 @@ You can make your custom color map and use it in Samila Upload generated image directly to [NFT.storage](https://NFT.storage) ```pycon ->>> g.nft_storage(api_key="YOUR_API_KEY") +>>> g.nft_storage(api_key="YOUR_API_KEY", timeout=5000) {'status': True, 'message': 'FILE_LINK'} ``` @@ -268,6 +268,8 @@ or {'status': {'image': True, 'data':True}, 'message': {'image':'IMAGE_FILE_LINK', 'data':'DATA_FILE_LINK'} ``` +* Default timeout is **3000** seconds + ### Save image Save generated image diff --git a/examples/demo.ipynb b/examples/demo.ipynb index ac33b5f..6a93dc1 100644 --- a/examples/demo.ipynb +++ b/examples/demo.ipynb @@ -134,7 +134,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -412,6 +411,15 @@ "g1.nft_storage(api_key=\"YOUR_API_KEY\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "g1.nft_storage(api_key=\"YOUR_API_KEY\", timeout=5000)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -436,11 +444,18 @@ "source": [ "g1.nft_storage(api_key=\"YOUR_API_KEY\", upload_data=True)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "* Default timeout is **3000** seconds" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3.8.10 64-bit", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -454,7 +469,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.10" + "version": "3.5.2" }, "toc": { "base_numbering": 1, diff --git a/samila/functions.py b/samila/functions.py index 7f22a75..5c677af 100644 --- a/samila/functions.py +++ b/samila/functions.py @@ -499,7 +499,7 @@ def _GI_initializer(g, function1, function2): g.missed_points_number = 0 -def nft_storage_upload(api_key, data): +def nft_storage_upload(api_key, data, timeout): """ Upload file to nft.storage. @@ -507,6 +507,8 @@ def nft_storage_upload(api_key, data): :type api_key: str :param data: image data :type data: binary + :param timeout: upload timeout (in seconds) + :type timeout: int :return: result as dict """ result = {"status": True, "message": NFT_STORAGE_SUCCESS_MESSAGE} @@ -515,7 +517,8 @@ def nft_storage_upload(api_key, data): response = requests.post( url=NFT_STORAGE_API, data=data, - headers=headers) + headers=headers, + timeout=timeout) response_json = response.json() if response_json["ok"]: result["message"] = NFT_STORAGE_LINK.format( diff --git a/samila/genimage.py b/samila/genimage.py index 9a2c2ac..ce86ed1 100644 --- a/samila/genimage.py +++ b/samila/genimage.py @@ -172,7 +172,8 @@ class GenerativeImage: api_key, upload_data=False, upload_config=False, - depth=None): + depth=None, + timeout=3000): """ Upload image to nft.storage. @@ -184,6 +185,8 @@ class GenerativeImage: :type upload_config: bool :param depth: image depth :type depth: float + :param timeout: upload timeout (in seconds) + :type timeout: int :return: result as dict """ save_params_filter(self, depth) @@ -191,20 +194,25 @@ class GenerativeImage: if not response["status"]: return {"status": False, "message": response["message"]} buf = response["buffer"] - response = nft_storage_upload(api_key=api_key, data=buf.getvalue()) + response = nft_storage_upload( + api_key=api_key, + data=buf.getvalue(), + timeout=timeout) if upload_config == False and upload_data == False: return response result = {key: {'image': value} for key, value in response.items()} if upload_config: response = nft_storage_upload( api_key=api_key, - data=json.dumps(get_config(self))) + data=json.dumps(get_config(self)), + timeout=timeout) for key, value in response.items(): result[key]['config'] = value if upload_data: response = nft_storage_upload( api_key=api_key, - data=json.dumps(get_data(self))) + data=json.dumps(get_data(self)), + timeout=timeout) for key, value in response.items(): result[key]['data'] = value return result