def get_all_data(self):
        uri = self._poll_uri
        hdrs = _headers(self._config)
        cache_entry = self._cache.get(uri)
        if cache_entry is not None:
            hdrs['If-None-Match'] = cache_entry.etag
        r = self._http.request('GET',
                               uri,
                               headers=hdrs,
                               timeout=urllib3.Timeout(
                                   connect=self._config.connect_timeout,
                                   read=self._config.read_timeout),
                               retries=1)
        throw_if_unsuccessful_response(r)
        if r.status == 304 and cache_entry is not None:
            data = cache_entry.data
            etag = cache_entry.etag
            from_cache = True
        else:
            data = json.loads(r.data.decode('UTF-8'))
            etag = r.getheader('ETag')
            from_cache = False
            if etag is not None:
                self._cache[uri] = CacheEntry(data=data, etag=etag)
        log.debug("%s response status:[%d] From cache? [%s] ETag:[%s]", uri,
                  r.status, from_cache, etag)

        return {FEATURES: data['flags'], SEGMENTS: data['segments']}
Esempio n. 2
0
 def _do_request(self, uri, allow_cache):
     hdrs = _headers(self._config.sdk_key)
     if allow_cache:
         cache_entry = self._cache.get(uri)
         if cache_entry is not None:
             hdrs['If-None-Match'] = cache_entry.etag
     r = self._http.request('GET',
                            uri,
                            headers=hdrs,
                            timeout=urllib3.Timeout(
                                connect=self._config.connect_timeout,
                                read=self._config.read_timeout),
                            retries=1)
     throw_if_unsuccessful_response(r)
     if r.status == 304 and allow_cache and cache_entry is not None:
         data = cache_entry.data
         etag = cache_entry.etag
         from_cache = True
     else:
         data = json.loads(r.data.decode('UTF-8'))
         etag = r.getheader('ETag')
         from_cache = False
         if allow_cache and etag is not None:
             self._cache[uri] = CacheEntry(data=data, etag=etag)
     log.debug("%s response status:[%d] From cache? [%s] ETag:[%s]", uri,
               r.status, from_cache, etag)
     return data
Esempio n. 3
0
    def urlopen(self, method, url, redirect=True, **kw):
        is_stream = not kw.get('preload_content', True)
        start_time = time.time()
        x = urlparse(url)
        name = url
        req_type = method
        if method == 'GET' and (x.path.find('/meval/') == 0
                                or x.path.find('/users/') > -1
                                or x.path.find('/eval/') == 0):
            parts = x.path.split('/')
            parts.pop()
            parts.append('[user]')
            newpath = '/'.join(parts)
            x = x._replace(path=newpath)
            name = urlunparse(x)

        if is_stream and kw.get('headers', {}).get(
                'Accept', 'lol') == 'text/event-stream':
            req_type = 'sse:connect'

        resp = None
        content_len = 0

        try:
            resp = super(LocustPoolManager,
                         self).urlopen(method, url, redirect, **kw)
            if is_stream:
                content_len = int(resp.headers.get("content-length") or 0)
            else:
                content_len = len(resp.data or b"")
            throw_if_unsuccessful_response(resp)
        except UnsuccessfulResponseException as e:
            request_failure.fire(request_type=req_type,
                                 name=name,
                                 exception=e,
                                 response_length=content_len,
                                 response_time=int(
                                     (time.time() - start_time) * 1000))
            return resp
        except Exception as e:
            request_failure.fire(request_type=req_type,
                                 name=name,
                                 exception=e,
                                 response_length=content_len,
                                 response_time=int(
                                     (time.time() - start_time) * 1000))
            raise e
        request_success.fire(request_type=req_type,
                             name=name,
                             response_length=content_len,
                             response_time=int(
                                 (time.time() - start_time) * 1000))
        return resp
    def _do_request(self, base_uri, allow_cache):
        hdrs = _headers(self._config.sdk_key, client='PythonMobile')
        method = "GET"
        body = None
        uri = base_uri
        cache_uri = uri + EVALX_GET + '/' + self._config.user_b64
        if self._config.use_report:
            method = 'REPORT'
            body = self._config.user_json
            hdrs.update({'Content-Type': 'application/json'})
            uri = uri + EVALX_REPORT
        else:
            uri = cache_uri
        if self._config.evaluation_reasons:
            uri += '?withReasons=true'
            cache_uri += '?withReasons=true'

        if allow_cache:
            cache_entry = self._cache.get(cache_uri)
            if cache_entry is not None:
                hdrs['If-None-Match'] = cache_entry.etag

        r = self._http.request(method,
                               uri,
                               headers=hdrs,
                               timeout=urllib3.Timeout(
                                   connect=self._config.connect_timeout,
                                   read=self._config.read_timeout),
                               retries=1,
                               body=body)
        throw_if_unsuccessful_response(r)
        if r.status == 304 and allow_cache and cache_entry is not None:
            data = cache_entry.data
            etag = cache_entry.etag
            from_cache = True
        else:
            data = json.loads(r.data.decode('UTF-8'))
            etag = r.getheader('ETag')
            from_cache = False
            if allow_cache and etag is not None:
                self._cache[uri] = CacheEntry(data=data, etag=etag)
        log.debug("%s response status:[%d] From cache? [%s] ETag:[%s]", uri,
                  r.status, from_cache, etag)
        return data
