示例#1
1
文件: util.py 项目: stabora/nbsf
    def get_http_request(url, payload, method='POST', headers=None, use_proxy=False, use_proxy_auth=False, trust_env=True):
        try:
            session = Session()
            session.trust_env = trust_env
            session.proxies = Util.get_proxies() if use_proxy else None
            session.auth = Util.get_proxy_auth() if use_proxy_auth else None

            request = Request(
                'POST' if method not in ('GET', 'POST') else method,
                url,
                data=payload if method == 'POST' else None,
                params=payload if method == 'GET' else None,
                headers=headers
            )

            prepped = request.prepare()

            response = session.send(
                prepped,
                timeout=app.config['HTTP_REQUESTS_TIMEOUT']
            )

            session.close()
        except Exception, e:
            response = Response()
            response.raise_for_status()
            return response, 'Error al realizar la consulta - Motivo: {}'.format(e.message)
    def test_token(self, mock_head, mock_post, mock_service_creds):

        # mock out the token request response
        the_response = Response()
        the_response.status_code = 200
        the_response.json = MagicMock(return_value={'access_token': 'foobar'})
        mock_post.return_value = the_response

        block = OAuthBlock()
        self.configure_block(block, {
            "key_config_file": "tests/KEY_FILE_GOES_HERE.json"
        })

        token = block.get_access_token(
            'https://www.googleapis.com/auth/analytics.readonly')

        self.assertEqual(token['access_token'], 'foobar')

        # Oauth2ServiceAccount.get_access_token is mocked, so
        # Oauth2ServiceAccount.parse_token_from_response(r) is never called,
        # and self._oauth_token is never assigned.
        # Need to assign it here before checking block.get_access_token_headers
        block._oauth_token = {'access_token': 'foobar'}
        self.assertEqual(block.get_access_token_headers(), {
            'Authorization': 'Bearer foobar'
        })
示例#3
0
def get_acacess_token_app(tenant_id,client_id,client_secret,resource):
    initialise()
    
    url = "https://login.windows.net/" + tenant_id + "/oauth2/token"        
    
    body_data = "&grant_type=client_credentials&resource="+ resource +"&client_id="+ client_id + "&client_secret="+  urllib.quote_plus(client_secret)
    
    headers = {"Content-Type":"application/x-www-form-urlencoded"}
    req = Request(method="POST",url=url,data=body_data)
    req_prepped = req.prepare()
    s = Session()
    res = Response()
    res = s.send(req_prepped)
    access_token_det = {}
    if (res.status_code == 200):
        responseJSON = json.loads(res.content)
        access_token_det["details"]= responseJSON["access_token"]
        access_token_det["status"]="1"
        access_token_det["exp_time"]=responseJSON["expires_in"]
        access_token_det["exp_date"]=responseJSON["expires_on"]
        access_token_det["accessDetails"]=responseJSON
    else:
        access_token_det["details"]= str(res.status_code) + str(res.json())
        access_token_det["status"]="0"
    return access_token_det
示例#4
0
def parse_arrivals_from_infotrafic(line_id: int, stations: Dict[str, Station], response: requests.Response, include_unknown_stations: bool = False) -> Tuple[List[Tuple[Union[Station,str], Arrival]]]:
	response.raise_for_status()
	if response.status_code == requests.codes.ok:
		bs = bs4.BeautifulSoup(response.text, "html.parser")
		prevcolor = None
		datacolor = '00BFFF'
		routes = []
		route = None
		tz = pytz.timezone("Europe/Bucharest")
		now = tzlocal.get_localzone().localize(datetime.now()).astimezone(tz).replace(second=0, microsecond=0)
		for row in bs.find_all("table"):
			if row['bgcolor'] == datacolor:
				if prevcolor != datacolor:
					route = []
					routes.append(route)

				cols = row.find_all("b")
				raw_station_name = cols[1].text.strip()
				station = stations.get(raw_station_name, None)
				arrival = parse_arrival(now, line_id, station.station_id if station else -1, cols[2].text)
				if station is not None or include_unknown_stations:
					route.append((station if station is not None else raw_station_name, arrival))

			prevcolor = row['bgcolor']

		return routes if route else None

	return None
示例#5
0
    def test_get_json_data(self, gobase_get_full_response):
        response = Response()
        response._content = "not a json object"
        gobase_get_full_response.return_value = response

        with self.assertRaises(GoCdApiException):
            self.gobase.get_json_data(self.gobase.url)
