[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-list
Subject:    Re: Most "pythonic" syntax to use for an API client library
From:       Peter Otten <__peter__ () web ! de>
Date:       2019-04-29 7:18:59
Message-ID: qa68h7$5v6e$1 () blaine ! gmane ! org
[Download RAW message or body]

Jonathan Leroy - Inikup via Python-list wrote:

> Hi all,
> 
> I'm writing a client library for a REST API. The API endpoints looks like
> this: /customers
> /customers/1
> /customers/1/update
> /customers/1/delete
> 
> Which of the following syntax do you expect an API client library to
> use, and why?
> 
> 1/
> api.customers_list()
> api.customers_info(1)
> api.customers_update(1, name='Bob')
> api.customers_delete(1)
> 
> 2/
> api.customers.list()
> api.customers.info(1)
> api.customers.update(1, name='Bob')
> api.customers.delete(1)
> 
> 3/
> api.customers.list()
> api.customers(1).info()
> api.customers(1).update(name='Bob')
> api.customers(1).delete()
> 
> ...any other?

How about mimicking (to some extent) an existing interface, like a list, 
dict, or set:

customers = api.customers

list(customers)  # __iter__

alice = customers[1]  # __getitem__

print(alice)  # __str__

alice.name = "Bob"  # __setattr__

del customers[42]  # __delitem__
del customers[alice]

> #3 seems to be more "pretty" to me, but I did not find any "official"
> recommendation online.
> 
> Thanks.
> 
> --
> Jonathan.


-- 
https://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic