Example #1
0
    def get(self, resource, params=None):
        logger.debug('Getting resource %s', resource)
        if params is None:
            params = {}

        # remove params from resource URI (needed for paginated stuff)
        parsed_uri = urlparse(resource)
        qs = parsed_uri.query
        resource = urlunparse(parsed_uri._replace(query=''))
        prms = {}
        for tup in parse_qsl(qs):
            prms[tup[0]] = tup[1]

        # params supplied to self.get() override parsed params
        for key in params:
            prms[key] = params[key]

        # check cache
        key = (resource, frozenset(self._session.headers.items()),
               frozenset(prms.items()))
        cached = self.cache.get(key)
        if cached and cached['expires'] > time.time():
            logger.debug('Cache hit for resource %s (params=%s)', resource,
                         prms)
            return cached['payload']
        elif cached:
            logger.debug('Cache stale for resource %s (params=%s)', resource,
                         prms)
            self.cache.invalidate(key)
        else:
            logger.debug('Cache miss for resource %s (params=%s', resource,
                         prms)

        logger.debug('Getting resource %s (params=%s)', resource, prms)
        try:
            res = self._session.get(resource,
                                    params=prms,
                                    timeout=APIConnection.TIMEOUT)
        except requests.exceptions.RequestException:
            raise APIException("No response from server")

        if res.status_code != 200:
            raise APIException("Got unexpected status code from server: %i" %
                               res.status_code)

        ret = res.json()

        # cache result
        key = (resource, frozenset(self._session.headers.items()),
               frozenset(prms.items()))
        expires = self._get_expires(res)
        if expires > 0:
            self.cache.put(key, {
                'expires': time.time() + expires,
                'payload': ret
            })

        return ret
Example #2
0
    def post(self, resource, data, params=None):
        try:
            res = self._session.post(resource,
                                     data=data,
                                     params=params,
                                     timeout=APIConnection.TIMEOUT)
        except requests.exceptions.RequestException:
            raise APIException("No response from server")

        if res.status_code != 200:
            raise APIException("Got unexpected status code from server: %i" %
                               res.status_code)

        return res
Example #3
0
 def _authorize(self, params):
     auth = text_(
         base64.b64encode(bytes_("%s:%s" % (self.client_id, self.api_key))))
     headers = {"Authorization": "Basic %s" % auth}
     try:
         res = self._session.post("%s/token" % self._oauth_endpoint,
                                  params=params,
                                  headers=headers,
                                  timeout=APIConnection.TIMEOUT)
     except requests.exceptions.RequestException:
         raise APIException("No response from server")
     if res.status_code != 200:
         raise APIException("Got unexpected status code from API: %i" %
                            res.status_code)
     return res.json()
Example #4
0
File: eve.py Project: mecnoc/Pyfa
 def _authorize(self, params):
     auth = text_(
         base64.b64encode(bytes_("%s:%s" % (self.client_id, self.api_key))))
     headers = {"Authorization": "Basic %s" % auth}
     res = self._session.post("%s/token" % self._oauth_endpoint,
                              params=params,
                              headers=headers)
     if res.status_code != 200:
         raise APIException("Got unexpected status code from API: %i" %
                            res.status_code)
     return res.json()
Example #5
0
 def get(self):
     try:
         token = request.user.get_verification()
     except Exception as e:
         raise APIException(e.message)
     data = {
         'username': request.user.username,
         'url': urljoin(config.SITE_URL, config.VERIFY_URL),
         'token': token,
     }
     send_mail_template('verification', [request.user.email], data=data)
     return 'send verification email'
Example #6
0
 def get(self):
     try:
         limit = int(request.args.get('limit', -1))
         offset = int(request.args.get('offset', -1))
         if limit < 0:
             limit = config.DEFAULT_COUNT_MESSAGES
         if offset < 0:
             offset = 0
     except Exception as e:
         logger.error(e)
         raise APIException('wrong arguments')
     messages = ChatMessage.select()\
                    .where(ChatMessage.chat == None)\
                    .order_by(-ChatMessage.date_created)\
                    .offset(offset)\
                    .limit(limit)
     return {
         'messages': [MessageSerializer(m).calc() for m in messages],
     }