def _get_help(api_call_name): """ Generates the url and post body to get the API docstring from the Jinx API. Keyword arguments: api_call_name -- the name of the API call to retrieve documentation for """ if not isinstance(api_call_name, basestring): raise JinxInvalidRequestError("Argument is not a string: '%s'" % api_call_name) if self.interactive: self.auth() url = "https://%s/jinx/%s/%s?doc" %(self.jinx_host, self.api_version, api_call_name) json = jinx_json.dumps(list()) resp_code, resp_hdrs, resp = _post_data(url, json) # If the headers expire, re-auth. if resp_code == 401: self.auth_hdr = None resp_code, resp_hdrs, resp = _post_data(url, json) elif resp_code == 404: raise NameError("API call '%s' does not exist" % api_call_name) _check_resp_code(resp_code, resp_hdrs, resp) print resp return
def do_api_call(self, *args, **kwargs): """Performs the API call for this test case and returns the results. JSON encoding and decoding is handled behind the scenes. The decoded response data is stored in response.data (as opposed to response.content, which will hold the raw JSON blob). """ response = self.client.post( self.api_call_path, jinx_json.dumps(dict(args=args, kwargs=kwargs)), "application/json" ) if response.status_code == 200: self.assertEqual( response["Content-Type"], "application/json", "API call %s(%s) returned type %s instead of application/json" % (self.api_call_path, str(args), response["Content-Type"]), ) response.data = jinx_json.loads(response.content) else: response.data = response.content return response
def _post_json(self, path, data): """Perform a JSON-based post using the Django test client. The supplied data will be converted to JSON. The response will be returned verbatim, without being converted from JSON format. """ return self.client.post(path, jinx_json.dumps(data), "application/json")
def _call_jinx(*args, **kwargs): """Generates the url and post body to send. Keyword arguments: *args -- python tuple of arguments required for the API call **kwargs -- dict of keyword arguments required for the API call """ url = "https://%s/jinx/%s/%s" %(self.jinx_host, self.api_version, api_call) try: json = jinx_json.dumps({'args': list(args), 'kwargs': kwargs}) except TypeError, e: _be_verbose(e) raise JinxInvalidRequestError("Request arguments are in a non-serializable format: %s" % args)
def _post_data(url, json): """Sends the HTTP POST request and returns the (response code, response headers, response) as a tuple. Raises JinxInvalidRequestError if request fails. Keyword arguments: url -- the url to access the API json -- the post body written as a json blob """ curl = pycurl.Curl() resp = StringIO() resp_hdrs = StringIO() curl.setopt(pycurl.POST, True) curl.setopt(pycurl.URL, url) curl.setopt(pycurl.POSTFIELDS, json) curl.setopt(pycurl.WRITEFUNCTION, resp.write) curl.setopt(pycurl.HEADERFUNCTION, resp_hdrs.write) if self.auth_hdr: req_hdrs = ["Authorization: Negotiate %s" % self.auth_hdr, "Content-Type: application/json"] else: req_hdrs = ['Content-Type: application/json'] curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_GSSNEGOTIATE) curl.setopt(pycurl.USERPWD, ":") # Capture the Authorization: Negotiate header that libcurl # generates so that it can be reused for later connections. curl.setopt(pycurl.VERBOSE, True) # verbose is required to catch outgoing headers curl.setopt(pycurl.DEBUGFUNCTION, _set_auth_hdr) curl.setopt(pycurl.HTTPHEADER, req_hdrs) _be_verbose("Request URL -- %s" % url) _be_verbose("Request Headers -- %s" % req_hdrs) _be_verbose("Request POST Body -- %s" % json) if self.simulate: return (200, "", jinx_json.dumps(None)) try: curl.perform() except pycurl.error, e: raise JinxInvalidRequestError("Unable to request %s: %s" % (url, e))
def _test_serialize(self, data): self.assertEquals(jinx_json.loads(jinx_json.dumps(data)), data)
exception_traceback = traceback.format_exception(*sys.exc_info()) #print >> sys.stderr, "Unhandled exception from view:" #print >> sys.stderr, exception_traceback response = HttpResponseServerError(exception_traceback, mimetype='text/plain') response['X-Jinx-Error-Source'] = 'api' return response # Let the view return an HTTP response directly if it wants to, e.g. HttpResponseNotFound if isinstance(response_data, HttpResponse): response_data['X-Jinx-Error-Source'] = 'api' return response_data else: try: response_json = jinx_json.dumps(response_data) except TypeError, e: return HttpResponseServerError('%s returned unserializable data: %s' % (view.__name__, str(e))) return HttpResponse(response_json, mimetype='application/json') class JinxAuthorizationMiddleware(object): def __init__(self): self.principal_re = re.compile(r'^(?P<user>[^/@]+)(/(?P<cluster>[^@]+))?@(?P<realm>.*)$') def get_cluster_name(self): try: return self.cluster_name except AttributeError: try: