Exemplo n.º 1
0
    async def mask_request(self, method, uri, params={}, body={}, **kwargs):
        '''
        uri: 接口URI `/gettoken`, `/send`
        method: 方法, GET, POST ....
        params: 追加参数
        body: post 体, 格式为数组 {} # POST的时候才有用
        '''
        url = self.base_url % {'uri': uri}

        client_session = aiohttp.ClientSession(raise_for_status=True)
        async with client_session as session:
            if method == 'GET':

                resp = await client_session.request(method=method, url=url, params=params)
            elif method == 'POST':

                resp = await client_session.request(method=method, url=url, params=params, json=body)
            else:

                raise HttpBadRequest('not validate method')

        async with resp:
            if resp.status != 200:
                raise HttpBadRequest({'status': resp.status})

            resp_json = await resp.json()

            if resp_json['errcode'] > 0:
                raise HttpBadRequest(resp_json)

            return resp_json
Exemplo n.º 2
0
async def _form_data_request_parser(request: web.Request) -> Dict[str, Any]:
    """Parse a multipart/form-data content type request.

    :param request: incoming aiohttp request
    :type request: web.Request
    :return: GraphQL params extracted from the request
    :rtype: Dict[str, Any]
    :raise HttpBadRequest: if file paths are invalid
    """
    data = await request.post()

    operations, files_map = _extract_multipart_params(data)

    for field_name, paths in files_map.items():
        if not isinstance(paths, list):
            raise HttpBadRequest(
                "Invalid type for the < map > multipart field entry key "
                f"< {field_name} > array."
            )

        for index, path in enumerate(paths):
            if not isinstance(path, str):
                raise HttpBadRequest(
                    "Invalid type for the < map > multipart field entry key "
                    f"< {field_name} > array index < {index} > value."
                )

            operations = _inject_file_to_operations(
                operations, data.get(field_name), path.split("."), path
            )

    return operations
Exemplo n.º 3
0
def _inject_file_to_operations(
    operations: Union[Dict[str, Any], List[Any]],
    file_instance: FileField,
    current_path: List[str],
    full_path: str,
) -> Union[FileField, Dict[str, Any]]:
    """Inject the file instance in operations according to the path.

    :param operations: operations on which inject the file
    :param file_instance: the file instance to inject
    :param current_path: current path to follow to inject the file
    :param full_path: the raw full path used
    :type operations: Union[Dict[str, Any], List[Any]]
    :type file_instance: FileField
    :type current_path: List[str]
    :type full_path: str
    :return: the operations to update or the file instance
    :rtype: Union[FileField, Dict[str, Any]]
    :raise HttpBadRequest: if the operations doesn't fit with the path
    """
    if not current_path:
        if operations is not None:
            raise HttpBadRequest(
                f"Path < {full_path} > in < map > multipart field doesn't "
                f"lead to a null value."
            )
        return file_instance

    key = current_path[0]

    try:
        index = int(key)
    except ValueError as e:
        if not isinstance(operations, dict):
            raise HttpBadRequest(
                f"Key < {key} > from path < {full_path} > doesn't lead to an "
                "object."
            ) from e
        operations[key] = _inject_file_to_operations(
            operations[key], file_instance, current_path[1:], full_path
        )
    else:
        if not isinstance(operations, list):
            raise HttpBadRequest(
                f"Index < {index} > from path < {full_path} > doesn't lead to "
                "a list."
            )
        operations[index] = _inject_file_to_operations(
            operations[index], file_instance, current_path[1:], full_path
        )
    return operations
async def handle_graphql(request: web.Request) -> web.Response:
    """GraphQL service response handler.

    :param request: incoming aiohttp request
    :type request: web.Request
    :return: a GraphQL response
    :rtype: web.Response
    :raise HttpBadRequest: if none query was provided
    """
    try:
        query, variables, operation_name = await extract_graphql_params(
            request
        )
        if not query:
            raise HttpBadRequest("Must provide query string.")
        return _prepare_response(
            await request.app["graphql_engine"].execute(
                query,
                operation_name=operation_name,
                variables=variables,
                context=context_factory(request),
            )
        )
    except Exception as e:  # pylint: disable=broad-except
        return _prepare_response(
            {"errors": [format_error(e)]},
            status=(
                e.code  # pylint: disable=no-member
                if isinstance(e, HttpProcessingError)
                and e.code  # pylint: disable=no-member
                else 500
            ),
        )