Esempio n. 5
0
    def urlopen(self, method, url, redirect=True, **kw):
        is_stream = not kw.get('preload_content', True)
        start_time = time.time()
        name = clean_name(method, url)
        req_type = method

        if is_stream and kw.get('headers', {}).get(
                'Accept', 'lol') == 'text/event-stream':
            req_type = 'sse:connect'

        resp = None
        content_len = 0

        try:
            resp = super(LocustProxyPoolManager,
                         self).urlopen(method, url, redirect, **kw)
            if is_stream:
                content_len = int(resp.headers.get("content-length") or 0)
            else:
                content_len = len(resp.data or b"")
            throw_if_unsuccessful_response(resp)
        except UnsuccessfulResponseException as e:
            request_failure.fire(request_type=req_type,
                                 name=name,
                                 exception=e,
                                 response_length=content_len,
                                 response_time=int(
                                     (time.time() - start_time) * 1000))
            return resp
        except Exception as e:
            request_failure.fire(request_type=req_type,
                                 name=name,
                                 exception=e,
                                 response_length=content_len,
                                 response_time=int(
                                     (time.time() - start_time) * 1000))
            raise e
        request_success.fire(request_type=req_type,
                             name=name,
                             response_length=content_len,
                             response_time=int(
                                 (time.time() - start_time) * 1000))
        return resp
    def _connect(self):
        if self.last_id:
            self.requests_kwargs['headers']['Last-Event-ID'] = self.last_id

        # Use session if set.  Otherwise fall back to requests module.
        self.resp = self.http.request(
            'GET',
            self.url,
            timeout=urllib3.Timeout(connect=self._connect_timeout, read=self._read_timeout),
            preload_content=False,
            retries=0, # caller is responsible for implementing appropriate retry semantics, e.g. backoff
            **self.requests_kwargs)

        # Raw readlines doesn't work because we may be missing newline characters until the next chunk
        # For some reason, we also need to specify a chunk size because stream=True doesn't seem to guarantee
        # that we get the newlines in a timeline manner
        self.resp_file = self.resp.stream(amt=self._chunk_size)

        # TODO: Ensure we're handling redirects.  Might also stick the 'origin'
        # attribute on Events like the Javascript spec requires.
        throw_if_unsuccessful_response(self.resp)
 def _do_request(self, uri, allow_cache):
     hdrs = _headers(self._config.sdk_key)
     if allow_cache:
         cache_entry = self._cache.get(uri)
         if cache_entry is not None:
             hdrs['If-None-Match'] = cache_entry.etag
     r = self._http.request('GET', uri,
                            headers=hdrs,
                            timeout=urllib3.Timeout(connect=self._config.connect_timeout, read=self._config.read_timeout),
                            retries=1)
     throw_if_unsuccessful_response(r)
     if r.status == 304 and cache_entry is not None:
         data = cache_entry.data
         etag = cache_entry.etag
         from_cache = True
     else:
         data = json.loads(r.data.decode('UTF-8'))
         etag = r.getheader('ETag')
         from_cache = False
         if allow_cache and etag is not None:
             self._cache[uri] = CacheEntry(data=data, etag=etag)
     log.debug("%s response status:[%d] From cache? [%s] ETag:[%s]",
         uri, r.status, from_cache, etag)
     return data