{ "info": { "author": "S\u0142awomir Kabik", "author_email": "slawek@redsoftware.pl", "bugtrack_url": null, "classifiers": [], "description": "# Endpoint\nThis is the main thing what you will use in that lib.With this class you can easy create clean structure of you rest api clients.\n\nWe recommend create separate file for it i.e. endpoints.py \n\nTo use it you have to put that class as parent in your Endpoint Class.\n\n### Mandatory objects:\n\n- `endpoint`: the path to your endpoint i.e. `/user/{user_id}/details/`. To use endpoint parameters put them into curly braces: `{endpoint_parameter}` in your endpoint's path. \n- method: `GET`, `POST`, `PUT`, `PATCH` or `DELETE` method. You can set it as `Request.GET` or as a string `get`.\n\n### Optional objects:\n \n- `header` (`dict`) - set it if you need to send request to endpoints with some headers values. \n- `cookies` (`dict`) - set it when you need include cookies inside your request.\n- `auth` (`tuple`) - set it when you need authorization in your requests. This field should be set as instance of auth class. Check auth.py file.\n- `payload` (`dict`) - set it when you need to send `GET` or `POST` or `PATCH` or `PUT` or `DELETE` with parameters. You can set it directly as field in Endpoint class or as argument when you create Endpoint's instance.\n\n### Instance fields:\n- `payload` (`dict`) - definition above.\n- `url_params` (`dict`) - you set this when you use parameters in your endpoint path.\n\nLets do some example code!\n\n### HOW TO INSTALL \n\n`pip install py-rest-client`\n\n### HOW TO SET SETTINGS\n\nBefore you start using `rest-client` lib you have to set correctly your settings variables.\n\nOn the first step you have to put correct `REST_CLIENT_SETTINGS_MODULE` in you `__init__.py`.\n\n```python\n# __init__.py in main dir where you have project.\nimport os\n\nos.environ.setdefault(\"REST_CLIENT_SETTINGS_MODULE\", \"your_project_name.settings\") # this is only example\n```\n\nNow you can creaet `settings.py` file and set those variables:\n\n```python\n# Those are only examples of values.\nREST_SERVER_HOST = '0.0.0.0'\nREST_SERVER_PROTOCOL = 'http://'\nREST_SERVER_PORT = '8000'\n```\n\nDon't worry if you didn't set correct path to `settings module` or `settings variables` in `settings.py`. \n\nYou will be informed about that.\n\n```python\n# when you set bad path to settings module\nImportError: No module named 'my_path_to_settings'\n```\n\n```python\n# when you didn't set correctly one of variables.\nAttributeError: module 'rest_client.your_project.settings' has no attribute 'REST_SERVER_PROTOCOL'\n```\n\n\n### Lesson 1\n\nIn first lesson we show you how we handle errors in our `rest-client`. We create endpoint with wrong endpoint url. \n\n```python\nclass InstagramEndpointGET(Endpoint):\n method = Request.GET\n endpoint = 'api/v1/instagram_404'\n \nistagram = InstagramEndpointGET()\n# raises exception:\n# rest_client.exceptions.EndpointError: 404 Not Found\n```\n\nIt handles all REST API errors.\n\n### Lesson 2\n\nIn this lesson we show you how to create simple endpoint which use method `GET` and `endpoint` path to fetch data from REST API. \n\n#### Mock response:\n```json\n{\n \"data\": [\n {\n \"type\": \"image\",\n \"users_in_photo\": [],\n \"filter\": \"Earlybird\",\n \"tags\": [\"expobar\"],\n \"comments\": {\n \"count\": 0\n }\n }\n ]\n}\n```\n\n#### Rest-Client\n```python\nclass InstagramEndpointGET(Endpoint):\n method = Request.GET\n endpoint = 'api/v1/instagram'\n\n\ninstagram = InstagramEndpointGET()\n\nprint(instagram.objects.values())\n# returns: \n# {'data': [{'filter': 'Earlybird', 'comments': {'count': 0}, 'type': 'image', 'users_in_photo': [], 'tags': ['expobar']}]}\n\nprint(instagram.objects.data) # you can use dot notation to get values from fields.\n# returns:\n# InstagramEndpointGET.data: [TestInstagramEndpointGET.data[0]: {'users_in_photo': [], 'filter': 'Earlybird', 'type': 'image', 'tags': ['expobar'], 'comments': {'count': 0}}]\n\nprint(instagram.objects.data[0].type) # you can use dot notation and indexes to get value from fields.\n# returns:\n# InstagramEndpointGET.data[0].type: image\n\nfor field, value in testing.objects.data[0].values().items():\n print(\"{field}: {value}\".format(field=field, value=value))\n \n# returns:\n# type: image\n# tags: ['expobar']\n# comments: {'count': 0}\n# filter: Earlybird\n# users_in_photo: []\n```\n\nIn this snippet we've created Endpoint's Class with method `GET` and enpoint's path `api/v1/instagram`. \n\nThe response is in `json` format as we have in all Rest APIs.\n\nTo fetch data from this endpoint you have to create instance of your endpoint - in this moment you call endpoint with `GET` request. Fetched data is stored in field `objects`.\n\nTo see what the endpoint returned you can use `values()` method by calling it directly on `objects` field. This method returns data as python objects.\n\n```python\nendpoint_instance.objects.values()\n```\n\nTo get values from `objects` use dot notation to call them. It returns python types: `int`, `float`,`string`, `list` and `dict`. You can use them as standard python fields.\n\n```python\nendpoint_instance.objects.data\n```\n\nWhen endpoint returns `list` you can use dot notation with indexes.\n\n```python\nendpoint_instance.objects.list_data[0].some_field\n```\n\n** In the same way you can use endpoitn with methods: `GET`, `POST`, `PUT`, `PATCH`, `DELETE`. All REST API responses are handle in the same way. You can use `values()` method and dot notation. Response is always stored in `objects` field.\n\n### Lesson 3\n\nIn this lesson we show you how to create simple endpoint which uses method `GET` and `endpoint` path to fetch data from REST API with `url_param`.\n\n#### Rest-Client\n```python\nclass InstagramEndpointGET(Endpoint):\n method = Request.GET\n endpoint = 'api/v1/instagram/user/{user_id}'\n \ninstangram = InstagramEndpointGET(url_param={'user_id': 1})\ninstagram.values()\n```\n\n### Lesson 5\n\nWe know that sometimes we need to send `payload` or `body` in your request. In that case you have to create `endpoint` instance with `payload` as `argument`.\n\n#### Rest-Client\n```python\nclass InstagramEndpointGET(Endpoint):\n method = Request.GET\n endpoint = 'api/v1/instagram/user/{user_id}'\n\ninstangram = InstagramEndpointGET(url_param={'user_id': 1}, payload={'country': 'UK'})\ninstagram.values()\n```\n\n### Lesson 6\n\nNow it's time to show you how to create Endpoint when you need to send request with authentication.\n\nWe handle 3 types of authentication:\n- `HTTPBasicAuth('user', 'password')`\n- `HTTPDigestAuth('user', 'password')`\n- `OAuth1('YOUR_APP_KEY', 'YOUR_APP_SECRET', 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET')`\n\nYou need to choose one from above and put it as a `field` in `Endpoint` class.\n\n#### Rest-Client\n```python\nclass InstagramEndpointGET(Endpoint):\n method = Request.GET\n endpoint = 'api/v1/instagram/user/{user_id}'\n auth = HTTPBasicAuth('user', 'password')\n\ninstangram = InstagramEndpointGET(url_param={'user_id': 1})\ninstagram.values()\n```\n\n### Lesson 7\n\nYou should read that lesson if you would like to handle `endpoint` with `cookies`. You have to put `cookies` as a field in `Endpoint` class.\n\n#### Rest-Client\n```python\nclass InstagramEndpointGET(Endpoint):\n method = Request.GET\n endpoint = 'api/v1/instagram/user/{user_id}'\n cookies = {'some_cookie`: 'some_cookie_value'}\n\ninstangram = InstagramEndpointGET(url_param={'user_id': 1})\ninstagram.values()\n```\n\n### Lesson 8\n\nSometimes we have to send request with `headers`. To implement it you need set it as a `Enpoint` class `field`.\n\n#### Rest-Client\n```python\nclass InstagramEndpointGET(Endpoint):\n method = Request.GET\n endpoint = 'api/v1/instagram/user/{user_id}'\n headers = {'some_headers`: 'some_header_value'}\n\ninstangram = InstagramEndpointGET(url_param={'user_id': 1})\ninstagram.values()\n```", "description_content_type": null, "docs_url": null, "download_url": "https://github.com/slawek87/py-rest-client", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "UNKNOWN", "keywords": "rest api client,python rest api client,rest api endpoint,consume rest api", "license": "Copyright (c) 2017, S\u0142awomir Kabik\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice, this\n list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice,\n this list of conditions and the following disclaimer in the documentation\n and/or other materials provided with the distribution.\n\n* Neither the name of the {organization} nor the names of its\n contributors may be used to endorse or promote products derived from\n this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\nAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\nFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\nDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\nCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\nOR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.", "maintainer": null, "maintainer_email": null, "name": "py-rest-client", "package_url": "https://pypi.org/project/py-rest-client/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/py-rest-client/", "project_urls": { "Download": "https://github.com/slawek87/py-rest-client", "Homepage": "UNKNOWN" }, "release_url": "https://pypi.org/project/py-rest-client/0.1.1/", "requires_dist": null, "requires_python": null, "summary": "Py-Rest-Client is a useful lib for programmers who work with clients for REST API. In simply and easy way you can build Endpoint classes where you put endpoint settings.In that case you have clean structure of endpoints in your code. This lib is only for consume REST API endpoints.", "version": "0.1.1" }, "last_serial": 2923033, "releases": { "0.1.1": [ { "comment_text": "", "digests": { "md5": "aeaa1a8a9f68baeb6dcdafbee92e5992", "sha256": "3b6fd022ef7f5bcd350e7f062448e0b7445e1f5522169241f8ee8d3965e420d5" }, "downloads": -1, "filename": "py-rest-client-0.1.1.tar.gz", "has_sig": false, "md5_digest": "aeaa1a8a9f68baeb6dcdafbee92e5992", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7223, "upload_time": "2017-06-03T19:04:19", "url": "https://files.pythonhosted.org/packages/b6/05/0b08beb353ea515c894e647decea2c2dff76839d6813f7cb0c9afc58846a/py-rest-client-0.1.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "aeaa1a8a9f68baeb6dcdafbee92e5992", "sha256": "3b6fd022ef7f5bcd350e7f062448e0b7445e1f5522169241f8ee8d3965e420d5" }, "downloads": -1, "filename": "py-rest-client-0.1.1.tar.gz", "has_sig": false, "md5_digest": "aeaa1a8a9f68baeb6dcdafbee92e5992", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7223, "upload_time": "2017-06-03T19:04:19", "url": "https://files.pythonhosted.org/packages/b6/05/0b08beb353ea515c894e647decea2c2dff76839d6813f7cb0c9afc58846a/py-rest-client-0.1.1.tar.gz" } ] }