def ws_request(url, method='GET', content_type=None, body=None, token=None, **kwargs): ws_base_url = app.config.get('WEB_WS_SERVICE_URL') if ws_base_url: response = requests.get(urljoin(ws_base_url, url), params=kwargs).content else: def start_response(status, headers): meta['status'] = status meta['headers'] = headers # Make local in-process request at top of WSGI stack env = request.environ.copy() if content_type: env['CONTENT_TYPE'] = content_type if body: env['CONTENT_LENGTH'] = len(body) env['wsgi.input'] = StringIO(body) env['PATH_INFO'] = url env['REQUEST_METHOD'] = method env['QUERY_STRING'] = urlencode(kwargs) if token: env['HTTP_AUTHORIZATION'] = 'Bearer %s' % str(token) if not token and 'API_SUBDOMAIN' in app.config: env['HTTP_HOST'] = '.'.join((app.config['API_SUBDOMAIN'], app.config['SERVER_NAME'])) meta = {} response = u''.join(app.wsgi_app(env, start_response)) if meta['status'] == '404 NOT FOUND': abort(404) return response and json.loads(response)
def _create_user(self, form): if request.files.get('avatar'): avatar = resize_and_upload(request.files['avatar'], 'AVATAR') elif form.avatar_url.data: avatar_file = StringIO(requests.get(form.avatar_url.data).content) avatar = resize_and_upload(avatar_file, 'AVATAR') else: avatar = '' user = User( username=form.username.data.strip(), password_hash='', first_name=form.first_name.data, last_name=form.last_name.data, description=form.description.data, site_url=form.site_url.data, email='', date_of_birth=date(1900, 1, 1), avatar=avatar, refresh_token='', locale=form.locale.data or 'en-us', is_active=True) self.record_action('created', user) db.session.add(user) db.session.commit() return user.id
def complete(query, **params): url = 'http://www.google.com/complete/search' params = dict(client='youtube', ds='yt', q=query, **params) response = requests.get(url, params=params) try: response.raise_for_status() except Exception, e: if hasattr(e, 'response'): log.error('complete request failed (%d): %s', e.response.status_code, e.response.text) raise
def validate(self): if not super(ImportForm, self).validate(): return if request.files.get('cover'): cover = request.files['cover'] elif self.cover_url.data: cover = StringIO(requests.get(self.cover_url.data).content) else: cover = None if self.user.data: try: self.cover.data = resize_and_upload(cover, 'CHANNEL') if cover else '' except IOError, e: self.cover.errors = [str(e)] return UserCoverArt(cover=self.cover.data, owner=self.user.data).save()
def romeo_ws_request(url): ws_base_url = app.config['ROMEO_WS_URL'] response = requests.get(urljoin(ws_base_url, url)) if response.status_code == 404: abort(404) return response.json()
def get_external_resource(url): """Get the specified http resource and return a FileStorage-wrapped buffer.""" response = requests.get(url) response.raise_for_status() return FileStorage(StringIO(response.content), response.url)
def _get_external_data(self): r = requests.get(app.config['ROMEO_ACCOUNT_VERIFY_URL'], allow_redirects=False, cookies=dict(romeo=self.token)) if r.status_code == 200: return r.json()
def avatar(self): image_url = self._user_data['profile_image_url'].replace('_normal.', '.') r = requests.get(image_url) if r.status_code == 200 and r.headers.get('content-type', '').startswith('image/'): return StringIO(r.content) return ''
def avatar(self): r = requests.get(facebook.FACEBOOK_PICTURE_URL % self.id) if r.status_code == 200 and r.headers.get('content-type', '').startswith('image/'): return StringIO(r.content) return ''