maxcdn / python-maxcdn Goto Github PK
View Code? Open in Web Editor NEWThis project forked from netdna/netdnarws
A Python REST Client for MaxCD REST Web Services
This project forked from netdna/netdnarws
A Python REST Client for MaxCD REST Web Services
Remove the forked version of requests, in favor of the original request. This should also fix #1, as it seems that requests_netdna
has 3.x bugs that aren't in the latest requests
module.
see: #17
It was closed due to inactivity.
see: #16
It was closed due to inactivity.
Hi
I am trying to execute the reseller usage API for specific dates, and it is not working well.
The following code is an attempt to fetch July data, but all it does is always get the current month to date. Either I am not doing something right with this API, or there is a problem with the endpoint.
My code:
#!/usr/bin/env python
import pprint as pp
from textwrap import dedent
from maxcdn import MaxCDN
import json
alias="globaldots"
key="this is my key"
secret="this is my secret"
from_date="2016-07-01"
to_date="2016-07-31"
# report="/monthly"
report=""
date_str="from_date=%s&to_date=%s" % (from_date, to_date)
params={"from_date":from_date, "to_date" : to_date}
maxcdn = MaxCDN(alias, key, secret)
# Get my clients
clients = maxcdn.get("/clients.json")
i=2
clientid=clients["data"]["accounts"][i]["id"]
clientname=clients["data"]["accounts"][i]["alias"]
clientstatus=clients["data"]["accounts"][i]["status"]
stats_url="/clients/%s/reports/stats.json%s" % (clientid, report)
# Version 1: pass a dictionary of dates
client_stats=maxcdn.get(stats_url, params)
print("Version 1: pass a dictionary of dates")
print json.dumps(client_stats)
print(" ")
# Version 2: pass a query string of dates
client_stats=maxcdn.get(stats_url, params=date_str)
print("Version 2: pass a query string of dates")
print json.dumps(client_stats)
print(" ")
# Version 3: Do not pass any parameters
client_stats=maxcdn.get(stats_url)
print("Version 3: Do not pass any parameters")
print json.dumps(client_stats)
print(" ")
The results are identical - month to date - no matter how I pass the dates, and what type of report I run.
Version 1: pass a dictionary of dates
{"code": 200, "data": {"total": "1", "stats": {"cache_hit": "698300293", "noncache_hit": "1899", "hit": "698302192", "size": "8331462444846"}}}
Version 2: pass a query string of dates
{"code": 200, "data": {"total": "1", "stats": {"cache_hit": "698300293", "noncache_hit": "1899", "hit": "698302192", "size": "8331462444846"}}}
Version 3: Do not pass any parameters
{"code": 200, "data": {"total": "1", "stats": {"cache_hit": "698300293", "noncache_hit": "1899", "hit": "698302192", "size": "8331462444846"}}}
I was working on debugging one reported issue with python fetching raw logs via API and found that you are missing definition in get method for body:
def get(self, end_point, *_kwargs):
return self.client.get(self._get_url(end_point),
headers=self._get_headers(json=False),
*_kwargs).json()
should contain "data=None":
def get(self, end_point, data=None, *_kwargs):
return self.client.get(self._get_url(end_point),
headers=self._get_headers(json=False),
*_kwargs).json()
otherwise, exception is thrown as:
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/maxcdn/maxcdn.py", line 40,
in get
*_kwargs).json()
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 468,
in get
return self.request('GET', url, *_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 422,
in request
prep = self.prepare_request(req)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 360,
in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/usr/lib/python2.7/dist-packages/requests/models.py", line 298, in
prepare
self.prepare_auth(auth, url)
File "/usr/lib/python2.7/dist-packages/requests/models.py", line 475, in
prepare_auth
r = auth(self)
File
"/usr/local/lib/python2.7/dist-packages/requests_oauthlib/oauth1_auth.py",
line 79, in call
unicode(r.url), unicode(r.method), r.body or '', r.headers)
File
"/usr/local/lib/python2.7/dist-packages/oauthlib/oauth1/rfc5849/init.py",
line 287, in sign
raise ValueError('GET/HEAD requests should not include body.')
ValueError: GET/HEAD requests should not include body.
I'm having a transient issue with the library. Every so often, the get
call below fails. Waiting a few seconds and retrying to call usually fixes it so I suspect the remote server is responding with something unexpected. Clients can easily catch and retry, but it would be nice if the library gracefully failed and gave consumers a clearer picture of the cause so they can adapt appropriately.
start=(datetime.datetime.now() - datetime.timedelta(hours=1)).isoformat()
params = urllib.urlencode({'start': start})
res = api.get("/v3/reporting/logs.json?{0}".format(params))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/maxcdn/maxcdn.py", line 40, in get
**kwargs).json()
File "/Library/Python/2.7/site-packages/requests/models.py", line 797, in json
return json.loads(self.text, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 383, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
See node-maxcdn
and ruby-maxcdn
examples for source.
This results in the latest (0.0.6) version of maxcdn not being pip installable.
Currently when you do a 'pip install maxcdn' it will only install version 0.0.3 as the tar file is not available for the installer.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.