Revision 8b01f7f3
b/api/tests.py | ||
---|---|---|
2 | 2 |
# Copyright (c) 2010 Greek Research and Technology Network |
3 | 3 |
# |
4 | 4 |
|
5 |
from email.utils import parsedate |
|
6 |
from time import mktime |
|
7 |
|
|
5 | 8 |
import datetime |
6 | 9 |
|
7 | 10 |
from django.utils import simplejson as json |
... | ... | |
134 | 137 |
|
135 | 138 |
response = self.client.get('/api/v1.1/servers/detail') |
136 | 139 |
vms_from_api_initial = json.loads(response.content)['servers']['values'] |
137 |
then = datetime.datetime.now().isoformat().split('.')[0] + 'Z' |
|
138 |
|
|
139 |
#isoformat also gives miliseconds that are not needed |
|
140 |
response = self.client.get('/api/v1.1/servers/detail?changes-since=%s' % then) |
|
140 |
ts = mktime(parsedate(response['Date'])) |
|
141 |
since = datetime.datetime.fromtimestamp(ts).isoformat() + 'Z' |
|
142 |
response = self.client.get('/api/v1.1/servers/detail?changes-since=%s' % since) |
|
141 | 143 |
self.assertEqual(len(response.content), 0) |
142 |
#no changes were made |
|
143 | 144 |
|
144 | 145 |
#now create a machine. Then check if it is on the list |
145 | 146 |
request = { |
... | ... | |
158 | 159 |
response = self.client.post(path, json.dumps(request), content_type='application/json') |
159 | 160 |
self.assertEqual(response.status_code, 202) |
160 | 161 |
|
161 |
response = self.client.get('/api/v1.1/servers/detail?changes-since=%s' % then)
|
|
162 |
response = self.client.get('/api/v1.1/servers/detail?changes-since=%s' % since)
|
|
162 | 163 |
vms_from_api_after = json.loads(response.content)['servers']['values'] |
163 | 164 |
#make sure the newly created server is included on the updated list |
164 | 165 |
self.assertEqual(len(vms_from_api_after), 1) |
b/api/util.py | ||
---|---|---|
6 | 6 |
from functools import wraps |
7 | 7 |
from random import choice |
8 | 8 |
from string import ascii_letters, digits |
9 |
from time import time |
|
9 | 10 |
from traceback import format_exc |
11 |
from wsgiref.handlers import format_date_time |
|
10 | 12 |
|
11 | 13 |
from django.conf import settings |
12 | 14 |
from django.http import HttpResponse |
... | ... | |
122 | 124 |
raise BadRequest('Unsupported Content-Type.') |
123 | 125 |
|
124 | 126 |
|
127 |
def update_response_headers(request, response): |
|
128 |
if request.serialization == 'xml': |
|
129 |
response['Content-Type'] = 'application/xml' |
|
130 |
elif request.serialization == 'atom': |
|
131 |
response['Content-Type'] = 'application/atom+xml' |
|
132 |
else: |
|
133 |
response['Content-Type'] = 'application/json' |
|
134 |
|
|
135 |
response['Date'] = format_date_time(time()) |
|
136 |
|
|
125 | 137 |
def render_metadata(request, metadata, use_values=False, status=200): |
126 | 138 |
if request.serialization == 'xml': |
127 | 139 |
data = render_to_string('metadata.xml', {'metadata': metadata}) |
... | ... | |
148 | 160 |
data = json.dumps(d) |
149 | 161 |
|
150 | 162 |
resp = HttpResponse(data, status=fault.code) |
151 |
|
|
152 |
if request.serialization == 'xml': |
|
153 |
resp['Content-Type'] = 'application/xml' |
|
154 |
elif request.serialization == 'atom': |
|
155 |
resp['Content-Type'] = 'application/atom+xml' |
|
156 |
else: |
|
157 |
resp['Content-Type'] = 'application/json' |
|
158 |
|
|
163 |
update_response_headers(request, resp) |
|
159 | 164 |
return resp |
160 | 165 |
|
161 | 166 |
|
... | ... | |
197 | 202 |
raise BadRequest('Method not allowed.') |
198 | 203 |
|
199 | 204 |
resp = func(request, *args, **kwargs) |
200 |
if request.serialization == 'xml': |
|
201 |
resp['Content-Type'] = 'application/xml' |
|
202 |
elif request.serialization == 'atom': |
|
203 |
resp['Content-Type'] = 'application/atom+xml' |
|
204 |
else: |
|
205 |
resp['Content-Type'] = 'application/json' |
|
206 |
|
|
205 |
update_response_headers(request, resp) |
|
207 | 206 |
return resp |
208 | 207 |
|
209 | 208 |
except Fault, fault: |
Also available in: Unified diff