def registration_view(request): serializer = serializers.RegistrationSerializer(data=request.data) if not serializer.is_valid(): return Response(status=400, data=serializer.errors) user = serializer.save() confirm_url = 'http://%s:%s/v1/user/confirm/%s' % ( settings.SERVICE_HOST, settings.SERVICE_PORT, user.confirm_url_id, ) try: notification_response = http_request( method='POST', url='http://%s:%s/%s' % ( settings.NOTIFICATION_HOST, settings.NOTIFICATION_PORT, 'v1/send-registartion-email', ), json={ 'email': user.email, 'confirm_url': confirm_url }, ) except: return Response(status=500, data={'text': 'internal server error'}) if not (notification_response.status_code == 200): return Response(status=500, data={'text': 'internal server error'}) return Response(data={'username': user.username})
def request_api(self, method, **kwargs): ''' Requests http://codeforces.com/api/<method_name> and returns json object with info from site. Type of method must be a string. Uses key and secret if provided. Make sure you have set correct system time. Checks the request.Response object for valid answer. Raises ConnectionError if request is not OK. Cryptographically secure. Creates last_api dict. Contains JSON with last API request result. Example: cf.request_api('user.info', handles=['tourist', 'Petr']) ''' typeChecker(method, str) self.last_api = http_request(self._api_link + method + '?' + self._url_api_request(method, **kwargs)) self._check_request(self.last_api) try: self.last_api = self.last_api.json() except JSONDecodeError: raise UnexpectedResponce('Codeforces API has returned not json') if self.last_api.get('status') is None: raise UnexpectedResponce( 'There is no \"status\" field in response. Codeforces API changed?' ) if self.last_api['status'] != 'OK': raise ConnectionError( 'Codeforces API error: \ {}', self.last_api['comment']) return self.last_api
def get_page(self, link, **params): ''' Requests and returns HTML page by link with params: http://codeforces.com/<link> ''' params['locale'] = self.language self.last_page = http_request(self._site_link + link, params=params) self._check_request(self.last_page) return self.last_page.text
def proxy_view(request, url): user = request.user headers = dict(request.headers) headers.update({ 'X-Username': user.username, 'X-Email': user.email, }) headers.pop('Authorization') try: response = http_request( method=request.method, url='http://%s:%s/%s' % (settings.PROXY_HOST, settings.PROXY_PORT, url), params=request.query_params, headers=headers, json=request.data) return Response( data=response.json(), status=response.status_code, content_type=response.headers['Content-Type'], ) except: return Response(status=500, data={'text': 'Internal server error'})
def fetch_and_store_from_google(self, object_id: str, access_token: str): res = http_request('GET', self._google_userinfo_endpoint, headers={"Authorization": f'Bearer {access_token}'}) info_dict = parse_json(res.text) user_info = UserInfoModel(**{ 'object_id': object_id, 'username': info_dict.get("email", None), 'name': info_dict.get("name", None), 'first_name': info_dict.get("given_name", None), 'last_name': info_dict.get("family_name", None), 'email': info_dict.get("email", None), 'email_verified': info_dict.get("email_verified", None), 'photo_url': info_dict.get("picture", None) }) storage_entry = StorageEntryModel(**{ 'id': object_id, 'partition_key': 0, 'data': user_info.to_dict(), 'etag': '*' }) self._storage.add_or_update(storage_entry)
def request(self, method, url, headers, data=None): response = http_request(method, url, headers=headers, data=data) return response.json()