Created
September 3, 2020 07:26
-
-
Save Jay-flow/adffc8559673bb081c5b16c01f4a3e98 to your computer and use it in GitHub Desktop.
Falsk API Queue Crawling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
from flask_api import status | |
from flask_restplus import Resource | |
from src.api.docs.const import * | |
q = queue.Queue() | |
class RequestHandling: | |
def __init__(self): | |
self.crawling_api = RequestCrawlingAPI() | |
def async_requests(self): | |
while True: | |
if not q.empty() > 0: | |
self.crawling_api.request_handling(q.get()) | |
@app.before_first_request | |
def before_first_request(): | |
req = RequestHandling() | |
threading.Thread(target=req.async_requests).start() | |
@ns.route('/request_crawling') | |
@ns.param("Authorization", c_bear_token, required=True, _in="header") | |
@ns.param('pin_code', c_pin_code, required=True) | |
@ns.param('client_id', c_client_id, required=True) | |
class RequestCrawling(Resource): | |
@ns.doc(c_request_crawling) | |
@ns.expect(req_parser) | |
@ns.response(202, description=c_202_response, model=api_models.request_parameters) | |
@ns.response(401, description=c_401_response) | |
def get(self): | |
if not is_authorization(): | |
return {"result": c_401_response}, status.HTTP_401_UNAUTHORIZED | |
args = req_parser.parse_args() | |
validator_pin_code(args) | |
validator_client_id(args) | |
args['client_ip'] = request.remote_addr | |
q.put(args) | |
return { | |
"pin_code": args["pin_code"], | |
"client_id": args["client_id"], | |
"client_ip": args["client_ip"] | |
}, status.HTTP_202_ACCEPTED |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment