コード例 #1
0
ファイル: test_api.py プロジェクト: uploadcare/pyuploadcare
    def test_request_headers(self, request):
        user_agent = _build_user_agent()

        request.return_value = MockResponse(200, b'[]')

        rest_request('GET', 'files/')
        headers = request.call_args[1]['headers']
        self.assertIn('Accept', headers)
        self.assertIn('User-Agent', headers)
        self.assertEqual(headers['Accept'],
                         'application/vnd.uploadcare-v0.4+json')
        self.assertEqual(headers['User-Agent'], user_agent)

        conf.api_version = '0.1'
        rest_request('GET', 'files/')
        headers = request.call_args[1]['headers']
        self.assertIn('Accept', headers)
        self.assertIn('User-Agent', headers)
        self.assertEqual(headers['Accept'],
                         'application/vnd.uploadcare-v0.1+json')
        self.assertEqual(headers['User-Agent'], user_agent)
コード例 #2
0
    def test_request_headers(self, request):
        user_agent = _build_user_agent()

        request.return_value = MockResponse(200, b'[]')

        rest_request('GET', 'files/')
        headers = request.call_args[1]['headers']
        self.assertIn('Accept', headers)
        self.assertIn('User-Agent', headers)
        self.assertEqual(headers['Accept'],
                         'application/vnd.uploadcare-v0.5+json')
        self.assertEqual(headers['User-Agent'], user_agent)

        conf.api_version = '0.1'
        rest_request('GET', 'files/')
        headers = request.call_args[1]['headers']
        self.assertIn('Accept', headers)
        self.assertIn('User-Agent', headers)
        self.assertEqual(headers['Accept'],
                         'application/vnd.uploadcare-v0.1+json')
        self.assertEqual(headers['User-Agent'], user_agent)
コード例 #3
0
def sync_files(arg_namespace):
    session = requests.Session()
    session.headers.update({'User-Agent': _build_user_agent()})

    def _get(url, max_retry=3):
        response = None

        for i in range(max_retry):
            try:
                response = session.get(url, stream=True,
                                       verify=conf.verify_api_ssl)
            except requests.exceptions.ConnectionError as e:
                pprint('Connection Error: {0}'.format(e))
                pprint('Retry..')
                time.sleep(i ** 2)
                continue
            else:
                break

        if response is None:
            pprint('Can\'t fetch URL: {0}'.format(url))
            pprint('Skip it.')
            return

        try:
            response.raise_for_status()
        except requests.HTTPError as e:
            pprint(('Can\'t download file: `{0}`. '
                    'Origin error: {1}').format(url, e))
            return

        return response

    if arg_namespace.starting_point:
        ordering_field = (arg_namespace.ordering or '').lstrip('-')
        if ordering_field in ('', 'datetime_uploaded'):
            arg_namespace.starting_point = parser.parse(
                arg_namespace.starting_point)

    kwargs = dict(uuids=arg_namespace.uuids or None)
    if kwargs['uuids'] is None:
        kwargs.update(dict(
            starting_point=arg_namespace.starting_point,
            ordering=arg_namespace.ordering,
            limit=arg_namespace.limit,
            stored=arg_namespace.stored,
            removed=arg_namespace.removed,
            request_limit=arg_namespace.request_limit,
        ))

    with SyncSession(TrackedFileList(**kwargs)) as files:
        for f in files:
            if f.is_image() and arg_namespace.effects:
                f.default_effects = arg_namespace.effects.lstrip('-/')

            local_filepath = build_filepath(arg_namespace.path, f)
            dirname = os.path.dirname(local_filepath)

            if dirname and not os.path.exists(dirname):
                os.makedirs(dirname)

            if os.path.exists(local_filepath) and not arg_namespace.replace:
                pprint('File `{0}` already exists. '
                       'To override it use `--replace` option'
                       .format(local_filepath))
                continue

            url = f.cdn_url
            response = _get(url)

            if response:
                save_file_locally(local_filepath, response, f.size())