Created
September 20, 2012 14:23
-
-
Save mschmulen/3756244 to your computer and use it in GitHub Desktop.
ACS Python push content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash/env python | |
from urlparse import urljoin | |
import requests | |
import json | |
ACS_BASE_URI = 'http://api.cloud.appcelerator.com/v1/' | |
class API(object): | |
def __init__(self, key, username, password): | |
self.key = key | |
self.username = username | |
self.password = password | |
self.cookies = {} | |
def users(self, method): | |
http_method = 'GET' | |
if method == 'login': | |
uri = urljoin(ACS_BASE_URI, 'users/login.json') | |
http_method = 'POST' | |
args = {'key': self.key, 'login': self.username, 'password': | |
self.password} | |
r = self._call(http_method, uri, args) | |
#print 'yack yack' | |
if r.status_code == 200: | |
self.cookies = r.cookies | |
else: | |
raise Exception('invalid status code: ' + str(r.status_code) ) | |
elif method == 'create': | |
uri = urljoin(ACS_BASE_URI, 'users/create.json') | |
http_method = 'POST' | |
else: | |
raise Exception('method not supported') | |
return r | |
def objects(self, method, args): | |
http_method = 'GET' | |
if method == 'show': | |
pass | |
elif method == 'query': | |
uri = urljoin(ACS_BASE_URI, 'objects/' + args['class_name'] + | |
'/query.json') | |
params = {'key': self.key} | |
r = self._call(http_method, uri, params) | |
if r.status_code == 200: | |
return json.loads(r.text)['response'][args['class_name']] | |
elif method == 'create': | |
http_method = 'POST' | |
uri = urljoin(ACS_BASE_URI, 'objects/' + args['class_name'] + | |
'/create.json') | |
params = {'key': self.key, 'fields': json.dumps(args['fields'])} | |
r = self._call(http_method, uri, params) | |
else: | |
raise Exception('method not supported') | |
if r.status_code == 200: | |
return r | |
else: | |
raise Exception('invalid response code') | |
def _call(self, method, path, args): | |
if method == 'GET': | |
r = requests.get(path, params=args, cookies=self.cookies) | |
elif method == 'POST': | |
r = requests.post(path, params=args, cookies=self.cookies) | |
elif method == 'PUT': | |
r = requests.put(path, params=args, cookies=self.cookies) | |
elif method == 'DELETE': | |
r = requests.delete(path, params=args, cookies=self.cookies) | |
else: | |
raise Exception('invalid method') | |
return r | |
if __name__ == '__main__': | |
# Login to ACS | |
appc = acs.API(key='Yx4ft1VerijgFmM3tVcQmXSXsCtUSWby', username='mschmulen', password='appc') | |
appc.users('login') | |
appc.objects('create', {'class_name': class_name, 'fields': fields}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment