Batching HTTP requests with httplib (Python 2.7)
Dwight Hutto
dwightdhutto at gmail.com
Fri Sep 14 06:56:30 EDT 2012
>> | The problem with most web services is that they require a list of
>> | sequential commands to be executed in a certain order to complete a
>> | given task (or at least the one I am using does) so having to manually
>> | call each command is a bit of a pain. How would you go about the design
>> | of a library to interact with these services?
>> |
>> | Any help is greatly appreciated :).
>>
>> Maybe I'm missing something. What's hard about:
>>
>> - wrapping the web services calls in a simple wrapper which
>> composes the call, runs it, and returns the result parts
>> This lets you hide all the waffle about the base URL,
>> credentials etc in the wrapper and only supply the essentials
>> at call time.
>>
>> - writing your workflow thing then as a simple function:
>>
>> def doit(...):
>> web_service_call1(...)
>> web_service_call2(...)
>> web_service_call3(...)
>>
>> with whatever internal control is required?
>>
>> This has worked for me for simple things.
>>
>> What am I missing about the larger context?
>>
>
> That is what I have at the moment but it is ugly as hell. I was wondering if
> there was a somewhat more elegant solution that I was missing.
> --
Well if the code works it works. There probably is a better way than
what I came up with:
def web_service_call0(y):
print "Service call = %i)" % y
def web_service_call1(y):
print "Service call = %i)" % y
def web_service_call2(y):
print "Service call = %i)" % y
def web_service_call3(y):
print "Service call = %i)" % y
def web_service_call4(y):
print "Service call = %i)" % y
service_num_list = [num for num in range(0,5)]
for service_num in service_num_list:
eval("web_service_call%i(%i)" % (service_num,service_num))
I just define the service calls, and iterate through them with an eval
that places in the service call num, but you would have to have a
matching list of the params that would go with each one.
I'm almost positive there will be some more well written ones on the way.
--
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
More information about the Python-list
mailing list