Skip to main content

Almost Requested

This module provides a simple wrapper around requests 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.

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()

Release files for almost_requested 1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for almost_requested 1
File Size Uploaded
almost_requested-1.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for almost_requested 1
File Interpreter ABI Platform
almost_requested-1-py3-none-any.whl Python 3 none any Details

Total release size: 9.7 kB

Release files / almost_requested-1.tar.gz

Download URL almost_requested-1.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c72f1fa87871be2a70289767422f4e24fe27b810fa931318f36aafd3da4324ac
BLAKE2b-256 checksum
How to use checksums
ed8019271924e6925b8e4ba77cd9556216d8554c24d49f67e9b35685c87738f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.23.3

Release files / almost_requested-1-py3-none-any.whl

Download URL almost_requested-1-py3-none-any.whl
Size 3.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dec7e1dc080c7cab987ed5fe7ddb80a7ec2797419029223543e1fbbad42c7eb6
BLAKE2b-256 checksum
How to use checksums
b8b3c5f621d29c92e8f423269a7e9d4b9efe82ce2c0e44a7709e00c8e28d10b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.23.3

Release history Release notifications | RSS feed

This release

1 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page