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 An Importable default_user_agent Function #3226

Conversation

MichaelYochpaz
Copy link

Summary

This PR adds a default_user_agent function (corresponding to requests.utils.default_user_agent) and allows it to be imported using from httpx import default_user_agent to allow different projects to check what user-agent should be expected by default from the clients they're using (for unit-tests, for example).

This value existed as a constant variable named USER_AGENT (accessible only using httpx._client.USER_AGENT) which I didn't necessarily have to convert to a function, but I thought it would be better as a function both because it will be implemented similarly to requests, and because it will allow more flexibility in the future, in case it will need to be dynamically calculated (changing it to a function from a variable if needed in the future will break backwards compatibility).

Don't think this change requires any unit-tests or docs to be updated (didn't find any matching section), but if you think it does, let me know and I'll try to add those.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@tomchristie
Copy link
Member

Reasonable request, tho we'll prioritise minimising API surface area.

Perhaps try this?...

ua = httpx.Client().headers['User-Agent']

@tomchristie tomchristie closed this Oct 8, 2024
@MichaelYochpaz
Copy link
Author

MichaelYochpaz commented Oct 8, 2024

Reasonable request, tho we'll prioritise minimising API surface area.

Perhaps try this?...

ua = httpx.Client().headers['User-Agent']

@tomchristie Thanks for the answer.
This proposed solution will initialize a new Client object just to get this constant string. Seems very wasteful to me, compared to the simple solution of just allowing access to this variable through the API (which, as I mentioned, requests allows).

@tomchristie
Copy link
Member

That’s a valid point.
Alternatives here…

  • Set the User-Agent explicitly on the client instance, so that it is known.
  • Use f”python-httpx/{ httpx.__version__ }”

Should the UA format be covered by our versioning contract once we’re at 1.0?

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