Skip to main content

Make APIs more human

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

almost_requested-1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

almost_requested-1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file almost_requested-1.tar.gz.

File metadata

  • Download URL: almost_requested-1.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.3

File hashes

Hashes for almost_requested-1.tar.gz
Algorithm Hash digest
SHA256 c72f1fa87871be2a70289767422f4e24fe27b810fa931318f36aafd3da4324ac
MD5 d552b4afe72cba7d542f7306718b31af
BLAKE2b-256 ed8019271924e6925b8e4ba77cd9556216d8554c24d49f67e9b35685c87738f0

See more details on using hashes here.

File details

Details for the file almost_requested-1-py3-none-any.whl.

File metadata

File hashes

Hashes for almost_requested-1-py3-none-any.whl
Algorithm Hash digest
SHA256 dec7e1dc080c7cab987ed5fe7ddb80a7ec2797419029223543e1fbbad42c7eb6
MD5 88f0f69334db39d128abc17c1f9059f3
BLAKE2b-256 b8b3c5f621d29c92e8f423269a7e9d4b9efe82ce2c0e44a7709e00c8e28d10b8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page