sckott / habanero

client for Crossref search API

Home Page:https://habanero.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Any way to set/add to user agent string?

hmpf opened this issue · comments

I know mailto can be set, but I hope to use this library as part of a service that fetches from many APIs and would like the same or same-ish user agent everywhere.

thanks for your question @hmpf

we currently send with each request the following UA headers (package versions change obviously)

User-Agent: python-requests/2.22.0 habanero/0.7.0
X-USER-AGENT: python-requests/2.22.0 habanero/0.7.0

Would you want to add to that string or change it completely?

Good question. If the existing setting can be both read and changed (allowing changing it completely and adding to it) I think you'll never get this question again :)

I need just to add to the end, my UA, link to site, and a mailto. I'll be hitting crossref once a week or so, is the current plan. (The actual mailto wil lhave to be a setting on my side though as it's a microservice that no doubt will end up running in multiple places.)

i'll have a look. Not sure right now what it will look like but it'll be possible for sure

@hmpf reinstall from branch user-agent [sudo] pip[3] install git+git://github.com/sckott/habanero.git@caching#egg=habanero

# set an additional user-agent string
x = Crossref(ua_string = "foo bar")
x
x.works(ids = '10.1371/journal.pone.0033693')

## unset the additional user-agent string
x = Crossref()
x.works(ids = '10.1371/journal.pone.0033693')

do verbose logging to see the request headers

import requests
import logging
import httplib as http_client
http_client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

The pip-url was wrong, this is the correct one: git+git://github.com/sckott/habanero.git@user-agent#egg=habanero.

For python 3, replace import httplib as http_client with import http.client as http_client.

Tested. This covers my needs!

woops, thx for the url fix - forgot to fix the copy/paste.

glad it works.