async def get(self, request, *args, **kwargs):
     '''Get cosmoz stations.'''
     property_filter = request.args.getlist('property_filter', None)
     if property_filter:
         property_filter = str(next(iter(property_filter))).split(',')
         property_filter = [p for p in property_filter if len(p)]
     count = request.args.getlist('count', None)
     if count:
         count = min(int(next(iter(count))), MAX_RETURN_COUNT)
     else:
         count = 1000
     offset = request.args.getlist('offset', None)
     if offset:
         offset = min(int(next(iter(offset))), MAX_RETURN_COUNT)
     else:
         offset = 0
     obs_params = {
         "property_filter": property_filter,
         "count": count,
         "offset": offset,
     }
     res = await get_stations_mongo(obs_params, json_safe='orjson')
     if use_body_bytes:
         resp = HTTPResponse(None,
                             status=200,
                             content_type='application/json',
                             body_bytes=fast_dumps(res,
                                                   option=orjson_option))
     else:
         resp = HTTPResponse(fast_dumps(res, option=orjson_option),
                             status=200,
                             content_type='application/json')
     return resp
Ejemplo n.º 2
0
 def output_json_fast_ujson(request, data, code, headers=None):
     current_app = request.app
     if current_app.debug:
         return output_json_pretty(request, data, code, headers=headers)
     settings = current_app.config.get('RESTPLUS_JSON', {})
     dumped = fast_dumps(data, **settings) + "\n"
     resp = text(dumped, code, headers, content_type='application/json')
     return resp