示例#6
0
    def logged_in(self, login='******', travis_user=True):
        """ A context manager to do stuff, while logged in as fred. """

        response = Response()
        response._content = json.dumps(
            dict(id=12345, login=login, name='Fred')
        )
        data = Mock()
        data.get = Mock(return_value=response)
        data.access_token = GH_TOKEN

        true = Mock(return_value=True)

        if travis_user:
            travis_patch = patch('travis_utils.is_travis_user', true)
            travis_patch.start()

        with patch('statiki.github', Mock(spec=OAuth2Service)) as gh:
            gh.get_auth_session = Mock(return_value=data)

            try:
                yield self.app.get('/authorized?code="bazooka"')

            finally:
                if travis_user:
                    travis_patch.stop()

                self.app.get('/logout')
def test_yahoo_get_by_date(yahoo, logger):
    sample = Response()
    sample.status_code = 200
    sample._content = YAHOO_RESPONSE
    yahoo._get = lambda *a, **kw: sample

    assert yahoo.get_by_date(date.today(), "CZK", logger) == Decimal("25.959")
示例#8
0
 def _method(self, url, *args, **kwargs):
     success = url in self.urls
     content = self.urls.get(url, '')
     resp = Response()
     resp._content = content.encode('utf-8')
     resp.status_code = codes.ok if success else codes.not_found
     return resp
示例#9
0
文件: parser.py 项目: kelvan/pyWL
 def stub(url):
     response = Response()
     response._content = json
     response.url = url
     response.status_code = status_code
     response.json = json_loads_stub(json)
     return response
示例#10
0
def fake_response_handler(url=None, **kwargs):
    r = Response()
    r.status_code = 200
    r.encoding = "utf-8"
    assert url in fake_responses, "unknown URL: " + url
    r._content = fake_responses[url]
    return r
示例#11
0
    def test_download_redownload_as_hash_mismatches(self):
        with open('os.snap', 'wb') as f:
            f.write(b'0000000')

        snap_content = b'1234567890'
        snap_sha512 = ('12b03226a6d8be9c6e8cd5e55dc6c7920caaa39df14aab92d5e'
                       '3ea9340d1c8a4d3d0b8e4314f1f6ef131ba4bf1ceb9186ab87c'
                       '801af0d5c95b1befb8cedae2b9')
        mock_details = self.mock_get.return_value
        mock_details.ok = True
        mock_details.content = json.dumps({
            '_embedded': {
                    'clickindex:package': [{
                        'download_url': 'http://localhost',
                        'anon_download_url': 'http://localhost',
                        'download_sha512': snap_sha512,
                    }],
            }
        }).encode('utf-8')
        mock_snap = Response()
        mock_snap.status_code = 200
        mock_snap._content = snap_content

        self.mock_get.side_effect = [mock_details, mock_snap]

        download('os', 'edge', 'os.snap', None, 'amd64')

        self.mock_logger.info.assert_has_calls([
            call("Getting details for 'os'"),
            call("Downloading 'os'"),
            call("Successfully downloaded 'os'")])
        self.assertTrue(os.path.exists('os.snap'))
示例#12
0
    def test_download_fails_due_to_hash_mismatch(self):
        snap_content = b'1234567890'
        mock_details = self.mock_get.return_value
        mock_details.ok = True
        mock_details.content = json.dumps({
            '_embedded': {
                    'clickindex:package': [{
                        'download_url': 'http://localhost',
                        'anon_download_url': 'http://localhost',
                        'download_sha512': '12345',
                    }],
            }
        }).encode('utf-8')
        mock_snap = Response()
        mock_snap.status_code = 200
        mock_snap._content = snap_content

        self.mock_get.side_effect = [mock_details, mock_snap]

        with self.assertRaises(RuntimeError) as raised:
            download('os', 'edge', 'os.snap', None, 'amd64')

        self.assertEqual("Failed to download 'os'", str(raised.exception))
        self.mock_logger.info.assert_has_calls([
            call("Getting details for 'os'"),
            call("Downloading 'os'")])
        self.assertTrue(os.path.exists('os.snap'))
示例#13
0
 def new_response(self, response):
     """Convert an tornado.HTTPResponse object to a requests.Response object"""
     new = Response()
     new._content = response.body
     new.status_code = response.code
     new.headers = dict(response.headers.get_all())
     return new