Exemplo n.º 5
0
async def get_raw_hash(request):
    item_hash = request.match_info.get("hash", None)

    try:
        engine = ItemType.from_hash(item_hash)
    except UnknownHashError:
        raise HttpBadRequest(message="Invalid hash")

    if item_hash is not None:
        value = await get_hash_content(
            item_hash,
            use_network=False,
            use_ipfs=True,
            engine=engine,
            store_value=False,
        )

        if value is not None and value != -1:
            response = web.Response(body=value)
            response.enable_compression()
            return response
        else:
            raise web.HTTPNotFound(text="not found")
    else:
        raise web.HTTPBadRequest(text="no hash provided")
Exemplo n.º 6
0
    async def dispatch(self, request: Request):
        """

        :param request:
        :return:
        """
        method = self.methods.get(request.method.upper())
        if not method:
            raise HTTPMethodNotAllowed('', SUPPORTED_METHODS)

        wanted_args = list(inspect.signature(method).parameters.keys())
        available_args = request.match_info.copy()

        for aa in available_args:
            if aa not in wanted_args:
                raise Exception(
                    'REST endpoint method {0} should be able to process URL "{1}" parameter.'
                    .format(request.method.upper(), aa))

        available_args.update({'request': request})

        unsatisfied_args = set(wanted_args) - set(available_args.keys())
        if unsatisfied_args:
            # Expected match info that doesn't exist
            raise HttpBadRequest('')

        return await method(
            **{arg_name: available_args[arg_name]
               for arg_name in wanted_args})
Exemplo n.º 7
0
def _parse_graphql_params(
    body_data: Dict[str, Any], url_data: Dict[str, Any]
) -> Tuple[str, Optional[Dict[str, Any]], Optional[str]]:
    """Validate and extract GraphQL params from request body or URL.

    :param body_data: data extracted from the body request
    :param url_data: data extracted from the query string
    :type body_data: Dict[str, Any]
    :type url_data: Dict[str, Any]
    :return: a tuple containing the query, variables and operation name
    :rtype: Tuple[str, Optional[Dict[str, Any]], Optional[str]]
    :raise HttpBadRequest: if the filled in variables are invalid JSON
    """
    query = url_data.get("query") or body_data.get("query")
    if not isinstance(query, str):
        query = None

    variables = url_data.get("variables") or body_data.get("variables")
    if variables and isinstance(variables, str):
        try:
            variables = json.loads(variables)
        except Exception as e:
            raise HttpBadRequest("Variables are invalid JSON.") from e
    elif not isinstance(variables, dict):
        variables = None

    operation_name = url_data.get("operationName") or body_data.get(
        "operationName"
    )
    if not isinstance(operation_name, str):
        operation_name = None

    return (query, variables, operation_name)
Exemplo n.º 8
0
async def _json_request_parser(request: web.Request) -> Dict[str, Any]:
    """Parse an application/json content type request.

    :param request: incoming aiohttp request
    :type request: web.Request
    :return: GraphQL params extracted from the request
    :rtype: Dict[str, Any]
    :raise HttpBadRequest: if the body is an invalid JSON
    """
    try:
        return await request.json()
    except Exception as e:
        raise HttpBadRequest(message="POST body sent invalid JSON.") from e
Exemplo n.º 9
0
    def get(self, request, *args, **kwargs):
        if "code" not in request.GET:
            raise HttpBadRequest("Wrong code")

        payload = {
            "code": request.GET["code"],
            "client_id": settings.SLACK_CLIENT_ID,
            "client_secret": settings.SLACK_CLIENT_SECRET
        }

        try:
            client = slack.WebClient()
            response = client.oauth_v2_access(**payload)
        except slack.errors.SlackApiError as e:
            raise HttpBadRequest("Invalid code")

        if not response["ok"]:
            print("E] processing failed...")
            pprint(response.data)
            return HttpResponse("failed")

        authed_user = response["authed_user"]
        team = response["team"]

        token, _ = USER_MODEL.objects.update_or_create(
            user=authed_user["id"],
            defaults={
                "token": authed_user["access_token"],
                "team_id": team["id"],
                "team_name": team["name"],
            }
        )
        slack_events.emit("oauth", token)

        if hasattr(settings, "SLACK_AFTER_OAUTH") and settings.SLACK_AFTER_OAUTH:
            return HttpResponseRedirect(settings.SLACK_AFTER_OAUTH)

        return HttpResponseRedirect(reverse("oauth_done"))
