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

http server that uses an API key in the header #27

Open
TomHarrop opened this issue Dec 12, 2024 · 1 comment
Open

http server that uses an API key in the header #27

TomHarrop opened this issue Dec 12, 2024 · 1 comment

Comments

@TomHarrop
Copy link

Is it possible to pass custom headers using the snakemake_storage_plugin_http?

I'm trying to download files from a server that uses an API key in the header
for authentication (I have no control over this).

I've tried variations of storage.http(url_to_get,headers=request_headers) and
passing a requests.get object but it doesn't work.

The auth argument says it needs a request.auth subclass. I can do this in
python:

import os
import requests

class ApiKeyAuth(requests.auth.AuthBase):
    def __init__(self, api_key):
        self.api_key = api_key

    def __call__(self, r):
        r.headers["Authorization"] = self.api_key
        return r


apikey = os.getenv("my_key")
auth = ApiKeyAuth(apikey)

url_to_get = "https://server.com/file.bam"
resp = requests.get(url_to_get, auth=auth, stream=True)

Would it be possible to allow custom AuthBase classes with something like
issubclass(auth_type, requests.auth.AuthBase)? Right now the plugin hard
codes the available classes

if auth_type == "HTTPBasicAuth":
return HTTPBasicAuth(*matches.group("arg").split(","))
elif auth_type == "HTTPDigestAuth":
return HTTPDigestAuth(*matches.group("arg").split(","))
elif auth_type == "OAuth1":
return OAuth1(*matches.group("arg").split(","))

Maybe there's an easier way?

@TomHarrop
Copy link
Author

Here is a hack. I definitely don't recommend doing this IRL but I can't see another way of passing an API key in a header.

requests_get = requests.get

def requests_get_with_auth_header(url, **kwargs):
    if 'headers' not in kwargs:
        kwargs['headers'] = {}
    kwargs['headers']['Authorization'] = apikey
    return requests_get(url, **kwargs)

requests.get = requests_get_with_auth_header

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

No branches or pull requests

1 participant