示例#14
0
    def handle_401(self, r: R, **kwargs):
        """Takes the given response and re-tries auth with a new nonce."""

        if r.status_code == 401 and self.num_401_calls < 2:

            self.num_401_calls += 1

            # Renew nonce
            nonce = self.new_nonce(max(self._nonce(), self.last_nonce + 1))

            # Consume content and release the original connection
            # to allow our new request to reuse the same one.
            r.content
            r.close()
            prep = r.request.copy()
            cookies = prep._cookies
            requests.auth.extract_cookies_to_jar(cookies, r.request, r.raw)
            prep.prepare_cookies(cookies)

            self.auth_request(prep, nonce)

            _r = r.connection.send(prep, **kwargs)
            _r.history.append(r)
            _r.request = prep

            return _r

        return r
示例#15
0
    def test_upload_app_ok(self):
        # fake upload response
        mock_response = self.mock_post.return_value
        mock_response.ok = True
        mock_response.return_value = {
            'success': True,
            'status_url': 'http://example.com/status/',
        }
        # fake poll status response
        application_url = 'http://example.com/app/1'
        revision = '1'
        ok_response = Response()
        ok_response.status_code = 200
        ok_response.encoding = 'utf-8'
        ok_response._content = json.dumps(
            {'completed': True, 'revision': revision,
             'application_url': application_url}).encode('utf-8')
        self.mock_get.return_value = ok_response

        success = upload(self.binary_file.name, 'foo')

        self.assertTrue(success)
        self.assertIn(
            call('Application uploaded successfully (as revision {})'.format(
                revision)),
            self.mock_logger.info.call_args_list)
        self.assertIn(call('Please check out the application at: %s\n',
                           application_url),
                      self.mock_logger.info.call_args_list)
示例#16
0
    def test_get_token_with_scope(self, get_token):
        the_response = Response()
        the_response.status_code = 200
        the_response.json = MagicMock(return_value={'access_token': 'foobar'})
        get_token.return_value = the_response

        block = OAuthBlock()
        self.configure_block(block, {})
        token = block.get_access_token(
            'user', 'pass',
            scope='my-scope',
            addl_params={
                'extra': 'value'
            })

        self.assertEqual(token['access_token'], 'foobar')
        self.assertEqual(block.get_access_token_headers(), {
            'Authorization': 'Bearer foobar'
        })

        get_token.assert_called_once_with(
            'http://oauthbase/token',
            data={
                'username': '******',
                'password': '******',
                'scope': 'my-scope',
                'grant_type': 'password',
                'extra': 'value'
            })
示例#17
0
    def test_snap_already_downloaded(self):
        snap_content = b'1234567890'
        snap_sha512 = ('12b03226a6d8be9c6e8cd5e55dc6c7920caaa39df14aab92d5e'
                       '3ea9340d1c8a4d3d0b8e4314f1f6ef131ba4bf1ceb9186ab87c'
                       '801af0d5c95b1befb8cedae2b9')

        with open('os.snap', 'wb') as f:
            f.write(snap_content)

        mock_details = self.mock_get.return_value
        mock_details.ok = True
        mock_details.content = json.dumps({
            'download_url': 'http://localhost',
            'download_sha512': snap_sha512,
        }).encode('utf-8')
        mock_snap = Response()
        mock_snap.status_code = 200
        mock_snap._content = snap_content

        self.mock_get.side_effect = [mock_details, mock_snap]

        download('os', 'os.snap', None, 'amd64')

        self.mock_logger.info.assert_has_calls([
            call("Getting details for 'os'"),
            call("Already downloaded 'os'")])
        self.assertTrue(os.path.exists('os.snap'))
示例#18
0
文件: http.py 项目: rmyers/cannula
 async def did_receive_response(
     self,
     response: requests.Response,
     request: Request
 ) -> typing.Any:
     response.raise_for_status()
     return response.json(object_hook=self.convert_to_object)
示例#19
0
def test_add_channel_channel_already_exists(mock_staff_client, patched_users_api):
    """Channel already exists with that channel name"""
    response_409 = Response()
    response_409.status_code = statuses.HTTP_409_CONFLICT
    mock_staff_client.channels.create.return_value = response_409

    title = "title"
    name = "name"
    description = "public description"
    channel_type = "private"
    input_search = Search.from_dict({"unmodified": "search"})
    role = RoleFactory.create()
    mod = UserFactory.create()

    with pytest.raises(ChannelAlreadyExistsException):
        api.add_channel(
            original_search=input_search,
            title=title,
            name=name,
            description=description,
            channel_type=channel_type,
            program_id=role.program.id,
            creator_id=mod.id,
        )

    mock_staff_client.channels.create.assert_called_once_with(
        title=title,
        name=name,
        description=description,
        channel_type=channel_type,
    )
