Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to pass HTTP headers to API calls #106

Closed
wants to merge 1 commit into from

Conversation

kovan
Copy link

@kovan kovan commented Sep 11, 2022

Suggested implementation for #79.
Tested and working.

Summary:

Each Google API response has a E-tag attached to it, which is included in the JSON response.
If you save the response along with this E-tag, you can later check if the content of the response that you previously saved has has changed or not on the server, by adding the "If-None-Match" HTTP header to the API call. If the content has not changed, the API server answers with the code "304 not modifed".

This is a simple way to keep data retrieved from the API up to date.

The code of the API call would look like this:

    async with Aiogoogle(
        api_key= "your-api-key"
    ) as aiogoogle:
        api = await aiogoogle.discover("youtube", "v3")
        # etag hardcoded for the example, but this is supposed to be retrieved from the database or similar:
        etag = "vsu1KWWbOO-15Idit0GDZKgEFfo" 
        result = await aiogoogle.as_api_key( 
            api.videos.list(
                headers={"If-None-Match": etag},
                part="snippet,contentDetails,statistics",
                id="YOUR_VIDEO_ID"
            ),
            full_res=True
        )
        if result.status_code == 304:
            print("Resource was not modified since last check")

@omarryhan
Copy link
Owner

Hey, so sorry for the late response.
I'm hesitant to merge this in because if we pass the keyword headers here, it might conflict with a potential API argument with the same name. Can we maybe pass it inside a kwarg called aiogoogle_request_options? e.g.

api.videos.list(
    aiogoogle_request_options={
        "headers": {"If-None-Match": etag}
    },
    part="snippet,contentDetails,statistics",
    id="YOUR_VIDEO_ID",
)

@kovan
Copy link
Author

kovan commented Oct 29, 2022

Whatever works, OK. Works well for me.

@omarryhan omarryhan closed this Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants