Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #96828

Re: Writing a module to abstract a REST api

Path csiph.com!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <srkunze@mail.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'static': 0.03; 'api.': 0.04; 'classes,': 0.05; 'attributes': 0.07; 'classes.': 0.07; 'api': 0.09; '(without': 0.09; 'here?': 0.09; 'http,': 0.09; 'objects.': 0.09; 'subject:Writing': 0.09; 'subject:module': 0.09; 'thread': 0.10; 'python': 0.10; 'skip:f 30': 0.15; 'forth.': 0.16; 'globals.': 0.16; 'subject:api': 0.16; 'up*': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'basically': 0.18; 'to:name:python- list@python.org': 0.20; 'to:2**1': 0.21; 'aspect': 0.22; 'object.': 0.22; 'referring': 0.22; 'tuples': 0.22; 'code.': 0.23; "python's": 0.23; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; 'example': 0.26; '(which': 0.26; 'rest': 0.26; 'separate': 0.27; 'skip:( 20': 0.28; 'fine': 0.28; 'looks': 0.29; 'container': 0.29; 'initialized': 0.29; 'queue': 0.29; 'workaround': 0.29; 'objects': 0.29; 'work.': 0.30; 'creating': 0.30; 'call.': 0.30; 'query': 0.30; 'option': 0.31; 'another': 0.32; 'holds': 0.32; 'returned': 0.32; 'point': 0.33; 'class': 0.33; 'problem': 0.33; 'foo': 0.33; 'http': 0.33; 'correctly': 0.34; 'file': 0.34; 'except': 0.34; 'handle': 0.34; 'received:10.0': 0.34; 'skip:d 20': 0.34; 'add': 0.34; 'so,': 0.35; 'best,': 0.35; 'instance': 0.35; 'programming.': 0.35; 'protocol': 0.35; 'knowledge': 0.35; 'quite': 0.35; 'something': 0.35; 'remote': 0.35; 'skip:i 20': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'starting': 0.37; 'itself': 0.38; 'represent': 0.38; 'data': 0.39; 'build': 0.40; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'called': 0.40; 'ever': 0.60; 'provide': 0.61; 'per': 0.62; 'here.': 0.62; 'charset:windows-1252': 0.62; 'safe': 0.63; 'dont': 0.64; 'services.': 0.72; 'facilities': 0.72; 'capability': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/simple; d=mail.de; s=mail201212; t=1442598712; bh=rHAPOg8/Y4L8Kk8pi0rwdbiyBhwLOkqPGfPyNSqmyio=; h=Date:From:To:Subject:References:In-Reply-To:From; b=FozxLVtzn4AJn5QZr4EUR5PY1KoxOkykQ+HuZPsUabYdWJSs2ka170fXxznsGSNxu y4OC72B/JsIv+pZgMLcJdZS9Z8SXU5OHpa36du9/0n66XtROnYW60NaNlsFa8Ld8as Heh7BU7znL/tZYFJg9MFxmtg0qltk1drzEpvlz1A=
Date Fri, 18 Sep 2015 19:51:48 +0200
From "Sven R. Kunze" <srkunze@mail.de>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0
MIME-Version 1.0
To "Joseph L. Casale" <jcasale@activenetwerx.com>, "python-list@python.org" <python-list@python.org>
Subject Re: Writing a module to abstract a REST api
References <55FB2A8F.9050501@mail.de> <1442590107897.9135@activenetwerx.com>
In-Reply-To <1442590107897.9135@activenetwerx.com>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-purgate clean
X-purgate This mail is considered clean (visit http://www.eleven.de for further information)
X-purgate-type clean
X-purgate-Ad Categorized by eleven eXpurgate (R) http://www.eleven.de
X-purgate This mail is considered clean (visit http://www.eleven.de for further information)
X-purgate clean
X-purgate-size 2445
X-purgate-ID 154282::1442598712-00000778-F7639CD6/0/0
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.0.1442598714.21674.python-list@python.org> (permalink)
Lines 63
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1442598714 news.xs4all.nl 23750 [2001:888:2000:d::a6]:60431
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:96828

Show key headers only | View raw


On 18.09.2015 17:28, Joseph L. Casale wrote:
> So a design pattern I use often is to create Python objects to represent
> objects returned from what ever api I am abstracting. For example I
> might create named tuples for static data I dont intend to change or
> for an object I can both query for and create, I might build a class to
> represent it.
>
> 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.

> # 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.

> 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.

> 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").

> One option is to put everything in one module and use globals. If the handle is
> initialized it'll just work.
>
> Problem with that is its not thread safe (without work) and I subscribe to
> one object per file (bloody hard with Python's import/load procedure.

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*

Best,
Sven

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Writing a module to abstract a REST api "Sven R. Kunze" <srkunze@mail.de> - 2015-09-18 19:51 +0200

csiph-web