示例#20
0
 def make_response(self, status_code=200, reason='OK', data=None):
     data = data or {}
     response = Response()
     response.status_code = status_code
     response.reason = reason
     response._content = json.dumps(data).encode('utf-8')
     return response
示例#21
0
    def handle(self, method, *args, **kwargs):
        """Log method calls to a list and return a mock response."""
        MockGovDelivery.calls.append((method, args, kwargs))

        response = Response()
        response.status_code = 200
        return response
示例#22
0
def build_response(request,
                   status_code=200,
                   headers={},
                   content='(none)'):
    """
    Build a :class:`requests.Response` object on the basis of the passed
    parameters.
    """

    response = Response()

    response.status_code = status_code
    response.reason = responses[status_code]
    response.headers = CaseInsensitiveDict(headers)
    # Pretend that we've already read from the socket
    response._content = content

    response.encoding = get_encoding_from_headers(response.headers)
    response.url = request.url
    response.raw = MockRawResponse()

    # Give the Response some context.
    response.request = request
    response.connection = MockConnection()

    return response
示例#23
0
    def test_outside_app(self):
        resp = Response()
        resp.status_code = 200
        resp._content = '{"ok": true}'

        flexmock(urlfetch.S).should_receive('request').with_args(
        method='POST',
        url='http://services.dev.guokr.com/taskqueue.json',
        files=None,
        data={
            'appname': 'test',
            'method_': 'GET',
            'url_': 'http://www.g.cn',
            'countdown_': 0
            }).and_return(resp).once()
        taskqueue.add_url('http://www.g.cn') # success

        flexmock(urlfetch.S).should_receive('request').and_return(resp)
        self.assertRaises(RuntimeError, taskqueue.add_url, '/test') # fail
        self.assertRaises(RuntimeError, taskqueue.add, '.test_view') # fail

        with self.app.test_request_context('http://localhost/testxx'):
            flexmock(urlfetch.S).should_receive('request').with_args(
                method='POST',
                url='http://services.dev.guokr.com/taskqueue.json',
                files=None,
                data={
                    'appname': 'test',
                    'method_': 'POST',
                    'url_': 'http://backends.dev.guokr.com/test.json',
                    'countdown_': 0
                }).and_return(resp).twice()
            taskqueue.add_url('/test.json', method_='POST') # success
            taskqueue.add('.test_view', method='POST') # success
 def test_raises_error_on_4XX_or_5XX_responses(self, mock_request):
     mock_request.__name__ = 'request'
     data_set = DataSet('', None)
     response = Response()
     response.status_code = 418
     mock_request.return_value = response
     assert_raises(HTTPError, data_set.post, {'key': 'foo'})
示例#25
0
 def send(self, request, stream=False, timeout=None, verify=True, cert=None,
          proxies=None):
     resp = Response()
     resp.status_code = 200
     ticket = re.findall(r"abematv-license://(.*)", request.url)[0]
     resp._content = self._get_videokey_from_ticket(ticket)
     return resp
示例#26
0
    def __init__(self):
        """Use response.text have performance problem, use response.html fix it.
        This is an issue, maybe some init charset not set, requests use chardet
        library to detect charset of the html content.

        """
        ResponseLib.__init__(self)
 def test_private_user(self, mock_get, mock_id, mock_auth, mock_retry):
     blk = InstagramSearchByUser()
     self.configure_block(blk, {
         "queries": [
             "user1",
             "user2"
         ]
     })
     blk.queries = ['1', '2']
     blk._n_queries = len(blk.queries())
     resp = Response()
     resp.status_code = 400
     resp.json = Mock()
     resp.json.return_value = \
         {
             'meta': {
                 'error_type': 'APINotAllowedError',
                 'code': 400,
                 'error_message': 'you cannot view this resource'
             }
         }
     mock_get.return_value = resp
     paging = False
     self.assertEqual(0, blk._idx)
     blk.poll(paging)
     # skip to next idx because we are not retrying.
     self.assertEqual(1, blk._idx)
