MikeWooster / api-client

Separate the high level client implementation from the underlying CRUD.
MIT License
159 stars 35 forks source link

Allow defining a client-level base URL that is prepended to endpoint URLs in requests #83

Open petebachant opened 2 years ago

petebachant commented 2 years ago

Is your feature request related to a problem? Please describe.

I would like to make requests without the full URL.

Describe the solution you'd like

I'd like to define a class or instance attribute base_url that is automatically prepended to the endpoint passed into get, post, etc.

Describe alternatives you've considered Considering subclassing, but it feels like this is a fundamental feature. Did I miss it in the documentation?

holdenweb commented 2 years ago

You should look at the documentation for the @endpoint decorator - I'm afraid the README doesn't appear to honour fragments, so you'll want to do a text search on that page.

The decorator allows you to specify a base_url value that is prepended to all attributes, making it simple to relocate a set of endpoints into whatever web space you want to serve. The example in the docs is instructive but I won't repeat it here.

Please confirm this answer resolves your issue.

petebachant commented 2 years ago

I took a look at the endpoint documentation, and it's not quite what I'm looking to do, since it looks like I would need to abstract all of my endpoints into a class, versus something simple like:

class MyClient(APIClient):
    base_url = "https://google.com"

c = MyClient()
something = c.get("/something")  # Automatically prepends base URL