def run_request(request, token = None):
    """
    run_request

    Param: request of ADT Request

    Runs a customized request until proper data is
    given.
    """
    if token:
        request.headers["Authorization"] = "Bearer " + token

    while True:
        prepared = request.prepare()
        session = requests.Session()
        response = session.send(prepared)

        try:
            json = response.json()
        except:
            print(sys.argv[0] + " ERROR: Did not receive JSON from request.", file=sys.stderr)
            sys.exit(1)

        if "errors" not in json:
            return json
        else:
            for error in json["errors"]:
                # Error 88 is wait error
                if error["code"] == 88:
                    wait_till_time_token(response)
                    continue
            # Has an error but not limit error.
            print(sys.argv[0] + " WARNING: Request returned an error.  Will try to keep running normally.", file=sys.stderr)
            return json
Ejemplo n.º 2
0
 def requests_open(request):
     r = request.prepare()
     s = requests.Session()
     s.verify = True
     response = s.send(r)
     response.read = MethodType(lambda x: x.text, response)
     return response
Ejemplo n.º 3
0
 def _prepare_request(self, topic):
     headers = {'referer': topic.url, 'host': "tapochek.net"}
     cookies = self.tracker.get_cookies()
     request = requests.Request('GET',
                                self.tracker.get_download_url(topic.url),
                                headers=headers,
                                cookies=cookies)
     return request.prepare()
Ejemplo n.º 4
0
    def _make_request(self, method, url, **kwargs) -> requests.Response:
        kwargs.setdefault('timeout', self.timeout)
        session = self.session
        timeout = kwargs.pop('timeout', self.timeout)

        headers = self.default_headers.copy()
        headers.update(kwargs.pop('headers', {}))

        request = requests.Request(method, url, headers=headers, **kwargs)
        prepared_request = request.prepare()
        self._log_request(method, url, prepared_request.body)
        start_time = time.time()
        try:
            response = session.send(prepared_request, timeout=timeout)
            duration = time.time() - start_time
            if response.status_code >= 400:
                log_method = logging.error
            else:
                log_method = logging.debug

            self._log_response(response,
                               duration=duration,
                               log_method=log_method)
            return response
        except requests.exceptions.RequestException as e:
            duration = time.time() - start_time
            if e.response:
                self._log_response(e.response,
                                   duration=duration,
                                   log_method=logging.error)
            else:
                self._log_request(method,
                                  url,
                                  prepared_request.body,
                                  log_method=logging.exception)
            raise
        finally:
            if not self.keep_alive:
                session.close()
Ejemplo n.º 5
0
 def _prepare_request(self, topic):
     headers = {'referer': topic.url, 'host': "tapochek.net"}
     cookies = self.tracker.get_cookies()
     request = requests.Request('GET', self.tracker.get_download_url(topic.url), headers=headers, cookies=cookies)
     return request.prepare()
Ejemplo n.º 6
0
 def _prepare_request(self, topic):
     cookies = self.tracker.get_cookies()
     request = requests.Request('GET', self.tracker.get_download_url(topic.url), cookies=cookies)
     return request.prepare()
Ejemplo n.º 7
0
 def _prepare_request(self, topic):
     cookies = self.tracker.get_cookies()
     request = requests.Request('GET',
                                self.tracker.get_download_url(topic.url),
                                cookies=cookies)
     return request.prepare()