def create_response(**kw):
    r = Response()
    r._content = b''
    r.status = 200
    for k, v in kw.items():
        setattr(r, k, v)
    return r
 def test_retry(self, mock_get, mock_id, mock_auth, mock_retry):
     blk = InstagramSearchByUser()
     self.configure_block(blk, {
         "queries": [
             "user1",
             "user2"
         ]
     })
     blk.queries = ['1', '2']
     blk._n_queries = len(blk.queries())
     resp = Response()
     resp.status_code = 400
     resp.json = Mock()
     resp.json.return_value = \
         {
             'meta': {
                 'error_type': 'WardrobeMalfunction',
                 'code': 400,
                 'error_message': "Your pants don't fit"
             }
         }
     mock_get.return_value = resp
     paging = False
     self.assertEqual(0, blk._idx)
     blk.poll(paging)
     # don't skip to next idx because we are retrying.
     self.assertEqual(0, blk._idx)
示例#30
0
def test_reset_rabbit_connection_errors():

    rabbit_manager = Mock()

    with patch('nameko.testing.utils.get_rabbit_connections') as connections:
        connections.return_value = [{
            'vhost': 'vhost_name',
            'name': 'connection_name'
        }]

        # 500 error
        response_500 = Response()
        response_500.status_code = 500
        error_500 = HTTPError(response=response_500)
        rabbit_manager.delete_connection.side_effect = error_500

        with pytest.raises(HTTPError):
            reset_rabbit_connections("vhost_name", rabbit_manager)

        # 404 error
        response_404 = Response()
        response_404.status_code = 404
        error_404 = HTTPError(response=response_404)
        rabbit_manager.delete_connection.side_effect = error_404

        # does not raise
        reset_rabbit_connections("vhost_name", rabbit_manager)
示例#31
0
def test_malformed_request(postmark, postmark_request):
    postmark_request.return_value = Response()
    postmark_request.return_value.status_code = 500
    postmark_request.return_value._content = b"Server Error"
    with pytest.raises(HTTPError, matches="Server Error"):
        postmark.call("GET", "endpoint")
示例#32
0
 def raise_for_status(self):
     if hasattr(self, 'error') and self.error:
         raise self.error
     Response.raise_for_status(self)
示例#33
0
文件: source.py 项目: Mu-L/airbyte
 def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapping]:
     json_response = response.json()
     records = json_response.get("results", [])
     yield from records
示例#34
0
 def _assert_valid_credentials(login_response: requests.Response) -> None:
     if not login_response.json()['success']:
         raise InvalidCredentials(login_response.json()['message'])
