Esempio n. 1
0
 def getStopsBySmsCode(self, sms_code):
     try:
         stopPoint = requests.get(
             f'https://api.tfl.gov.uk/StopPoint/Sms/{sms_code}',
             params=self.keys)
         stopPoint.raise_for_status()
         naptanIds = []
         stopNames = []
         commonNames = []
         for child in stopPoint.json()['children']:
             naptanIds.append(child['naptanId'])
             stopNames.append(child['indicator'])
             commonNames.append(child['commonName'])
         return [naptanIds, stopNames, commonNames]
     except requests.exceptions.HTTPError as e:
         print(e)
         if e.response.status_code == 429:
             raise TooManyRequests()
         elif e.response.status_code == 404:
             raise NotFound()
         else:
             abort(
                 e.response.status_code,
                 'An error occurred while contacting TfL. ' +
                 times.json()['message'])
     except:
         raise Exception()
Esempio n. 2
0
 def response(cls, data, request):
     if isinstance(request, XMLRequest):
         if isinstance(data, TrytonException):
             data = client.Fault(data.code, str(data))
         elif isinstance(data, Exception):
             data = client.Fault(255, str(data))
         else:
             data = (data,)
         return Response(client.dumps(
                 data, methodresponse=True, allow_none=True),
             content_type='text/xml')
     else:
         if isinstance(data, UserWarning):
             return Conflict(data)
         elif isinstance(data, LoginException):
             return Forbidden(data)
         elif isinstance(data, ConcurrencyException):
             return Locked(data)
         elif isinstance(data, RateLimitException):
             return TooManyRequests(data)
         elif isinstance(data, MissingDependenciesException):
             return InternalServerError(data)
         elif isinstance(data, TrytonException):
             return BadRequest(data)
         elif isinstance(data, Exception):
             return InternalServerError(data)
         return Response(data)
Esempio n. 3
0
def on_over_limit(limit):
    ''' 
        Set a nice and readable error message for over the limit requests.
    '''
    raise TooManyRequests(
        'You have exceeded your rate limit. See the X-RateLimit-* response headers for more ' \
        'information on your current rate limit.')
def metrics():
    """
    Provide endpoint for Prometheus.
    """
    if not ('target' in request.args and 'port' in request.args):
        raise BadRequest("Please provide 'target' and 'port' parameters.")

    try:
        1 / int(1 < int(request.args['port']) < 65536)
    except Exception:
        raise BadRequest("Please provide valid TCP port number.")

    target = request.args['target']
    port = request.args['port']
    for thread in threading.enumerate():
        if worker_thread_prefix + target == thread.name:
            return export_all(thread)
    if worker_threads_count() >= max_threads:
        raise TooManyRequests("Limit of threads reached.")
    thread = MokshaMonitorExporter(target, port)
    thread.start()
    # Let's wait for 5 seconds, because it takes some time to connect
    # to moksha.monitoring.socket and receive data.
    time.sleep(5)
    return export_all(thread)
Esempio n. 5
0
 def response(cls, data, request):
     try:
         parsed_data = request.parsed_data
     except BadRequest:
         parsed_data = {}
     if (isinstance(request, JSONRequest)
             and set(parsed_data.keys()) == {'id', 'method', 'params'}):
         response = {'id': parsed_data.get('id', 0)}
         if isinstance(data, TrytonException):
             response['error'] = data.args
         elif isinstance(data, Exception):
             # report exception back to server
             response['error'] = (str(data), data.__format_traceback__)
         else:
             response['result'] = data
     else:
         if isinstance(data, UserWarning):
             return Conflict(data)
         elif isinstance(data, LoginException):
             return Forbidden(data)
         elif isinstance(data, ConcurrencyException):
             return Locked(data)
         elif isinstance(data, RateLimitException):
             return TooManyRequests(data)
         elif isinstance(data, MissingDependenciesException):
             return InternalServerError(data)
         elif isinstance(data, TrytonException):
             return BadRequest(data)
         elif isinstance(data, Exception):
             return InternalServerError(data)
         response = data
     return Response(json.dumps(response,
                                cls=JSONEncoder,
                                separators=(',', ':')),
                     content_type='application/json')
Esempio n. 6
0
 def __enter__(self):
     ret = self.acquire(blocking=False)
     if ret is False:
         raise TooManyRequests(
             'Only {} concurrent request(s) allowed'.format(
                 self._initial_value))
     return ret
