Using a Proxy to Simplify API Access

The example below shows a simple utilisation of the proxy model to securely access an API. The API in question happens to be fairly full secure access to the Django ORM for a substantial business application but it could be any object oriented API.

The objects are served as local proxy objects which can be handled as if they are local.

The proxy model objects are initially obtained either by id, url or by a search query. Once obtained, proxies are manipulated like ORM model objects in Django save that their related objects are supplied as simple lazy lists. This makes using the API very similar to working with the ORM directly, except that there may be a need to handle communication related exceptions.

~ $ ipython
Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
Type "copyright", "credits" or "license" for more information.

IPython 1.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from lsproxies.proxy import Proxy

In [2]: from lsproxies.models import *

In [3]: proxy.initialise('http://dev:8900/', credentials={'username': 'pwadmin', 'password': 'xxxxxxx'})
Out[3]: <lsproxies.proxy.Proxy at 0x387c7d0>

In [4]: user = proxy.direct_get_object(User, 1)  # direct fetch by id (can also use url)

In [5]: company = user.company  # fetch other objects as if using ORM (but this is remote - they are really proxies)

In [6]: company.apps  # fetch related objects by referencing the attribute (this is lazy - they are fetched now, not before)
Out[6]: 
[<lsproxies.models.App http://dev:8900/companies/100002/apps/1/ at 0x3888b10>,
 <lsproxies.models.App http://dev:8900/companies/100002/apps/3/ at 0x388c7d0>,
 <lsproxies.models.App http://dev:8900/companies/100002/apps/4/ at 0x388c910>,
 <lsproxies.models.App http://dev:8900/companies/100002/apps/5/ at 0x388c9d0>,
 <lsproxies.models.App http://dev:8900/companies/100002/apps/6/ at 0x388ca90>]

In [7]: new_app = App(name='my new app', company=company, platform="/platforms/ios/")  # make a new app (just like ORM)

In [8]: new_app.save()  # save the app (just like ORM)

In [9]: company.apps
Out[9]: 
[<lsproxies.models.App http://dev:8900/companies/100002/apps/1/ at 0x3888b10>,
 <lsproxies.models.App http://dev:8900/companies/100002/apps/3/ at 0x388c7d0>,
 <lsproxies.models.App http://dev:8900/companies/100002/apps/4/ at 0x388c910>,
 <lsproxies.models.App http://dev:8900/companies/100002/apps/5/ at 0x388c9d0>,
 <lsproxies.models.App http://dev:8900/companies/100002/apps/6/ at 0x388ca90>,
 <lsproxies.models.App http://dev:8900/companies/100002/apps/7/ at 0x388c590>]

In [10]: new_app
Out[10]: <lsproxies.models.App http://dev:8900/companies/100002/apps/7/ at 0x388c590>

The proxy objects can be manipulated in a shell or in code just like the original model objects.


Comments

There are currently no comments

New Comment

required

required (not published)

optional

Australia: 07 3103 2894

International: +61 410 545 357