示例#35
0
    def upload_file(self,
                    body,
                    key=None,
                    metadata=None,
                    headers=None,
                    access_key=None,
                    secret_key=None,
                    queue_derive=None,
                    verbose=None,
                    verify=None,
                    checksum=None,
                    delete=None,
                    retries=None,
                    retries_sleep=None,
                    debug=None,
                    request_kwargs=None):
        """Upload a single file to an item. The item will be created
        if it does not exist.

        :type body: Filepath or file-like object.
        :param body: File or data to be uploaded.

        :type key: str
        :param key: (optional) Remote filename.

        :type metadata: dict
        :param metadata: (optional) Metadata used to create a new item.

        :type headers: dict
        :param headers: (optional) Add additional IA-S3 headers to request.

        :type queue_derive: bool
        :param queue_derive: (optional) Set to False to prevent an item from
                             being derived after upload.

        :type verify: bool
        :param verify: (optional) Verify local MD5 checksum matches the MD5
                       checksum of the file received by IAS3.

        :type checksum: bool
        :param checksum: (optional) Skip based on checksum.

        :type delete: bool
        :param delete: (optional) Delete local file after the upload has been
                       successfully verified.

        :type retries: int
        :param retries: (optional) Number of times to retry the given request
                        if S3 returns a 503 SlowDown error.

        :type retries_sleep: int
        :param retries_sleep: (optional) Amount of time to sleep between
                              ``retries``.

        :type verbose: bool
        :param verbose: (optional) Print progress to stdout.

        :type debug: bool
        :param debug: (optional) Set to True to print headers to stdout, and
                      exit without sending the upload request.

        Usage::

            >>> import internetarchive
            >>> item = internetarchive.Item('identifier')
            >>> item.upload_file('/path/to/image.jpg',
            ...                  key='photos/image1.jpg')
            True
        """
        # Set defaults.
        headers = {} if headers is None else headers
        metadata = {} if metadata is None else metadata
        access_key = self.session.access_key if access_key is None else access_key
        secret_key = self.session.secret_key if secret_key is None else secret_key
        queue_derive = True if queue_derive is None else queue_derive
        verbose = False if verbose is None else verbose
        verify = True if verify is None else verify
        delete = False if delete is None else delete
        # Set checksum after delete.
        checksum = True if delete or checksum is None else checksum
        retries = 0 if retries is None else retries
        retries_sleep = 30 if retries_sleep is None else retries_sleep
        debug = False if debug is None else debug
        request_kwargs = {} if request_kwargs is None else request_kwargs
        md5_sum = None

        if not hasattr(body, 'read'):
            body = open(body, 'rb')

        size = get_file_size(body)

        if not headers.get('x-archive-size-hint'):
            headers['x-archive-size-hint'] = size

        # Build IA-S3 URL.
        key = body.name.split('/')[-1] if key is None else key
        base_url = '{0.session.protocol}//s3.us.archive.org/{0.identifier}'.format(
            self)
        url = '{0}/{1}'.format(
            base_url, urllib.parse.quote(key.lstrip('/').encode('utf-8')))

        # Skip based on checksum.
        if checksum:
            md5_sum = get_md5(body)
            ia_file = self.get_file(key)
            if (not self.tasks) and (ia_file) and (ia_file.md5 == md5_sum):
                log.info('{f} already exists: {u}'.format(f=key, u=url))
                if verbose:
                    print(' {f} already exists, skipping.'.format(f=key))
                if delete:
                    log.info('{f} successfully uploaded to '
                             'https://archive.org/download/{i}/{f} '
                             'and verified, deleting '
                             'local copy'.format(i=self.identifier, f=key))
                    os.remove(body.name)
                # Return an empty response object if checksums match.
                # TODO: Is there a better way to handle this?
                return Response()

        # require the Content-MD5 header when delete is True.
        if verify or delete:
            if not md5_sum:
                md5_sum = get_md5(body)
            headers['Content-MD5'] = md5_sum

        def _build_request():
            body.seek(0, os.SEEK_SET)
            if verbose:
                try:
                    chunk_size = 1048576
                    expected_size = size / chunk_size + 1
                    chunks = chunk_generator(body, chunk_size)
                    progress_generator = progress.bar(
                        chunks,
                        expected_size=expected_size,
                        label=' uploading {f}: '.format(f=key))
                    data = IterableToFileAdapter(progress_generator, size)
                except:
                    print(' uploading {f}'.format(f=key))
                    data = body
            else:
                data = body

            request = S3Request(method='PUT',
                                url=url,
                                headers=headers,
                                data=data,
                                metadata=metadata,
                                access_key=access_key,
                                secret_key=secret_key,
                                queue_derive=queue_derive)
            return request

        if debug:
            return _build_request()
        else:
            try:
                error_msg = ('s3 is overloaded, sleeping for '
                             '{0} seconds and retrying. '
                             '{1} retries left.'.format(
                                 retries_sleep, retries))
                while True:
                    if retries > 0:
                        if self.session.s3_is_overloaded(access_key):
                            sleep(retries_sleep)
                            log.info(error_msg)
                            if verbose:
                                print(' warning: {0}'.format(error_msg),
                                      file=sys.stderr)
                            retries -= 1
                            continue
                    request = _build_request()
                    prepared_request = request.prepare()
                    response = self.session.send(prepared_request,
                                                 stream=True,
                                                 **request_kwargs)
                    if (response.status_code == 503) and (retries > 0):
                        log.info(error_msg)
                        if verbose:
                            print(' warning: {0}'.format(error_msg),
                                  file=sys.stderr)
                        sleep(retries_sleep)
                        retries -= 1
                        continue
                    else:
                        if response.status_code == 503:
                            log.info(
                                'maximum retries exceeded, upload failed.')
                        break
                response.raise_for_status()
                log.info('uploaded {f} to {u}'.format(f=key, u=url))
                if delete and response.status_code == 200:
                    log.info(
                        '{f} successfully uploaded to '
                        'https://archive.org/download/{i}/{f} and verified, deleting '
                        'local copy'.format(i=self.identifier, f=key))
                    os.remove(body.name)
                return response
            except HTTPError as exc:
                msg = get_s3_xml_text(exc.response.content)
                error_msg = (' error uploading {0} to {1}, '
                             '{2}'.format(key, self.identifier, msg))
                log.error(error_msg)
                if verbose:
                    print(' error uploading {0}: {1}'.format(key, msg),
                          file=sys.stderr)
                # Raise HTTPError with error message.
                raise type(exc)(error_msg,
                                response=exc.response,
                                request=exc.request)
示例#36
0
 def _check_for_captcha(login_response: requests.Response) -> None:
     if login_response.json().get('captcha_needed', False):
         raise CaptchaRequired('Captcha required')