Esempio n. 7
0
def result():
    """Renders the result page."""
    form = flask.request.form

    J_factor, M_factor = float(form['J_factor']), float(form['M_factor'])

    if form['direction'] == 'to_dark':
        translate_fn = partial(cam.translate,
                               cond_src=cam.LIGHT_BG,
                               cond_dst=cam.DARK_BG)
        left_bg, right_bg = '#fff', '#000'
    elif form['direction'] == 'to_light':
        translate_fn = partial(cam.translate,
                               cond_src=cam.DARK_BG,
                               cond_dst=cam.LIGHT_BG)
        left_bg, right_bg = '#000', '#fff'
    else:
        raise InternalServerError('Translation direction not specified.')
    translate_fn = partial(translate_fn, J_factor=J_factor, M_factor=M_factor)

    inputs_txt, inputs, outputs_txt = [], [], []
    for color in form['colors'].split('\n'):
        if not color.strip():
            continue
        try:
            inputs_txt.append(color)
            if len(inputs_txt) > 256:
                raise TooManyRequests('Limit 256 colors per request.')
            rgb_src = parse_color(color)
        except ValueError as err:
            raise InternalServerError(str(err))
        inputs.append(rgb_src)

    if not inputs:
        raise InternalServerError('At least one valid color is required.')
    inputs_arr = np.stack(inputs)
    outputs_arr = np.atleast_2d(translate_fn(inputs_arr))

    dump = ''
    csv = 'r_src,g_src,b_src,r_dst,g_dst,b_dst\n'
    for i, color in enumerate(inputs_txt):
        csv += '%d,%d,%d,' % color_to_int(inputs_arr[i])
        csv += '%d,%d,%d\n' % color_to_int(outputs_arr[i])
        if color.count(','):
            outputs_txt.append((color_to_decimal(inputs_arr[i]),
                                color_to_decimal(outputs_arr[i])))
            dump += color_to_decimal(outputs_arr[i]) + '\n'
        else:
            outputs_txt.append(
                (color_to_hex(inputs_arr[i]), color_to_hex(outputs_arr[i])))
            dump += color_to_hex(outputs_arr[i]) + '\n'

    return flask.render_template('result.html',
                                 left_bg=left_bg,
                                 right_bg=right_bg,
                                 outputs=outputs_txt,
                                 text=dump,
                                 csv=csv)
Esempio n. 8
0
def enable_rate_limit(request):
    if request.authz.logged_in:
        return
    limit = settings.API_RATE_LIMIT * settings.API_RATE_WINDOW
    request.rate_limit = get_rate_limit(_get_remote_ip(),
                                        limit=limit,
                                        interval=settings.API_RATE_WINDOW,
                                        unit=60)
    if not request.rate_limit.check():
        raise TooManyRequests("Rate limit exceeded.")
Esempio n. 9
0
def check_post_time_interval(user, model_cls):
    if BypassAntiFlood.is_granted(user):
        return
    last_post = order_query(model_cls.query.filter_by(user_id=user.user_id),
                            by='newest').first()
    if last_post is None:
        return
    diff_secs = (get_now() -
                 get_datetime(last_post.created_at)).total_seconds()
    if int(diff_secs) < current_app.config['MIN_POST_TIME_INTERVAL']:
        raise TooManyRequests('not outside minimum time interval for posting')
def _check_rate_limitation():
    global number_of_max_predictions
    logger.debug('Number of remaining max requests: %s',
                 number_of_max_predictions)
    number_of_max_predictions -= 1
    if (number_of_max_predictions < 0):
        logger.error('Maximal number of prediction requests exceeded: %s',
                     number_of_max_predictions)
        raise TooManyRequests(
            'Maximal number of prediction requests exceeded: {0}'.format(
                number_of_max_predictions))
def _check_rate_limitation():
    global number_of_max_predictions
    logger.debug('Number of remaining max requests: %s',
                 number_of_max_predictions)
    number_of_max_predictions -= 1
    if (number_of_max_predictions % 10 == 0):
        logger.info(str(number_of_max_predictions))
    if (number_of_max_predictions < 0):
        logger.error('Maximal number of prediction requests exceeded: %s',
                     number_of_max_predictions)
        CrowdAiNotifier.too_many_requests()
        raise TooManyRequests(
            'Maximal number of prediction requests exceeded: {0}'.format(
                number_of_max_predictions))