Exemplo n.º 10
0
    async def dispatch(self, request: Request):
        method = self.methods.get(request.method.upper())
        if not method:
            raise HTTPMethodNotAllowed('', DEFAULT_METHODS)

        wanted_args = list(inspect.signature(method).parameters.keys())
        available_args = request.match_info.copy()
        available_args.update({'request': request})

        unsatisfied_args = set(wanted_args) - set(available_args.keys())
        if unsatisfied_args:
            raise HttpBadRequest('')

        return await method(**{arg_name: available_args[arg_name] for arg_name in wanted_args})
Exemplo n.º 11
0
async def rm_client_for_app(request: aiohttp.web.Request):
    try:
        app_id = request.query["app_id"]
    except KeyError as exc:
        raise HttpBadRequest("No key " + str(exc))

    app = request.app
    if app_id not in app["LOADERS"]:
        return aiohttp.web.json_response({"error": "client does not exist"},
                                         status=404)

    del app["LOADERS"][app_id]
    app["LOADER_TASKS"][app_id].cancel()
    del app["LOADER_TASKS"][app_id]
    return aiohttp.web.json_response(
        {"status": f"client for {app_id} deleted"})
Exemplo n.º 12
0
    async def dispatch(self, request: Request):
        """
        This is the method being called when the user sends the request
        """
        method = self.methods.get(request.method.upper())
        if not method:
            raise HTTPMethodNotAllowed('', DEFAULT_METHODS)

        wanted_args = list(inspect.signature(method).parameters.keys())
        available_args = request.match_info.copy()
        available_args.update({'request': request})

        unsatisfied_args = set(wanted_args) - set(available_args.keys())
        if unsatisfied_args:
            # Expected match info that doesn't exist
            raise HttpBadRequest('')

        return await method(**{arg_name: available_args[arg_name] for arg_name in wanted_args})
Exemplo n.º 13
0
    async def dispatch(self, request: Request):
        method = self.methods.get(request.method.upper())
        if not method:
            raise HTTPMethodNotAllowed("", DEFAULT_METHODS)

        wanted_args = list(inspect.signature(method).parameters.keys())

        available_args = request.match_info.copy()
        available_args["request"] = request

        unsatisfied_args = set(wanted_args) - set(available_args.keys())
        if unsatisfied_args:
            # Expected match info that doesn't exist
            raise HttpBadRequest("")

        return await method(
            **{arg_name: available_args[arg_name]
               for arg_name in wanted_args})
Exemplo n.º 14
0
    async def dispatch(self, request: web.Request):
        method = self.methods.get(request.method.upper())
        if not method:
            raise HTTPMethodNotAllowed('', DEFAULT_METHODS)

        wanted_args = list(inspect.signature(method).parameters.keys())
        available_args = request.match_info.copy()
        available_args.update({'request': request})

        unsatisfied_args = set(wanted_args) - set(available_args.keys())
        if unsatisfied_args:
            raise HttpBadRequest('')

        try:
            return await method(**{
                arg_name: available_args[arg_name]
                for arg_name in wanted_args
            })
        except Exception as ex:
            template = "Server error, an exception of type {0} occurred. Arguments:{1!r}"
            message = template.format(type(ex).__name__, ex.args)
            return web.Response(status=500,
                                body=json.dumps({message: 500}),
                                content_type='application/json')
Exemplo n.º 15
0
def _extract_multipart_params(
    data: MultiDictProxy,
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
    """Validate and extract the operations and map fields from the data.

    :param data: the data from which extract fields
    :type data: MultiDictProxy
    :return: the operations and map fields
    :rtype: Tuple[Dict[str, Any], Dict[str, Any]]
    :raise HttpBadRequest: if missing or invalid fields
    """
    if "operations" not in data:
        raise HttpBadRequest("Missing multipart field < operations >.")

    if "map" not in data:
        raise HttpBadRequest("Missing multipart field < map >.")

    try:
        operations = json.loads(data["operations"])
    except Exception as e:
        raise HttpBadRequest(
            "Invalid JSON in the < operations > multipart field."
        ) from e
    else:
        if not isinstance(operations, dict):
            raise HttpBadRequest(
                "Invalid type for the < operations > multipart field."
            )

    try:
        files_map = json.loads(data["map"])
    except Exception as e:
        raise HttpBadRequest(
            "Invalid JSON in the < map > multipart field."
        ) from e
    else:
        if not isinstance(files_map, dict):
            raise HttpBadRequest(
                "Invalid type for the < map > multipart field."
            )

    return operations, files_map
Exemplo n.º 16
0
 def check_config(self):
     if all([self._corpecret, self._corpid, self._agent_id]) == False:
         raise HttpBadRequest('config error')