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

feat: pluggable URL parsing #41

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

feat: pluggable URL parsing #41

wants to merge 7 commits into from

Conversation

msabramo
Copy link
Owner

@msabramo msabramo commented Aug 15, 2019

This way people can customize it to their liking, as there a lot of
opinions about this, as evidenced by the comments on GH-34.

The default parsing is still the same as before, so new version don't
break existing code. But the user has the option of passing in a
settings object, which has a urlparse attribute that can be set to a
custom function that processes the URL and splits into a sockpath and
a reqpath.

Sem-Ver: feature

DEFAULT_SCHEME = 'http+unix://'

def default_urlparse(url):
return UnixAdapter.Settings.ParseResult(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit of a waste to urlparse url three times

how about:

parsed = urlparse(url)
return UnixAdapter...

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you're right. I will definitely update that. Thanks!

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Thanks again!

@msabramo msabramo force-pushed the pluggable-urlparse branch from f43c18d to 17b07ed Compare August 15, 2019 22:42
@msabramo msabramo force-pushed the pluggable-urlparse branch 2 times, most recently from 49b9339 to cef606f Compare December 28, 2021 03:13
@msabramo msabramo changed the title Support plugging in a new way of parsing URLs feat: pluggable URL parsing Dec 28, 2021
@msabramo
Copy link
Owner Author

Example of usage:

import json
from requests.compat import urlparse

from requests_unixsocket import Session, UnixAdapter


def custom_urlparse(url):
    parsed_url = urlparse(url)
    return UnixAdapter.Settings.ParseResult(
        sockpath=parsed_url.path,
        reqpath=parsed_url.fragment,
    )


session = Session(settings=UnixAdapter.Settings(urlparse=custom_urlparse))

r = session.get('http+unix://sock.localhost/var/run/docker.sock#/info')
registry_config = r.json()['RegistryConfig']
print(json.dumps(registry_config, indent=4))

@msabramo msabramo force-pushed the pluggable-urlparse branch from 58c4061 to 365b053 Compare January 3, 2022 20:44
This way people can customize it to their liking, as there a lot of
opinions about this, as evidenced by the comments on GH-34.

The default parsing is still the same as before, so new versions don't
break existing code. But the user has the option of passing in a
settings object, which has a `urlparse` attribute that can be set to a
custom function that processes the URL and splits it into a `sockpath`
and a `reqpath`.

Sem-Ver: feature
Example of using customizable `urlparse` function
This new test tests using the `UnixAdapter` directly, like
[httpie-unixsocket](https://github.com/httpie/httpie-unixsocket) does.

I wrote this test because I found a case, on the `pluggable-urlparse`
branch, where tests were passing, but executing:

```
http http+unix://%2Fvar%2Frun%2Fdocker.sock/info
```

was failing.
so it doesn't choke when used directly (e.g.: like
[httpie-unixsocket](https://github.com/httpie/httpie-unixsocket) does)
and no explicit settings are provided.
@msabramo msabramo force-pushed the pluggable-urlparse branch from 81e89a5 to afbc9fd Compare January 3, 2022 21:50
@RainCat1998
Copy link

@msabramo
The correct example of usage:

import json
from requests.compat import urlparse

from requests_unixsocket import Session, Settings


def custom_urlparse(url):
    parsed_url = urlparse(url)
    return Settings.ParseResult(
        sockpath=parsed_url.path,
        reqpath=parsed_url.fragment,
    )


session = Session(settings=Settings(urlparse=custom_urlparse))

r = session.get('http+unix://sock.localhost/var/run/docker.sock#/info')
registry_config = r.json().get('RegistryConfig', {})
print(json.dumps(registry_config, indent=4))

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.

3 participants