def check(self, action, version=None): """Check if the given action is supported in the given version. @raises APIError: If there's no method class registered for handling the given action or version. """ if action not in self._by_action: raise APIError( 400, "InvalidAction", "The action %s is not valid " "for this web service." % action) by_version = self._by_action[action] if None not in by_version: # There's no catch-all method, let's try the version-specific one if version not in by_version: raise APIError(400, "InvalidVersion", "Invalid API version.")
def test_with_str_message(self): """ L{APIError} will convert message to plain ASCII if converted to string. """ error = APIError(400, code="APIError", message="cittá") self.assertEqual("cittá", error.message) self.assertEqual("citt?", str(error))
def test_with_string_status(self): """ The L{APIError} constructor can be passed a C{str} as status code, and it will be converted to C{intp}. """ error = APIError("200", response="noes") self.assertEqual(200, error.status)
def _validate_generic_parameters(self, args): """Validate the generic request parameters. @param args: Parsed schema arguments. @raises APIError: In the following cases: - Action is not included in C{self.actions} - SignatureVersion is not included in C{self.signature_versions} - Expires and Timestamp are present - Expires is before the current time - Timestamp is older than 15 minutes. """ utc_now = self.get_utc_time() if getattr(self, "actions", None) is not None: # Check the deprecated 'actions' attribute if not args["action"] in self.actions: raise APIError( 400, "InvalidAction", "The action %s is not " "valid for this web service." % args["action"]) else: self.registry.check(args["action"], args["version"]) if not args["signature_version"] in self.signature_versions: raise APIError( 403, "InvalidSignature", "SignatureVersion '%s' " "not supported" % args["signature_version"]) if args["expires"] and args["timestamp"]: raise APIError( 400, "InvalidParameterCombination", "The parameter Timestamp cannot be used with " "the parameter Expires") if args["expires"] and args["expires"] < utc_now: raise APIError( 400, "RequestExpired", "Request has expired. Expires date is %s" % (args["expires"].strftime(self.time_format))) if (args["timestamp"] and args["timestamp"] + timedelta(minutes=15) < utc_now): raise APIError( 400, "RequestExpired", "Request has expired. Timestamp date is %s" % (args["timestamp"].strftime(self.time_format)))
def parse(self, schema, strict=True): """Update C{args} and C{rest}, parsing the raw request arguments. @param schema: The L{Schema} the parameters must be extracted with. @param strict: If C{True} an error is raised if parameters not included in the schema are found, otherwise the extra parameters will be saved in the C{rest} attribute. """ self.args, self.rest = schema.extract(self._raw_params) if strict and self.rest: raise APIError( 400, "UnknownParameter", "The parameter %s is not " "recognized" % self.rest.keys()[0])
def get_method(self, call, *args, **kwargs): """Return the L{Method} instance to invoke for the given L{Call}. @param args: Positional arguments to pass to the method constructor. @param kwargs: Keyword arguments to pass to the method constructor. """ method_class = self.registry.get(call.action, call.version) method = method_class(*args, **kwargs) if not method.is_available(): raise APIError(400, "InvalidAction", "The action %s is not " "valid for this web service." % call.action) else: return method
def _validate_signature(self, request, principal, args, params): """Validate the signature.""" creds = AWSCredentials(principal.access_key, principal.secret_key) endpoint = AWSServiceEndpoint() endpoint.set_method(request.method) endpoint.set_canonical_host(request.getHeader("Host")) path = request.path if self.path is not None: path = "%s/%s" % (self.path.rstrip("/"), path.lstrip("/")) endpoint.set_path(path) params.pop("Signature") signature = Signature(creds, endpoint, params) if signature.compute() != args.Signature: raise APIError(403, "SignatureDoesNotMatch", "The request signature we calculated does not " "match the signature you provided. Check your " "key and signing method.")
def _validate_principal(self, principal, args): """Validate the principal.""" if principal is None: raise APIError( 401, "AuthFailure", "No user with access key '%s'" % args.AWSAccessKeyId)
def fail_execute(call): raise APIError(400, code="LangError", message=u"\N{HIRAGANA LETTER A}dvanced")
def fail_execute(call): raise APIError(500, response="oops")