-
Notifications
You must be signed in to change notification settings - Fork 98
Set X-Planet-App header based on origin of request #609
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
Conversation
747549f to
d9e6e61
Compare
|
notably - this doesn't yet set an origin for Subscriptions, to avoid conflicting with any imminent changes |
jreiberkyle
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great and my only comments are more food for thought around how the header will be used than anything that requires change
| except exceptions.PlanetError: | ||
| auth = Auth.from_file() | ||
|
|
||
| if origin in ('cli', 'sdk'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a kind way of handling an unexpected value for origin. What about just kicking up a ClientError if origin isn't one of the expected values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could, but as this value is solely for logging purposes I think failing permissively is fine. If a user for some reason initiates a Session and deliberately changes origin to an unexpected value, I'd be curious about the reason but not enough to block with a ClientError :)
| auth = Auth.from_file() | ||
|
|
||
| if origin in ('cli', 'sdk'): | ||
| x_planet_app = "python-" + origin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the origin is CLI, do we want the python prepended? Probably depends on how we intend to use it. From the user's point of view, a CLI is a CLI and the only python part about it is how they install it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the python-xxx format is just following precedent in the current logged values (e.g., V1 is/was logged as python-client). We'll ultimately want to be able to log future tools (e.g., a hypothetical r-sdk) too, so this gives us room to grow towards that.
| auth = ctx.obj['AUTH'] | ||
| base_url = ctx.obj['BASE_URL'] | ||
| async with Session(auth=auth) as sess: | ||
| async with Session(auth=auth, origin='cli') as sess: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
slick solution :)
|
@sarasafavi @jreiberkyle my concern is that we're adding a new argument to the Session constructor with no guidance to external users about whether they can choose their own values. It could be a red herring? If we would like to know if a Session is created in the context of a CLI command or not, we can look at the interpreter frame stack. It's a bit esoteric, but is reliable for CPython (not PyPy). For example, I copied this to the end of import inspect
caller = inspect.stack()[1]
LOGGER.info("Caller function=%r, filename=%r", caller.function, caller.filename)and then when I run There we go, the Session constructor was called by create_subscription_cmd from planet/cli/subscriptions.py. In that case we could add "cli" to the header. I like this because it doesn't increase the Session API surface and doesn't require any new documentation. It won't work for PyPy because that Python runtime doesn't have frames, but in that case we could stick with "sdk" in the header. |
|
I have another idea, much simpler than my previous one and with all the same benefits (no Session API expansion). We subclass Session for use under planet/cli and set the user agent and header there. class CliSession(Session):
def __init__(self, auth: AuthType = None):
super().__init__(auth)
self._client.headers.update({
'User-Agent': f'planet-sdk-python/{__version__}',
'X-Planet-App': 'python-cli'
}) |
|
Superseded by #607 after discussion with @sarasafavi . |
|
Ohh yes subclassing is a great approach!
… On Jun 28, 2022, at 1:01 PM, Sean Gillies ***@***.***> wrote:
I have another idea, much simpler. We subclass Session for use under planet/cli and set the user agent and header there.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.
|
Closes #336, #339
A quick-n-dirty attempt to set "X-Planet-App" custom header to either
planet-sdkorplanet-cli, depending on origin of the request (acceptable values of this header areplanet-sdk,planet-cli, orunknown). Defaults toplanet-sdkfor non-CLI-originated Sessions.