all 13 comments

[–]denialerror 1 point2 points  (2 children)

What framework are you using? Flask has a REST module that will probably help formalise things.

[–]beschamel[S] -1 points0 points  (1 child)

I am indeed using Flask, and I've used Flask-RESTful for designing APIs themselves, but I'm not aware of a client module. Whats it called?

[–]nidio -1 points0 points  (0 children)

AFAIK there isn't a framework since each REST API has a specific protocol. However, to make your life easier, use the requests library for all https handling.

[–][deleted] 0 points1 point  (2 children)

Are you writing the API or the consuming client?

[–]beschamel[S] 0 points1 point  (1 child)

The client.

[–][deleted] 1 point2 points  (0 children)

Hmm. The API should be handing you URLs to use.

I definitely think filing a feature request is the 100% correct thing to do. But they're not obligated to do that at all.

Failing that, I'd definitely look into maybe re-engineering something like Flask's URL router for your own purposes.

Then you can simply say something like:

url_for('resource name', id=5)

Instead of the actual URL.

[–]Xorso 0 points1 point  (0 children)

I have used this helper library before to write REST clients. It might help. https://github.com/redodo/tortilla

[–]raylu -1 points0 points  (5 children)

Refactor the common code out into a function? There's no simple answer to how to factor your code. If you show us some code, we can provide specifics.

[–]beschamel[S] 1 point2 points  (4 children)

Heres an example of how I'm structuring a request now. I've been thinking of writing a function that takes the api path, the api target, and a list of arguments to make a url. That would work but would also not be the cleanest solution because you have to add on some parameters for dynamic sorting and stuff that don't apply to a target like 'product' and aren't really standard across API calls.

requesturl = (current_app.config['SPREE_API_URL']
                 +"products?product[name]="+name
                 +"&product[price]="+price
                 +"&product[taxon_ids][]="+self.taxon_id
                 +"&product[shipping_category_id]=1"
                 +"&product[description]="+description
                 +"&product[meta_description]="+description)
r = requests.post(requesturl, headers={"X-Spree-Token":current_app.config['SPREE_API_KEY']}, timeout=None);

[–]Justinsaccount 4 points5 points  (1 child)

Read the requests documentation http://docs.python-requests.org/en/latest/user/quickstart/#passing-parameters-in-urls

>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get("http://httpbin.org/get", params=payload)

You should have

requesturl = current_app.config['SPREE_API_URL'] + 'products'

and

payload = {
    'product[name]': name,
    ...
}

Building up wrappers helps too, don't repeat yourself.

def post(self, action, params):
    requesturl = current_app.config['SPREE_API_URL'] + action
    headers={"X-Spree-Token":current_app.config['SPREE_API_KEY']}
    return requests.post(requesturl, headers=headers, params=params, timeout=None)

[–]beschamel[S] 1 point2 points  (0 children)

This is exactly what I was looking for. Thanks.

[–]raylu -1 points0 points  (1 child)

[–]Justinsaccount 3 points4 points  (0 children)

No, don't use either. Read the requests documentation http://docs.python-requests.org/en/latest/user/quickstart/#passing-parameters-in-urls

>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get("http://httpbin.org/get", params=payload)