Пример #1
0
def index():
    client = HTTPClient()
    try:
        response = client.fetch(USERS_API)
        users = json.loads(response.body.decode())['data']['users']
        return users
    except Exception as e:
        application.error('error on recover users:' + str(e))
        return []
Пример #2
0
def create(attributes):
    body = json.dumps(attributes)
    request = HTTPRequest(USERS_API, 'POST', body=body)
    client = HTTPClient()

    try:
        response = client.fetch(request)
        return json.loads(response.body.decode())['data']['user']
    except Exception as e:
        application.error('error on create user:' + str(e))
        return None
Пример #3
0
def get(uid):
    uri = "%s/%s" % (USERS_API, uid)
    client = HTTPClient()

    try:
        response = client.fetch(uri)
        attributes = json.loads(response.body.decode())['data']['user']
        return attributes
    except Exception as e:
        application.error('error on recover user:' + str(e))
        return None
Пример #4
0
def index():
    uri = "%s"%(BOOKS_API)
    client = HTTPClient()

    try:
        response = client.fetch(uri)
        books = json.loads(response.body.decode())['data']
        return books
    except Exception as e:
        application.error('error on recover books:' + str(e))
        return None
Пример #5
0
def get(isbn):
    uri = "%s/%s"%(BOOKS_API, isbn)
    client = HTTPClient()

    try:
        response = client.fetch(uri)
        attributes = json.loads(response.body.decode())['data']['book']
        return attributes
    except Exception as e:
        application.error('error on recover book:' + str(e))
        return None
def create(attributes):
    body = json.dumps(attributes)
    request = HTTPRequest(COMMENTS_API, 'POST', body=body)
    client = HTTPClient()

    try:
        response = client.fetch(request)
        json.loads(response.body.decode())
    except:
        application.error('error on create comment:' + str(e))
        return None
def get(resource_type, resource_uid):
    uri = "%s/%s/%s"%(COMMENTS_API, resource_type, resource_uid)
    client = HTTPClient()

    try:
        response = client.fetch(uri)
        comments = json.loads(response.body.decode())['data']['comments']
        for comment in comments:
            comment['user'] = get_user(comment['user'])
        return comments
    except Exception as e:
        application.error('error on recover comments:' + str(e))
        return []
 def post(self):
     comment = {
         "comment": self.get_body_argument('comment'),
         "user": self.get_body_argument('user'),
         "resource_type": self.get_body_argument('resource_type'),
         "resource_uid": self.get_body_argument('resource_uid')
     }
     try:
         create(comment)
         self.success({'message': 'Comentário criado com sucesso!'})
     except Exception as e:
         application.error('error on create comment in api:' + str(e))
         self.set_status(422)
         self.fail({'message': 'Não foi possível criar o comentário'})
 def post(self):
     user = {
         "name": self.get_body_argument('name'),
         "email": self.get_body_argument('email'),
         "password": self.get_body_argument('password')
     }
     try:
         result = create(user)
         self.set_secure_cookie("user", result['uid'])
         self.success({'message': 'Usuário criado com sucesso!'})
     except Exception as e:
         application.error('error on create user in api: ' + str(e))
         self.set_status(422)
         self.fail({'message': 'Não foi possível criar o usuário'})