Ejemplo n.º 1
0
        def func(*a, **kw):
            ip = get_remote_address()

            if flood.is_banned(ip):
                abort(403, description="Too many request limits violations")
            else:
                if flood.has_violation(ip):
                    flood.decrease(ip)

            if args.api_keys:
                ak = get_req_api_key()
                if (ak and api_keys_db.lookup(ak) is None):
                    abort(
                        403,
                        description="Invalid API key",
                    )
                elif (args.require_api_key_origin
                      and api_keys_db.lookup(ak) is None
                      and request.headers.get("Origin") !=
                      args.require_api_key_origin):
                    abort(
                        403,
                        description=
                        "Please contact the server operator to obtain an API key",
                    )

            return f(*a, **kw)
Ejemplo n.º 2
0
        def func(*a, **kw):
            if flood.is_banned(get_remote_address()):
                abort(403, description="Too many request limits violations")

            if args.api_keys and args.require_api_key_origin:
                if request.is_json:
                    json = get_json_dict(request)
                    ak = json.get("api_key")
                else:
                    ak = request.values.get("api_key")

                if (api_keys_db.lookup(ak) is None
                        and request.headers.get("Origin") !=
                        args.require_api_key_origin):
                    abort(
                        403,
                        description=
                        "Please contact the server operator to obtain an API key",
                    )

            return f(*a, **kw)
Ejemplo n.º 3
0
    def detect():
        """
        Detect the language of a single text
        ---
        tags:
          - translate
        parameters:
          - in: formData
            name: q
            schema:
              type: string
              example: Hello world!
            required: true
            description: Text to detect
          - in: formData
            name: api_key
            schema:
              type: string
              example: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
            required: false
            description: API key
        responses:
          200:
            description: Detections
            schema:
              id: detections
              type: array
              items:
                type: object
                properties:
                  confidence:
                    type: number
                    format: float
                    minimum: 0
                    maximum: 1
                    description: Confidence value
                    example: 0.6
                  language:
                    type: string
                    description: Language code
                    example: en
          400:
            description: Invalid request
            schema:
              id: error-response
              type: object
              properties:
                error:
                  type: string
                  description: Error message
          500:
            description: Detection error
            schema:
              id: error-response
              type: object
              properties:
                error:
                  type: string
                  description: Error message
          429:
            description: Slow down
            schema:
              id: error-slow-down
              type: object
              properties:
                error:
                  type: string
                  description: Reason for slow down
          403:
            description: Banned
            schema:
              id: error-response
              type: object
              properties:
                error:
                  type: string
                  description: Error message
        """
        if flood.is_banned(get_remote_address()):
            abort(403, description="Too many request limits violations")

        if request.is_json:
            json = get_json_dict(request)
            q = json.get("q")
        else:
            q = request.values.get("q")

        if not q:
            abort(400, description="Invalid request: missing q parameter")

        return jsonify(detect_languages(q))