Ejemplo n.º 1
0
    def _save(self, resp, req, user_id):
        data = utils.decode_obj(req.stream.read())

        if not self.user_db.exists(user_id):
            raise falcon.HTTPError(
                falcon.HTTP_404,
                'User not found',
                'Request did not return any records'
            )

        if 'url' not in data:
            raise falcon.HTTPError(
                falcon.HTTP_400,
                'No identifier',
                'Could not decode the request body. The JSON was incorrect'
            )

        url = data['url']
        id_url = self.url_db.create_id_url()
        if self.user_db.user_has_url(user_id, id_url):
            raise falcon.HTTPError(
                falcon.HTTP_409,
                'The URL exists to the user',
                'Please try with another URL'
            )

        new_url = self.url_db.save(url, id_url)
        self.user_db.update_user(user_id, id_url)
        self.return_body(resp, new_url, falcon.HTTP_201)
Ejemplo n.º 2
0
 def get(self, hash_name, key):
     if hash_name not in key:
         key = '%s%s' % (hash_name, key)
     value = Redis(connection_pool=self._redis_pool).get(key)
     try:
         return utils.decode_obj(value)
     except:
         return value
Ejemplo n.º 3
0
 def post_user(self):
     self.user_id = utils.fake_name()
     path = '/users'
     body = utils.encode_obj({'id': self.user_id})
     headers = {'Content-Type': 'application/json'}
     response = self.simulate_post(path, body=body, headers=headers)
     response_user_id = utils.decode_obj(response[0])['id']
     self.assertEquals(self.user_id, response_user_id)
     self.assertEqual(self.srmock.status, falcon.HTTP_201)
Ejemplo n.º 4
0
    def _save(self, resp, req):
        data = decode_obj(req.stream.read())

        if 'id' not in data:
            raise falcon.HTTPError(
                falcon.HTTP_400, 'No identifier',
                'Could not decode the request body. The JSON was incorrect')

        user_id = data['id']
        if not self.db.exists(user_id):
            new_user = self.db.save(user_id)
            if new_user:
                user_json = {}
                user_json['id'] = new_user.id
                self.return_body(resp, user_json, falcon.HTTP_201)
            else:
                raise falcon.HTTPError(falcon.HTTP_400, 'User not created',
                                       'Please try again or contact support')
        else:
            resp.status = falcon.HTTP_409
Ejemplo n.º 5
0
 def get_stats(self):
     path = '/stats'.format(self.user_id)
     self.response_get_stats = self.simulate_request(path)
     self.assertEqual(self.srmock.status, falcon.HTTP_200)
     self.system_stats = utils.decode_obj(self.response_get_stats[0])
Ejemplo n.º 6
0
 def get_urls(self):
     id_surl = utils.decode_obj(self.response[0])['shorturl'].split('/')[-1]
     path = '/urls/{0}'.format(id_surl)
     self.simulate_request(path)
     self.assertEqual(self.srmock.status, falcon.HTTP_301)
Ejemplo n.º 7
0
 def get_stats(self):
     path = '/stats/{0}'.format(self.id_surl)
     self.response_get_stats = self.simulate_request(path)
     self.assertEqual(self.srmock.status, falcon.HTTP_200)
     self.hits = utils.decode_obj(self.response_get_stats[0])['hits']