slumber icon indicating copy to clipboard operation
slumber copied to clipboard

add resource name in a string

Open adam-dziedzic opened this issue 13 years ago • 4 comments

My resource name contains hyphens (-). I can only use underscore (_) character for a name in Python. I do a hack like this:

PATH = 'http://localhost:8000/api/v1/' # (the real url should be: http://localhost:8000/api/v1/conf/) response_post = api.conf("pdus-oid2").post(pdu.dict) response_delete = api.conf("pdus-oid2/" + response_post["_id"]).delete()

whereas "pdus-oid2" is the name of the resource. Thus, I reckon you should add a possibility of adding a resource name in a string.

adam-dziedzic avatar Nov 02 '12 16:11 adam-dziedzic

Not a slumber dev, but I think this would be hard to do. Given slumber’s simple principle of converting Python attributes to URI path segments and using a call for invalid syntax (like integers), your solution sounds like the way to go.

IOW, I think base.thing('hyphen-dash').get() is as good as base.things(42).get()

In the spirit of OO path manipulation libraries, you could make the case for new syntax like base / things / 'hyphen-dash' and base / things / 42, i.e. overriding the division operator, but this looks more cute/wrong to me.

merwok avatar Jan 30 '13 16:01 merwok

This would be possible to do if the base API object became a resource in it's own right, then you could do api("pdus-oid2").post() and such, however I'm not sure offhand how that world behind that, api("pdus-oid2")(25).delete() looks kinda gnarly to me and non obvious.

I'm not particully able to have the time to implement this, but if you brought API inline with Resource in this capacity I would probably be willing to accept it.

dstufft avatar Feb 07 '13 17:02 dstufft

After I have wandered to this page, I have found my solution to this problem. Using getattr can do it. The

response = getattr(zmenteto_api.forms, "post-files").post(photo_json)

works for me just fine.

PetrDlouhy avatar Dec 15 '17 13:12 PetrDlouhy

Now I have found even better (much more readable) solution:

zmenteto_api.forms.__call__("post-files").post(photo_json)

It also solves problem with API endpoints on reserved keywords like delete.

PetrDlouhy avatar Feb 21 '19 13:02 PetrDlouhy