示例#37
0
def get_pretty_response(resp: Response) -> JsonDict:
    resp.encoding = 'utf-8'
    return json.loads(resp.text, object_hook=convert_json)
示例#38
0
def resolve_url(url, *args, **kwargs):
    response = Response()
    response.status_code = 200
    response.url = url
    return response
示例#39
0
def responses(code,
              path=None,
              redirection=None,
              data=None,
              url=None,
              headers={'Content-Type': 'text/xml'}):
    response = Response()
    response.status_code = code
    if path is not None and redirection is None:
        with open(data_file(path), 'rb') as f:
            response.raw = BytesIO(f.read())
    elif data is not None:
        response._content = data.encode('utf-8')
    if redirection is not None:
        temp = Response()
        temp.status_code = 301 if 'permanent' in redirection else 302
        temp.url = path
        response.history.append(temp)
        response.url = redirection
        headers['location'] = path
    if url is None:
        if redirection is not None:
            url = redirection
        else:
            url = 'https://example.com/{}'.format(str(uuid4()))
    response.url = url
    response.headers = headers
    return response
示例#40
0
def create_response_obj_with_header():
    resp = Response()
    resp.headers['x-openstack-request-id'] = REQUEST_ID
    resp.headers['Etag'] = 'd5103bf7b26ff0310200d110da3ed186'
    resp.status_code = 200
    return resp
def create_response_with_compute_header():
    resp = Response()
    resp.headers['x-compute-request-id'] = FAKE_REQUEST_ID
    return resp
示例#42
0
 def process_response(response: Response, latest_log_id: int) -> int:
     for log in response.json():
         check_gt(log["id"], latest_log_id)
         latest_log_id = log["id"]
         print("{} [{}]: {}".format(log["time"], log["level"], log["message"]))
     return latest_log_id
示例#43
0
 def mocked_request(*_, **__):
     mocked_resp = Response()
     mocked_resp.status_code = mocked_codes["codes"].pop()
     return mocked_resp
示例#44
0
 def mock_request(*_, **__):
     mocked_resp = Response()
     mocked_resp.status_code = HTTPNotFound.code
     return mocked_resp
示例#45
0
 def _get_index_id_from_response(response: Response):
     return response.json()['result_url'].split('/')[3]
示例#46
0
def mesh_client_http_error():
    response = Response()
    response.status_code = 400
    response.reason = "Bad request for url"
    response.url = TEST_INBOX_URL
    return HTTPError(response=response)
示例#47
0
 def assert_status_hook(response: Response, *args, **kwargs):
     response.raise_for_status()
示例#48
0
 def _get_analysis_id_from_response(response: Response):
     return response.json()['result_url'].split('/')[2]
示例#49
0
 def get_valid_json_from_response(self, response: requests.Response):
     try:
         return response.json()
     except (ValueError, json.JSONDecodeError):
         return response.text
示例#50
0
    def send(self, request, **kwargs):
        """ Wraps a file, described in request, in a Response object.

            :param request: The PreparedRequest` being "sent".
            :returns: a Response object containing the file
        """

        # Check that the method makes sense. Only support GET
        if request.method != "GET":
            raise ValueError("Invalid request method %s" % request.method)

        # Parse the URL
        url_parts = urlparse(request.url)

        # Reject URLs with a hostname component
        if url_parts.netloc and url_parts.netloc != "localhost":
            raise ValueError(
                "file: URLs with hostname components are not permitted")

        resp = Response()

        # Open the file, translate certain errors into HTTP responses
        # Use urllib's unquote to translate percent escapes into whatever
        # they actually need to be
        try:
            # Split the path on / (the URL directory separator) and decode any
            # % escapes in the parts
            path_parts = [unquote(p) for p in url_parts.path.split('/')]

            # If os.sep is in any of the parts, someone fed us some shenanigans.
            # Treat is like a missing file.
            if any(os.sep in p for p in path_parts):
                raise IOError(errno.ENOENT, os.strerror(errno.ENOENT))

            # Put it the path back together
            # [0] will be an empty string, so stick os.sep in there to make
            # the path absolute
            path_parts[0] = os.sep
            path = os.path.join(*path_parts)

            resp.raw = open(path, "rb")
        except IOError as e:
            if e.errno == errno.EACCES:
                resp.status_code = 403
            elif e.errno == errno.ENOENT:
                resp.status_code = 404
            else:
                resp.status_code = 400

            # Wrap the error message in a file-like object
            # The error message will be localized, try to convert the string
            # representation of the exception into a byte stream
            resp_str = str(e).encode(locale.nl_langinfo(locale.CODESET))
            resp.raw = BytesIO(resp_str)
            resp.headers['Content-Length'] = len(resp_str)
        else:
            resp.status_code = 200

            # If it's a regular file, set the Content-Length
            resp_stat = os.fstat(resp.raw.fileno())
            if stat.S_ISREG(resp_stat.st_mode):
                resp.headers['Content-Length'] = resp_stat.st_size

        return resp
