msabramo / requests-unixsocket

Use requests to talk HTTP via a UNIX domain socket
Apache License 2.0
207 stars 29 forks source link

feat: pluggable URL parsing #41

Open msabramo opened 5 years ago

msabramo commented 5 years ago

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

msabramo commented 2 years ago

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))
RainCat1998 commented 4 weeks ago

@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))