def list_links(self, access_token): """List of all user links. :param access_token: :return: dict """ user_path = '/api/user/links' headers = self.header headers.update( dict(JWT_Authorization='Bearer {}'.format(access_token))) r = requests.get(self.rest_url + user_path, headers=headers) resp_obj = r.json() if int(r.status_code) == 401: if resp_obj['sub_status'] == 101: self.refresh_access_token() if self.header is None: raise UnAuthorized('Please login again to continue') r = requests.get(self.rest_url + user_path, headers=self.header) resp_obj = r.json() if r.status_code // 100 != 2: if r.status_code == 401: return UnAuthorized('Please login again to continue') raise ObjectNotFound(resp_obj) links = resp_obj for link in links: if link.get('short_code'): link['short_url'] = self.makeurl( self.HOSTNAME, link['short_code']) return links
def get_longurl_data(self, long_url): """ :param long_url: :return: """ url_path = '/api/shorten?url=' + long_url r = requests.get(self.rest_url + url_path, headers=self.header) if r.status_code // 100 != 2: raise ObjectNotFound(r.json()) return r.json()
def error_object_from_response(response): error_object = None if response.status_code == BAD_REQUEST: error_object = InvalidInput(response.json()) elif response.status_code == FORBIDDEN: error_object = UnAuthorized(response.json()) elif response.status_code == RESOURCE_EXPIRED: error_object = LinkExpired(response.json()) elif response.status_code // 100 != 2: error_object = ObjectNotFound(response.json()) return error_object