Ejemplo n.º 3
0
 def output_json_fast_orjson(request, data, code, headers=None):
     current_app = request.app
     if current_app.debug:
         return output_json_pretty(request, data, code, headers=headers)
     settings = current_app.config.get('RESTPLUS_JSON', {})
     dumped = fast_dumps(
         data, option=orjson_opts, default=orjson_default, **
         settings) + b"\n"
     resp = HTTPResponse(None,
                         code,
                         headers,
                         content_type='application/json',
                         body_bytes=dumped)
     return resp
    async def post(self, request, context):
        rcontext = context.for_request(request)
        shared_context = context.shared
        shared_rcontext = shared_context.for_request(request)

        action = request.args.getlist('action', None)
        if action:
            action = next(iter(action))
        else:
            raise RuntimeError("action is mandatory.")
        metrics_override = {
            'method': 'GET',
            'status': 200,
            'skip_response': True,
        }
        referer = request.headers.getall('Referer')
        if referer:
            referer = next(iter(referer))
            try:
                (scheme, netloc, path, query, fragment) = urlsplit(referer)
                metrics_override['host'] = netloc
                metrics_override['path'] = path
            except:
                pass
        else:
            # No referer, this will be hard to track
            pass
        if action == "page_visit":
            time = request.args.getlist('time', None)
            if time:
                time = next(iter(time))
                try:
                    metrics_override['datetime_start_iso'] = time
                    t = datetime_from_iso(time)
                    metrics_override['timestamp_start'] = t.timestamp()
                    metrics_override['datetime_start'] = t
                except Exception:
                    pass
            page = request.args.getlist('page', None)
            if page:
                page = next(iter(page))
                metrics_override['path'] = page
            query = request.args.getlist('query', None)
            if query:
                query = next(iter(query))
            else:
                query = None
            metrics_override['qs'] = query
        else:
            raise NotImplementedError(action)
        shared_rcontext['override_metrics'] = metrics_override
        res = {"result": "success"}
        if use_body_bytes:
            resp = HTTPResponse(None,
                                status=200,
                                content_type='application/json',
                                body_bytes=fast_dumps(res,
                                                      option=orjson_option))
        else:
            resp = HTTPResponse(fast_dumps(res, option=orjson_option),
                                status=200,
                                content_type='application/json')
        return resp
    async def get(self, request, *args, station_no=None, **kwargs):
        '''Get recent cosmoz records.'''
        return_type = match_accept_mediatypes_to_provides(
            request, self.accept_types)
        format = request.args.getlist('format', None)
        if not format:
            format = request.args.getlist('_format', None)
        if format:
            format = next(iter(format))
            if format in self.accept_types:
                return_type = format
        if return_type is None:
            return_type = "application/json"
        if station_no is None:
            raise RuntimeError("station_no is mandatory.")
        station_no = int(station_no)
        processing_level = request.args.getlist('processing_level', None)
        if processing_level:
            processing_level = int(next(iter(processing_level)))
        else:
            processing_level = 4
        excel_compat = request.args.getlist('excel_compat',
                                            [False])[0] in TRUTHS
        not_json = return_type != "application/json"
        if not not_json:
            property_filter = request.args.getlist('property_filter', None)
            if property_filter:
                property_filter = str(next(iter(property_filter))).split(',')
                property_filter = [p for p in property_filter if len(p)]
        else:
            property_filter = "*"
        count = request.args.getlist('count', None)
        if not not_json:
            fallback_count = 2000
        else:
            fallback_count = MAX_RETURN_COUNT
        if count:
            try:
                count = min(int(next(iter(count))), MAX_RETURN_COUNT)
            except ValueError:
                count = fallback_count
        else:
            count = fallback_count
        obs_params = {
            "processing_level": processing_level,
            "property_filter": property_filter,
            "count": count,
        }
        if not not_json:
            json_safe = 'orjson'
            try:
                res = get_last_observations_influx(station_no, obs_params,
                                                   json_safe, False)
                if use_body_bytes:
                    resp = HTTPResponse(None,
                                        status=200,
                                        content_type=return_type,
                                        body_bytes=fast_dumps(
                                            res, option=orjson_option))
                else:
                    resp = HTTPResponse(fast_dumps(res, option=orjson_option),
                                        status=200,
                                        content_type=return_type)
                return resp
            except Exception as e:
                print(e)
                raise e
        headers = {'Content-Type': return_type}
        jinja2 = get_jinja2_for_api(self.api)
        if return_type == "text/csv":
            if processing_level == 0:
                template = "raw_data_csv.html"
            else:
                template = "level{}_data_csv.html".format(processing_level)
            headers['Content-Disposition'] = "attachment; filename=\"station{}_level{}.csv\"" \
                .format(str(station_no), str(processing_level))
        elif return_type == "text/plain":
            headers = {'Content-Type': return_type}
            if processing_level == 0:
                template = "raw_data_txt.html"
            else:
                template = "level{}_data_txt.html".format(processing_level)
            headers['Content-Disposition'] = "attachment; filename=\"station{}_level{}.txt\"" \
                .format(str(station_no), str(processing_level))
        else:
            raise RuntimeError("Invalid Return Type")

        async def streaming_fn(response):
            nonlocal template
            nonlocal station_no
            nonlocal obs_params
            nonlocal request
            nonlocal excel_compat
            res = get_last_observations_influx(station_no, obs_params, False,
                                               excel_compat)
            if PY_36:
                r = await jinja2.render_string_async(template, request, **res)
            else:
                r = jinja2.render_string(template, request, **res)
            await response.write(r)

        return stream(streaming_fn,
                      status=200,
                      headers=headers,
                      content_type=return_type)
 async def get(self, request, *args, station_no=None, **kwargs):
     '''Get cosmoz station calibrations.'''
     if station_no is None:
         raise RuntimeError("station_no is mandatory.")
     station_no = int(station_no)
     return_type = match_accept_mediatypes_to_provides(
         request, self.accept_types)
     format = request.args.getlist('format', None)
     if not format:
         format = request.args.getlist('_format', None)
     if format:
         format = next(iter(format))
         if format in self.accept_types:
             return_type = format
     if return_type is None:
         return ServiceUnavailable("Please use a valid accept type.")
     if return_type == "application/json":
         property_filter = request.args.getlist('property_filter', None)
         if property_filter:
             property_filter = str(next(iter(property_filter))).split(',')
             property_filter = [p for p in property_filter if len(p)]
     else:
         # CSV and TXT get all properties, regardless of property_filter
         property_filter = "*"
     obs_params = {
         "property_filter": property_filter,
     }
     json_safe = 'orjson' if return_type == "application/json" else False
     jinja_safe = 'txt' if return_type == "text/plain" else False
     jinja_safe = 'csv' if return_type == "text/csv" else jinja_safe
     res = await get_station_calibration_mongo(station_no,
                                               obs_params,
                                               json_safe=json_safe,
                                               jinja_safe=jinja_safe)
     if return_type == "application/json":
         if use_body_bytes:
             resp = HTTPResponse(None,
                                 status=200,
                                 content_type=return_type,
                                 body_bytes=fast_dumps(
                                     res, option=orjson_option))
         else:
             resp = HTTPResponse(fast_dumps(res, option=orjson_option),
                                 status=200,
                                 content_type=return_type)
         return resp
     headers = {'Content-Type': return_type}
     jinja2 = get_jinja2_for_api(self.api)
     if return_type == "text/csv":
         template = 'site_data_cal_csv.html'
     elif return_type == "text/plain":
         template = 'site_data_cal_txt.html'
     else:
         raise RuntimeError(
             "Cannot determine template name to use for response type.")
     if PY_36:
         return await jinja2.render_async(template,
                                          request,
                                          headers=headers,
                                          **res)
     else:
         return jinja2.render(template, request, headers=headers, **res)