示例#51
0
import os

import mock
from guessit import guessit
from requests import Response

import tvsort_sl.utils as utils
from tvsort_sl.app import PROCESS_RUNNING
from tvsort_sl.conf import get_conf_file_name
from tvsort_sl.tests.test_base import tv_sort

r_200 = Response()
r_200.status_code = 200
r_202 = Response()
r_202.status_code = 202
r_400 = Response()
r_400.status_code = 400


def setup_function(_):
    utils.create_folder(tv_sort.settings.get('TV_PATH'))
    utils.create_folder(tv_sort.settings.get('DUMMY_PATH'))
    utils.create_folder(tv_sort.settings.get('MOVIES_PATH'))
    utils.create_folder(tv_sort.settings.get('UNSORTED_PATH'))


def teardown_function(_):
    utils.delete_folder(tv_sort.settings.get('TV_PATH'), force=True)
    utils.delete_folder(tv_sort.settings.get('DUMMY_PATH'), force=True)
    utils.delete_folder(tv_sort.settings.get('MOVIES_PATH'), force=True)
    utils.delete_folder(tv_sort.settings.get('UNSORTED_PATH'), force=True)
 def __init__(self, json_data):
     Response.__init__(self)
     self.json_data = json_data
     self.status_code = json_data["status_code"]
     self.reason = json_data["reason"]
     self.url = json_data["url"]
示例#53
0
 def parse_response(self, response: requests.Response,
                    **kwargs) -> Iterable[Mapping]:
     """
     We need to get out the nested complex data structures for further normalisation, so the transform_data method is applied.
     """
     yield from transform_data(response.json().get("elements"))
示例#54
0
Run these tests with tox -e test -- -k test_translation
"""
from pathlib import Path
from unittest import mock

from requests import Response

from dashboard.internet_nl_dashboard.logic.internet_nl_translations import (
    convert_vue_i18n_format, get_locale_content, get_po_as_dictionary_v2,
    load_as_po_file, translate_field)

path = Path(__file__).parent

# Create the desired normal response, by simply forcing the correct properties to be present.
# This is probably not the way to do it, yet i found the other methods be mostly obscure.
perfect_response = Response()
perfect_response._content = "yolo"


def file_get_contents(filepath):
    with open(filepath, 'r') as content_file:
        return content_file.read()


def mocked_requests_get(*args, **kwargs):
    class MockResponse:
        def __init__(self, content, status_code):
            self.content = content
            self.status_code = status_code

    if args[0] == 'https://raw.githubusercontent.com/NLnetLabs/Internet.nl/master/translations/nl/main.po':
示例#55
0
 def get(self, request, format=None):
     snippets = ResourceExperience.objects.all()
     serializer = ResourceExperienceSerializer(snippets, many=True)
     return Response(serializer.data)
示例#56
0
 def delete(self, ctx, payload, resource_id):
     response = Response()
     response.status_code = 200
     response._content = json.dumps({'profile': {'name': resource_id, 'message': 'deleted'}}).encode('utf-8')
     Configuration.Instance().delete_section_or_profile(resource_id)
     return response
示例#57
0
 def get(self, request, pk, format=None):
     snippet = self.get_object(pk)
     serializer = ResourceExperienceSerializer(snippet)
     return Response(serializer.data)
示例#58
0
 def delete(self, request, pk, format=None):
     snippet = self.get_object(pk)
     snippet.delete()
     return Response(status=status.HTTP_204_NO_CONTENT)
示例#59
0
def get_fields(res: Response):
    json = res.json()
    return json.get("data", []), json.get("meta", {}), json.get("error", [])
示例#60
0
 def _enter_steam_guard_if_necessary(self, login_response: requests.Response) -> requests.Response:
     if login_response.json()['requires_twofactor']:
         self.one_time_code = guard.generate_one_time_code(self.shared_secret)
         return self._send_login_request()
     return login_response