Ejemplo n.º 1
0
 def __call__(self, environ, start_response):
     """Invoke the Controller"""
     # WSGIController.__call__ dispatches to the Controller method
     # the request is routed to. This routing information is
     # available in environ['pylons.routes_dict']
     try:
         return WSGIController.__call__(self, environ, start_response)
     finally:
         Session.remove()
Ejemplo n.º 2
0
def init_model(engine):
    """Call me before using any of the tables or classes in the model"""
    Session.configure(bind=engine)

    apikey_table = sa.Table('apikey', meta.metadata, autoload=True, autoload_with=engine)
    APIKey.table = apikey_table
    orm.mapper(APIKey, APIKey.table)

    apicall_table = sa.Table('apicall', meta.metadata, autoload=True, autoload_with=engine)
    APICall.table = apicall_table
    orm.mapper(APICall, APICall.table)
    
    example_table = sa.Table('example', meta.metadata, autoload=True, autoload_with=engine)
    Example.table = example_table
    orm.mapper(Example, Example.table)
Ejemplo n.º 3
0
    def tag(self):

        text = request.params.get('text')
        apikey = request.params.get('key')
        language = request.params.get('language')
        channel = request.params.get('channel')
        referrer = request.headers.get('REFERER', '/')
        host = get_host(referrer)
        ip_address = request.environ.get("X_FORWARDED_FOR",
                                         request.environ.get("HTTP_X_FORWARDED_FOR",
                                                             request.environ.get("REMOTE_ADDR")))

        log.info('apikey=%s referrer=%s host=%s', apikey, referrer, host)


        # The text parameter is required for the tag method
        if not text:
            log.info('Missing text parameter.')
            return "001 Missing Parameter: Required parameter is not supplied (text)."

        log.info('Text to be tagged: %s', text)
        tags = TweetTagger.tag(text)
        log.info('Tags extracted: %s', str(tags))

        # Now update the call count on the key row...
        key = session.get('key')
        if key:
            key.calls = key.calls + 1
            key.last_call = datetime.datetime.now()

        # Log the api call
        apicall = APICall()
        apicall.parameters = text
        apicall.result = simplejson.dumps(tags)
        if key:
            apicall.apikey_id = key.id
        apicall.method = 'tag'
        apicall.http_method = request.method
        apicall.called_from = ip_address
        Session.add(apicall)

        Session.commit()
        response.headers['Content-Type'] = 'application/json'
        return simplejson.dumps(tags)
Ejemplo n.º 4
0
def _throttle(f, *args, **kwds):
    text = request.params.get('text')
    apikey = request.params.get('key')
    language = request.params.get('language')
    channel = request.params.get('channel')
    referrer = request.headers.get('REFERER', '/')
    host = get_host(referrer)
    ip_address = request.environ.get("X_FORWARDED_FOR",
                                     request.environ.get("HTTP_X_FORWARDED_FOR",
                                                         request.environ.get("REMOTE_ADDR")))

    allow_keyless_calls = config.get('allow_keyless_calls') and \
        config.get('allow_keyless_calls').lower() == 'true'

    if not apikey and not allow_keyless_calls:
        # From Swift River API docs:
        # 007 Access denied. Your API key is no longer valid.  Please contact the administrator.
        # 008 Access denied. You need an API key to perform that task.  Please contact the administrator.
        response.status = '401 Unauthorized'
        return "008 Access denied. You need an API key to perform that task. Please contact the administrator."
        
    if apikey:
        # Now load the key from the db if it exists...
        key = Session.query(APIKey).filter_by(keystr=apikey).first()
        if not key:
            log.info('No matching key was found in the db.')
            response.status = '401 Unauthorized'
            return "008 Access denied. You need an API key to perform that task.  Please contact the administrator."

    # Check that the key is valid for the referrer host...
    if apikey and key and (key.valid_domains != host and key.valid_domains != '*'):
        log.info("A Key was found but the referring host is invalid.")
        response.status = '401 Unauthorized'
        return "008 Access denied. You need an API key to perform that task.  Please contact the administrator."
            
    # Now check the number of calls in the last minute...
    query = select([func.count(APICall.table.c.id)])
    query = query.where("called_at > now() - interval 1 minute") 
    if apikey and key:
        # Note that if apikey was supplied and it doesnt exist we would have exited earlier...
        query = query.where(APICall.table.c.apikey_id==key.id)
    else:
        query = query.where(APICall.table.c.called_from==ip_address)
    results = Session.execute(query).fetchone()
    log.info('number of previous calls: %s', str(results))

    prev_calls = results[0]
    if not apikey and prev_calls >= 60: 
        # Keyless calls allow max of 60 per minute per ip address
        log.info("Over throttle limit for keyless calls from ip %s.", ip_address)
        response.status = '401 Unauthorized'
        return "008 Access denied. You have exceeded the maximum allowed calls. Please try again later"
        
    if apikey and key:
        session['key'] = key
        if prev_calls >= key.calls_per_minute:
            log.info("Over throttle limit for key %s.", key.id)
            response.status = '401 Unauthorized'
            return "008 Access denied. You have exceeded the maximum allowed calls. Please try again later"

    # If we get here it means we have passed all throttling tests...
    log.info("Throttling passed!")
    
    print "calling %s with args %s, %s" % (f.__name__, args, kwds)
    return f(*args, **kwds)