def get_company_overview(search: OverviewSearch) -> CompanyOverview:
    """
    Método responsável por buscar visão geral da empresas, considerando os parâmetros enviados
    :param search: Parâmetros para buscar a visão geral da empresa no Alpha Vantage
    :return: Objeto contendo visão geral da empresa.
    """

    response: dict = requests.get(constants.AV_URL,
                                  params=search.json()).json()
    if response.get(constants.AV_NOTE_KEY):
        raise TooManyRequests('Muitas requisições ao servidor.')

    overview_json: dict = response

    return alpha_vantage_utils.to_company_overview(overview_json)
def search_company(search: SymbolSearch) -> SearchResult:
    """
    Método responsável por buscar símbolos de empresas, considerando a palavra-chave recebida
    por parâmetro
    :param search: Parâmetros para buscar o símbolo da empresa no Alpha Vantage
    :return: Objeto contendo todas as empresas cujo símbolo coincide com a palavra-chave enviada.
    """

    response: dict = requests.get(constants.AV_URL,
                                  params=search.json()).json()
    if response.get(constants.AV_NOTE_KEY):
        raise TooManyRequests('Muitas requisições ao servidor.')

    result_search_json: dict = response.get(AV_SYMBOL_SEARCH_ROOT_KEY)

    return alpha_vantage_utils.to_search_result(result_search_json)
Esempio n. 14
0
def get_weather():
    # Auth
    try:
        user = check_api_key()
    except Unauthorized:
        raise

    # Check throttling
    if not check_ratelimit(user.api_key):
        raise TooManyRequests('Maximum of %s request per hour' %
                              config.LIMIT_REQ_PER_HOUR)

    q = request.args.get('q')
    if not q:
        raise BadRequest('q parameter required')

    # We're only supporting ?q=cityname[,countryname]
    # So we'll just remove anything that's not (alphanum or comma),
    # complain if more than one comma
    # and pass the ?q straight through to OWM (Open Weather Map)
    q = re.sub('[^\w,]', '', q)
    if q.count(',') > 1:
        raise BadRequest(
            'q parameter should be of format cityname[,countryname]')

    # Don't talk to OWM if we're running in dev mode
    if config.DEV_MODE:
        return 'cloudy with a chance of meatballs'

    # Request to OWM
    url = util.construct_owm_req_uri(q)
    res = requests.get(url)

    # Grab the first weather item's description
    # (according to doco looks like we only get more than one if specifying geo region)
    # If the format isn't exactly as expected, or other problem talking to OWM
    # (e.g. bad API key) just log this and return 502 bad gateway
    try:
        j = res.json()
        description = j['weather'][0]['description']
    except:
        # TODO log the invalid req/res from OWM for troubleshooting, raise an alert etc
        # Error returned to client is intentionally vague
        raise BadGateway('Problem talking to weather service')

    return description
def get_time_series(search: TimeSeriesSearch) -> ChartTimeSeries:
    """
    Método responsável por buscar série temporal de pontos por empresa, considerando os filtros recebidos
    por parâmetro
    :param search: Filtros da busca
    :return: Objeto contendo toda a série temporal de pontos da empresa
    """

    response: dict = requests.get(constants.AV_URL,
                                  params=search.json()).json()
    if response.get(constants.AV_ERROR_KEY):
        raise BadRequest(
            'Erro ao requisitar série temporal na API da Alpha Vantage. Verifique os parâmetros enviados.'
        )
    elif response.get(constants.AV_NOTE_KEY):
        raise TooManyRequests('Muitas requisições ao servidor.')
    return alpha_vantage_utils.to_chart_time_series(response, search.timedelta,
                                                    search.date_regex)
def get_global_quote(search: GlobalQuoteSearch) -> GlobalQuote:
    """
    Método responsável por buscar cotação global por empresa, considerando o símbolo recebido
    por parâmetro
    :param search: Parâmetros da empresa reconhecida pelo Alpha Vantage
    :return: Objeto contendo a cotação atual da empresa
    """

    response: dict = requests.get(constants.AV_URL,
                                  params=search.json()).json()
    if response.get(constants.AV_NOTE_KEY):
        raise TooManyRequests('Muitas requisições ao servidor.')

    global_quote_json: dict = response.get(AV_GLOBAL_QUOTE_ROOT_KEY)

    if not global_quote_json:
        raise BadRequest(
            'Cotação global vazia. Verifique os parâmetros enviados.')

    return alpha_vantage_utils.to_global_quote(global_quote_json)
