almost_requested


Namealmost_requested JSON
Version 1 PyPI version JSON
download
home_pageNone
SummaryMake APIs more human
upload_time2023-03-24 11:25:45
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Almost Requested

This module provides a simple wrapper around [requests](https://requests.readthedocs.io/en/latest/) to make accessing
APIs a little more simple.

You start off by providing a requests.session, pre-configured with the required headers, then AlmostRequested will
construct the URL you are accessing by taking the dotted path of the AlmostRequested instance, converting it into
a URL, and returning a new AlmostRequested object, ready to call get, put, post or delete on, which themselves return
a `requests.Response`.

```python
class Github(AlmostRequested):
    def __init__(self) -> None:
        s = requests.session()
        self.GITHUB_TOKEN = os.environ["GITHUB_TOKEN"]
        s.headers["Authorization"] = f"Bearer {self.GITHUB_TOKEN}"
        s.headers["Content-Type"] = "application/vnd.api+json"
        base_url = "https://api.github.com"

        super().__init__(s, base_url)

    def get_asset(self, asset: dict[str, str]) -> None:
        s = requests.session()
        s.headers["Authorization"] = f"Bearer {self.GITHUB_TOKEN}"
        s.headers["Accept"] = "application/octet-stream"

        if os.path.exists(asset["name"]):
            return

        print("downloading", asset["name"], asset["url"])

        with s.get(url=asset["url"], stream=True) as r:
            r.raise_for_status()
            with open(asset["name"], "wb") as f:
                for chunk in r.iter_content(chunk_size=8192):
                    f.write(chunk)
        
def main():
    gh = Github()
    splitpolicies = gh.repos.octoenergy.terraform_provider_splitpolicies
    
    # Get Releases from https://github.com/octoenergy/terraform-provider-splitpolicies - by default
    # we convert underscores to dashes in paths
    releases = splitpolicies.releases.latest.get()
    
    # pretty print the JSON decoded response
    releases = splitpolicies.releases.latest.get(print_json=True)
    
    # Insert something into the URL that we can't type as a python variable name
    gh.a("some$endpoint").get()  # Will raise an exception - this endpoint doesn't exist

    # The above is the same as
    gh.append("some$endpoint").get()  # Won't work, but with clearer code

    # Avoid URL magic, just want the headers and response code checking
    gh.get(url="https://github.com/octoenergy/terraform-provider-splitpolicies").get()
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "almost_requested",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "James Tunnicliffe <james@nanosheep.org>",
    "download_url": "https://files.pythonhosted.org/packages/ed/80/19271924e6925b8e4ba77cd9556216d8554c24d49f67e9b35685c87738f0/almost_requested-1.tar.gz",
    "platform": null,
    "description": "# Almost Requested\n\nThis module provides a simple wrapper around [requests](https://requests.readthedocs.io/en/latest/) to make accessing\nAPIs a little more simple.\n\nYou start off by providing a requests.session, pre-configured with the required headers, then AlmostRequested will\nconstruct the URL you are accessing by taking the dotted path of the AlmostRequested instance, converting it into\na URL, and returning a new AlmostRequested object, ready to call get, put, post or delete on, which themselves return\na `requests.Response`.\n\n```python\nclass Github(AlmostRequested):\n    def __init__(self) -> None:\n        s = requests.session()\n        self.GITHUB_TOKEN = os.environ[\"GITHUB_TOKEN\"]\n        s.headers[\"Authorization\"] = f\"Bearer {self.GITHUB_TOKEN}\"\n        s.headers[\"Content-Type\"] = \"application/vnd.api+json\"\n        base_url = \"https://api.github.com\"\n\n        super().__init__(s, base_url)\n\n    def get_asset(self, asset: dict[str, str]) -> None:\n        s = requests.session()\n        s.headers[\"Authorization\"] = f\"Bearer {self.GITHUB_TOKEN}\"\n        s.headers[\"Accept\"] = \"application/octet-stream\"\n\n        if os.path.exists(asset[\"name\"]):\n            return\n\n        print(\"downloading\", asset[\"name\"], asset[\"url\"])\n\n        with s.get(url=asset[\"url\"], stream=True) as r:\n            r.raise_for_status()\n            with open(asset[\"name\"], \"wb\") as f:\n                for chunk in r.iter_content(chunk_size=8192):\n                    f.write(chunk)\n        \ndef main():\n    gh = Github()\n    splitpolicies = gh.repos.octoenergy.terraform_provider_splitpolicies\n    \n    # Get Releases from https://github.com/octoenergy/terraform-provider-splitpolicies - by default\n    # we convert underscores to dashes in paths\n    releases = splitpolicies.releases.latest.get()\n    \n    # pretty print the JSON decoded response\n    releases = splitpolicies.releases.latest.get(print_json=True)\n    \n    # Insert something into the URL that we can't type as a python variable name\n    gh.a(\"some$endpoint\").get()  # Will raise an exception - this endpoint doesn't exist\n\n    # The above is the same as\n    gh.append(\"some$endpoint\").get()  # Won't work, but with clearer code\n\n    # Avoid URL magic, just want the headers and response code checking\n    gh.get(url=\"https://github.com/octoenergy/terraform-provider-splitpolicies\").get()\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Make APIs more human",
    "version": "1",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "b8b3c5f621d29c92e8f423269a7e9d4b9efe82ce2c0e44a7709e00c8e28d10b8",
                "md5": "88f0f69334db39d128abc17c1f9059f3",
                "sha256": "dec7e1dc080c7cab987ed5fe7ddb80a7ec2797419029223543e1fbbad42c7eb6"
            },
            "downloads": -1,
            "filename": "almost_requested-1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "88f0f69334db39d128abc17c1f9059f3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 3796,
            "upload_time": "2023-03-24T11:25:43",
            "upload_time_iso_8601": "2023-03-24T11:25:43.664984Z",
            "url": "https://files.pythonhosted.org/packages/b8/b3/c5f621d29c92e8f423269a7e9d4b9efe82ce2c0e44a7709e00c8e28d10b8/almost_requested-1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "ed8019271924e6925b8e4ba77cd9556216d8554c24d49f67e9b35685c87738f0",
                "md5": "d552b4afe72cba7d542f7306718b31af",
                "sha256": "c72f1fa87871be2a70289767422f4e24fe27b810fa931318f36aafd3da4324ac"
            },
            "downloads": -1,
            "filename": "almost_requested-1.tar.gz",
            "has_sig": false,
            "md5_digest": "d552b4afe72cba7d542f7306718b31af",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 5934,
            "upload_time": "2023-03-24T11:25:45",
            "upload_time_iso_8601": "2023-03-24T11:25:45.821813Z",
            "url": "https://files.pythonhosted.org/packages/ed/80/19271924e6925b8e4ba77cd9556216d8554c24d49f67e9b35685c87738f0/almost_requested-1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-24 11:25:45",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "almost_requested"
}
        
Elapsed time: 0.04832s