Ejemplo n.º 5
0
    def example(self):

        text = request.params.get('text')
        apikey = request.params.get('key')
        language = request.params.get('language')
        channel = request.params.get('channel')
        referrer = request.headers.get('REFERER', '/')
        host = get_host(referrer)
        ip_address = request.environ.get("X_FORWARDED_FOR",
                                         request.environ.get("HTTP_X_FORWARDED_FOR",
                                                             request.environ.get("REMOTE_ADDR")))
        tags = request.params.get('tags')
        corpus = request.params.get('corpus')

        log.info('apikey=%s referrer=%s host=%s', apikey, referrer, host)


        # The text parameter is required for the example method
        if not text:
            log.info('Missing text parameter.')
            return "001 Missing Parameter: Required parameter is not supplied (text)."

        # The tags parameter is required for the example method
        if not tags:
            log.info('Missing tags parameter.')
            return "001 Missing Parameter: Required parameter is not supplied (tags)."

        # The corpus parameter is required for the example method
        if not tags:
            log.info('Missing corpus parameter.')
            return "001 Missing Parameter: Required parameter is not supplied (corpus)."

        # Now update the call count on the key row...
        key = session.get('key')
        if key:
            key.calls = key.calls + 1
            key.last_call = datetime.datetime.now()

        # Log the api call...
        apicall = APICall()
        apicall.parameters = text
        apicall.result = simplejson.dumps(tags)
        if key:
            apicall.apikey_id = key.id
        apicall.method = 'example'
        apicall.http_method = request.method
        apicall.called_from = ip_address
        Session.add(apicall)
        Session.commit()

        # Save the example to the database...
        example = Example()
        example.text = text
        example.tags = tags
        example.corpus = corpus
        example.apicall_id = apicall.id
        Session.add(example)
        Session.commit()
        
        tags = tags.split()

        response.headers['Content-Type'] = 'application/json'
        return simplejson.dumps(dict(text=text,tags=tags,corpus=corpus))
Ejemplo n.º 6
0
                      default=60)
    (options, args) = parser.parse_args()

    if not options.owner_name:
        parser.error("You must supply the owner name. (--owner_name).")

    if not options.value:
        random.shuffle(KEYCHARS)
        options.value = ''.join(KEYCHARS[0:options.keylength])

    conf = appconfig('config:' + options.ini, relative_to='.')
    load_environment(conf.global_conf, conf.local_conf)

    engine = create_engine(conf['sqlalchemy.url'], echo=True)
    meta = MetaData()
    conn = engine.connect()

    apikey = APIKey()
    apikey.owner_name = options.owner_name
    apikey.keystr = options.value
    apikey.owner_url = options.owner_url
    apikey.valid_domains = options.valid_domains
    apikey.calls_per_minute = options.calls_per_minute

    Session.add(apikey)
    Session.commit()

    print "Thanks for applying to be an Alpha Tester for Swift Web Services."
    print "Here is your API key for OpenSiLCC: %s" % options.value

Ejemplo n.º 7
0
    def tag(self):

        text = request.params.get('text')
        apikey = request.params.get('key')
        language = request.params.get('language')
        channel = request.params.get('channel')
        referrer = request.headers.get('REFERER', '/')
        host = get_host(referrer)
        log.info('apikey=%s referrer=%s host=%s', apikey, referrer, host)

        if not apikey:
            # From Swift River API docs:
            # 007 Access denied. Your API key is no longer valid.  Please contact the administrator.
            # 008 Access denied. You need an API key to perform that task.  Please contact the administrator.
            response.status = '401 Unauthorized'
            return "008 Access denied. You need an API key to perform that task. Please contact the administrator."
        
        # Now load the key from the db if it exists...
        key = Session.query(APIKey).filter_by(keystr=apikey).first()
        if not key:
            log.info('No matching key was found in the db.')
            response.status = '401 Unauthorized'
            return "008 Access denied. You need an API key to perform that task.  Please contact the administrator."

        # Check that the key is valid for the referrer host...
        if key.valid_domains != host and key.valid_domains != '*':
            log.info("A Key was found but the referring host is invalid.")
            response.status = '401 Unauthorized'
            return "008 Access denied. You need an API key to perform that task.  Please contact the administrator."
            
        # Now check the number of calls in the last minute...
        query = select([func.count(APICall.table.c.id)])
        query = query.where("called_at > now() - interval 1 minute") 
        results = Session.execute(query).fetchone()
        log.info('number of previous calls: %s', str(results))

        # The text parameter is required for the tag method
        if not text:
            log.info('Missing text parameter.')
            return "001 Missing Parameter: Required parameter is not supplied (text)."

        log.info('Text to be tagged: %s', text)
        tags = TweetTagger.tag(text)
        log.info('Tags extracted: %s', str(tags))

        # Now update the call count on the key row...
        key.calls = key.calls + 1
        key.last_call = datetime.datetime.now()

        # Log the api call
        apicall = APICall()
        apicall.parameters = text
        apicall.result = simplejson.dumps(tags)
        apicall.apikey_id = key.id
        apicall.method = 'tag'
        apicall.http_method = request.method
        ip_address = request.environ.get("X_FORWARDED_FOR",
                                         request.environ.get("HTTP_X_FORWARDED_FOR",
                                                             request.environ.get("REMOTE_ADDR")))
        apicall.called_from = ip_address
        Session.add(apicall)

        Session.commit()
        response.headers['Content-Type'] = 'application/json'
        return simplejson.dumps(tags)