Esempio n. 17
0
 def getStopLiveArrivals(self, naptan_id):
     try:
         times = requests.get(
             f'https://api.tfl.gov.uk/StopPoint/{naptan_id}/arrivals',
             params=self.keys)
         times.raise_for_status()
         return times.json()
     except requests.exceptions.HTTPError as e:
         print(e)
         if e.response.status_code == 429:
             raise TooManyRequests()
         elif e.response.status_code == 404:
             raise NotFound()
         else:
             abort(
                 e.response.status_code,
                 'An error occurred while contacting TfL. ' +
                 times.json()['message'])
     except:
         raise Exception()
Esempio n. 18
0
def create_department():
    form = CreateDepartmentForm()
    if not current_user.can_create_departments:
        raise Forbidden

    # Ensure the user has no other pending organisation applications
    for dep in current_user.departments:
        if dep.verification_status == t.VerificationStatus.pending:
            raise TooManyRequests(
                description=
                "You cannot register more than one organisation at once")

    if form.validate_on_submit():

        # Find the corresponding organisation
        org = Organisation.query.filter_by(
            display_name=form.organisation_name.data).first()

        # Create a pending department
        supporting_evidence = [DepartmentFile(document=f) \
            for f in form.supporting_evidence.data]
        dep = Department(
            display_name=form.department_name.data,
            description='',
            users=[current_user],
            organisation=org,
            verification_status=t.VerificationStatus.pending,
            temp_organisation=form.organisation_name.data if org is None \
                else None,
            supporting_evidence=supporting_evidence)

        db.session.add(dep)
        db.session.commit()

        return redirect(url_for('profile.edit_profile'))
    return render_template('profile/create_department.html', form=form)
Esempio n. 19
0
 def __call__(self, environ, start_response):
     response = Response()
     response.mimetype = 'application/json'
     response.status_code = 200
     if environ["PATH_INFO"] == "/":
         return redirect("/index.html")
     elif environ["PATH_INFO"].startswith("/search"):
         what = environ["PATH_INFO"].split("/")
         if len(what) != 3:
             return BadRequest()
         what = urllib.parse.quote(what[2])
         dwl = self.getpage(self.search_url + what)
         authors = ET.fromstring(dwl)
         data = []
         for a in authors:
             data.append({"name": a.text, "url": a.get("urlpt")})
         response.data = json.dumps(data)
     elif environ["PATH_INFO"].startswith("/collaborators"):
         who = environ["PATH_INFO"].split("/")
         if len(who) < 3:
             return BadRequest()
         who = "/".join(who[2:])
         if who.startswith(self.person_url_human):
             who = who[len(self.person_url_human):]
         try:
             dwl = self.getpage(self.collaborators_url + who)
         except urllib.error.HTTPError as e:
             if e.code == 429:
                 return TooManyRequests()
             return NotFound()
         authors = ET.fromstring(dwl)
         data = []
         for a in authors:
             data.append({"name": a.text, "url": a.get("urlpt")})
         response.data = json.dumps(data)
     elif environ["PATH_INFO"].startswith("/geolocate"):
         who = environ["PATH_INFO"].split("/")
         if len(who) < 3:
             return BadRequest()
         who = "/".join(who[2:])
         if who.startswith(self.person_url_human):
             who = who[len(self.person_url_human):]
         try:
             dwl = self.getpage(self.person_url + who)
         except urllib.error.HTTPError as e:
             if e.code == 429:
                 return TooManyRequests()
             return NotFound()
         data = ET.fromstring(dwl)
         if 'f' in data.attrib:
             who2 = data.attrib.get('f')
             print(who2)
             try:
                 dwl = self.getpage(self.person_url + who2)
             except urllib.error.HTTPError as e:
                 if e.code == 429:
                     return TooManyRequests()
                 return NotFound()
             data = ET.fromstring(dwl)
         person_info = data.find("person")
         homepage = person_info.find("url")
         homepage = homepage.text if homepage is not None else None
         if homepage is None:
             homepage = self.get_homepage_from_google(who)
         if homepage is None:
             data = {"url": who}
         else:
             try:
                 domain = homepage.split("/")[2]
                 ip = self.gethostbyname(domain)
                 info = self.db.city(ip)
                 data = {
                     "url": who,
                     "country": info.country.name,
                     "coords":
                     (info.location.latitude, info.location.longitude)
                 }
             except:
                 traceback.print_exc()
                 data = {"url": who}
         response.data = json.dumps(data)
     else:
         return BadRequest()
     return response