A New Python API

That’s a good point, IDE autocompletion will not be of much help here. It might be worth using a different library if that’s important to you.

Since each method chain returns a new client, one advantage I can think of is storing a specific method chain in a variable and re-using it for subsequent calls. For example, let’s say you had a list of data for new posts that you wanted to create.

post_data = [
  {
    "raw": "Hello World!",
    "topic_id": 123,
    ...
  },
  {
    "raw": "Tester",
    "topic_id": 501,
    ...
  },
  ...
]

You could store the “New post” method chain and re-use it to create each of the posts in the list.

new_post_endpoint = client.posts.json

for post in post_data:
  new_post_endpoint.post(data=post) 

You might be able to imagine other scenarios where this ability to re-use a client could be useful. Beyond that, there’s not a huge advantage over just passing the url like you suggested. I encourage you to read about the Universal Client library which contains some information about the rational for this type of approach.

Also, for a PHP specific implementation of this approach, check out SendGrid’s PHP API. I was inspired to build this when I read about SendGrid’s Python API.

4 Likes