'Builds args struct out of HTTP request.'

def _GetArgsFromRequest(self, request, method_metadata, route_args):

    format_mode = GetRequestFormatMode(request, method_metadata)
    if (request.method in ['GET', 'HEAD']):
        if method_metadata.args_type:
            unprocessed_request = request.args
            if hasattr(unprocessed_request, 'dict'):
                unprocessed_request = unprocessed_request.dict()
            args = method_metadata.args_type()
            for type_info in args.type_infos:
                if (type_info.name in route_args):
                    self._SetField(args, type_info, route_args[type_info.name])
                elif (type_info.name in unprocessed_request):
                    self._SetField(args, type_info, unprocessed_request[type_info.name])
        else:
            args = None
    elif (request.method in ['POST', 'DELETE', 'PATCH']):
        try:
            args = method_metadata.args_type()
            for type_info in args.type_infos:
                if (type_info.name in route_args):
                    self._SetField(args, type_info, route_args[type_info.name])
            if request.content_type.startswith('multipart/form-data;'):
                payload = json.loads(request.form['_params_'])
                args.FromDict(payload)
                for (name, fd) in request.files.items():
                    args.Set(name, fd.read())
            elif (format_mode == JsonMode.PROTO3_JSON_MODE):
                args_proto = args.protobuf()
                json_format.Parse((request.get_data(as_text=True) or '{}'), args_proto)
                args.ParseFromString(args_proto.SerializeToString())
            else:
                payload = json.loads((request.get_data(as_text=True) or '{}'))
                if payload:
                    args.FromDict(payload)
        except Exception as e:
            logging.exception('Error    while    parsing    POST    request    %s    (%s):    %s', request.path, request.method, e)
            raise PostRequestParsingError(e)
    else:
        raise UnsupportedHttpMethod(('Unsupported    method:    %s.' % request.method))
    return args
