Commits

Shu Zong Chen committed 4ac18c0

initial api access method works

  • Participants
  • Parent commits e2af6c4

Comments (0)

Files changed (10)

 build/*
 dist/*
 django_snippet.egg-info/*
+*.local

File apollo/clients/requests.py

Empty file removed.

File apollo/clients/requestsclient.py

+import requests
+import json
+
+from functools import partial
+
+from apollo.response import ApolloResponse
+
+class ApolloRequestsClient(object):
+	endpoint = "https://api.apollohq.com/beta/"
+
+	def __init__(self, apikey):
+		self.apikey = apikey
+
+	def __fetch(self, requests_method, okay_code, method, data=None, **kwargs):
+		url = self.endpoint + method.format(**kwargs)
+		if data is not None:
+			data = json.dumps(data)
+		r = requests_method(url, auth=(self.apikey, 'x'), data=data)
+		success = ApolloResponse.SUCCESS if r.status_code == okay_code else ApolloResponse.FAIL
+		if success != ApolloResponse.SUCCESS:
+			print ""
+			print url
+			print r.text
+			print ""
+		return ApolloResponse(success, r.text)
+
+	def get(self, method, data=None, **kwargs):
+		return self.__fetch(
+			requests.get,
+			requests.codes.ok,
+			method,
+			data = data,
+			**kwargs
+		)
+
+	def post(self, method, data=None, **kwargs):
+		return self.__fetch(
+			requests.post,
+			requests.codes.created,
+			method,
+			data = data,
+			**kwargs
+		)
+	
+	def delete(self, method, data=None, **kwargs):
+		return self.__fetch(
+			requests.delete,
+			requests.codes.ok,
+			method,
+			data = data,
+			**kwargs
+		)
+
+	def put(self, method, data=None, **kwargs):
+		return self.__fetch(
+			requests.put,
+			requests.codes.ok,
+			method,
+			data = data,
+			**kwargs
+		)
+
+

File apollo/clients/tornado.py

Empty file removed.

File apollo/clients/tornadoclient.py

Empty file added.

File apollo/response.py

+import json
+
+class ApolloResponse(object):
+	SUCCESS = 0x01
+	FAIL = 0x02
+
+	def __init__(self, status, rawdata):
+		self.status = status
+		self.rawdata = rawdata
+		try:
+			self.data = json.loads(rawdata)
+		except ValueError:
+			self.data = None
+			self.status = ApolloResponse.FAIL
+
+	@property
+	def success(self):
+		return self.status == ApolloResponse.SUCCESS
+
+	@property
+	def fail(self):
+		return self.status == ApolloResponse.FAIL

File test.py

-import apollo
+export APOLLOAPIKEY=`cat credentials.local`
+python -m unittest discover tests

File tests/__init__.py

Empty file added.

File tests/test_apollo.py

-import sys
 import os
-sys.path.insert(0, os.path.abspath('..'))
 
 import unittest
+import pprint
 
 import apollo
+from apollo.response import ApolloResponse
+from apollo.clients.requestsclient import ApolloRequestsClient
+
+
+APIKEY = os.environ['APOLLOAPIKEY']
+
+class BasicApolloTest(unittest.TestCase):
+	def test_construct_client(self):
+		ApolloRequestsClient(APIKEY)
+
+class BasicApolloRequestsTest(unittest.TestCase):
+	def setUp(self):
+		self.apollo = ApolloRequestsClient(APIKEY)
+	
+	def test_get_projects(self):
+		result = self.apollo.get("projects")
+		self.assertEqual(result.status, ApolloResponse.SUCCESS)
+
+	def test_get_account(self):
+		return
+		result = self.apollo.get("account")
+		self.assertEqual(result.status, ApolloResponse.SUCCESS)
+
+class TestApolloAccess(unittest.TestCase):
+	PROJECT_NAME = "API_TEST"
+
+	def setUp(self):
+		self.apollo = ApolloRequestsClient(APIKEY)
+	
+	def test_evarthing(self):
+		r = self.apollo.get("projects")
+		projects = dict((i['name'], i) for i in r.data['projects'])
+
+		if self.PROJECT_NAME not in projects:
+			# CREATE TEST PROJECT
+			r = self.apollo.post(
+				"projects",
+				data = {
+					"project": {
+						"name": self.PROJECT_NAME,
+						"overview-page-announcement": "Testing API",
+						"no-email-notification": 0,
+					}
+				}
+			)
+			self.assertEqual(r.status, ApolloResponse.SUCCESS)
+
+			r = self.apollo.get("projects")
+			projects = dict((i['name'], i) for i in r.data['projects'])
+			self.assertEqual(self.PROJECT_NAME in projects, True)
+
+		project_id = projects[self.PROJECT_NAME]['id']
+
+		# MAKE TASKLIST
+		r = self.apollo.post(
+			"projects/{project_id}/taskLists",
+			project_id = project_id,
+			data = {
+				"task-list": {
+					"name": "Test List",
+					"description": "This is a test list",
+				}
+			}
+		)
+		self.assertEqual(r.status, ApolloResponse.SUCCESS)
+		tasklist_id = r.data['task-list-id']
+
+		# MODIFY TASKLIST
+		r = self.apollo.put(
+			"taskLists/{tasklist_id}",
+			tasklist_id = tasklist_id,
+			data = {
+				"task-list": {
+					"name": "Modified Test List",
+				}
+			}
+		)
+		self.assertEqual(r.status, ApolloResponse.SUCCESS)
+
+		# DELETE TASKLIST
+		r = self.apollo.delete(
+			"taskLists/{tasklist_id}",
+			tasklist_id = tasklist_id,
+		)
+		self.assertEqual(r.status, ApolloResponse.SUCCESS)
 
 if __name__ == '__main__':
 	unittest.main()