Writing a module to abstract a REST api

Joseph L. Casale jcasale at activenetwerx.com
Fri Sep 18 23:43:13 CEST 2015

Hi Sven,

>> The problem now comes from the following:
>> # foo now contains a handle to the remote api.
>> foo = InstanceOfApiWrapper()
> Is it necessary to have an instance of that API? Just curiosity here.

Not always but often as the pattern here might rely on a handle
to a c types based api handle for example which might be a pointer
or it may hold a session with credentials and state (not for REST of course).

>> # queues is a container of Queue classes.
>> queues = foo.get_queues()
>> queue = queues[0]
>> queue.delete()
> Alright. I see you want to have concrete classes, so you work with HTTP
> like you would do with SOAP-bases web services.


> I actually was referring to the self-expanding capability of REST
> (please note, REST is not HTTP, HTTP is one protocol that can be use to
> build an REST-ful API). It's basically like: you query a resource which
> holds an link to another resource which you then you query by following
> that link and so on and so forth. So, all what you need for REST is one
> starting point to explore the resource graph.

Well that is true, but what of the credential object that gets created
with the instance of InstanceOfApiWrapper? You are certainly right
in that the instance of Queue being REST based will always have all
it needs to perform the functions, it just lacks the credentials.

>> In this case its possible for foo (InstanceOfApiWrapper) to inject a reference
>> to itself so the delete() can actually make the call.
>> # I want to create a queue from scratch...
>> disparate_queue = Queue(x, y, z)
>> # Now what happens? No handle to an api?
>> disparate_queue.delete()
> I don't see an issue here. Looks quite readable.

Well, what credentials does .delete() use? I should have actually stated
that, sorry.

>> An alternative is:
>> foo.delete_queue(disparate_queue)
>> That's not a pattern I prefer, what facilities in Python provide for a workaround
>> here?
> You always can add attributes to objects. So, add something like an
> __api__ attribute (which nobody would ever use except you when creating
> these queue objects in "get_queues").

This is where I am going, but how do you perform dependency injection
in Python? 

> It's perfectly fine to add a "secret" API instance to the object. It's
> called aspect-oriented programming. You remove the aspect of how to
> correctly manage an API instance and put that knowledge into a separate
> piece of code. *thumbs up*

Yea, I am glad you agree:) Its a model I certainly subscribe to. I just don't
know how to make the disparate case "know" of the foo instance when
foo didn't instantiate it (which gives the injection opportunity).

That foo instance also serves of a starting point to make the GET type queries.

How would you go about making disparate case lookup a handle, especially
when I insist on placing all the objects in their files. I know, tough to do in
Python, but its what I am used to.

Thanks a lot for the knowledge and insight, I greatly appreciate this.

More information about the Python-list mailing list