Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for adding static files to an app #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions pbclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,34 @@ def set(key, val):
_opts[key] = val


def _pybossa_req(method, domain, id=None, payload=None, params=None):
def _pybossa_req(method, domain, id=None, action=None, payload=None, params=None, files=None, asjson=True):
"""
Sends a JSON request

Returns True if everything went well, otherwise it returns the status code of the response
"""
headers = {'content-type': 'application/json'}
headers = {}
if asjson:
headers = {'content-type': 'application/json'}
url = _opts['endpoint'] + '/api/' + domain
if id is not None:
url += '/' + str(id)
if action is not None:
url += '/' + action
if params is None:
params = dict()
if 'api_key' in _opts:
params['api_key'] = _opts['api_key']
if payload is not None and asjson:
payload = json.dumps(payload)
if method == 'get':
r = requests.get(url, params=params)
elif method == 'post':
r = requests.post(url, params=params, headers=headers, data=json.dumps(payload))
r = requests.post(url, params=params, headers=headers, data=payload, files=files)
elif method == 'put':
r = requests.put(url, params=params, headers=headers, data=json.dumps(payload))
r = requests.put(url, params=params, headers=headers, data=payload)
elif method == 'delete':
r = requests.delete(url, params=params, headers=headers, data=json.dumps(payload))
r = requests.delete(url, params=params, headers=headers, data=payload)
#print r.status_code, r.status_code / 100
if r.status_code / 100 == 2:
if r.text and r.text != '""':
Expand Down Expand Up @@ -199,6 +205,11 @@ def update_app(app):
except:
raise

def add_file(app, srcfile, dstname):
return _pybossa_req('post', 'app', app.id, "addfile",
params={'filename':dstname},
files={'file': srcfile},
asjson=False)

def delete_app(app_id):
"""Deletes an Application with id = app_id
Expand Down