Giter VIP home page Giter VIP logo

analytics-agent-python's Introduction

Mashape Analytics Python Agent

for more information on Mashape Analytics, please visit apianalytics.com

Installation

pip install mashape-analytics

If you're using Mac OS X. You may need virtualenv. This is especially the case for El Capitan.

Usage

Django

Add the following middleware and setting in settings.py:

MIDDLEWARE_CLASSES = (
  'mashapeanalytics.middleware.DjangoMiddleware',
)

# Mashape Analytics
MASHAPE_ANALYTICS_SERVICE_TOKEN = 'SERVICE_TOKEN' # Replace with your App Service Token
MASHAPE_ANALYTICS_ENVIRONMENT = 'production' # Leave blank for default, or replace with your Environment ID

Note: See Django Configurations for a full list of configuration properties.

Flask

Add the middleware to wsgi_app:

from mashapeanalytics.middleware import FlaskMiddleware as MashapeAnalytics
from flask import Flask

app = Flask(__name__)
app.wsgi_app = MashapeAnalytics(app.wsgi_app, 'SERVICE_TOKEN', 'production') # Attach middleware with environment, `production`

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

Note: See WSGI Configurations for a full list of configuration properties.

Pyramid

This example starts from Pyramid's simplest application.

Wrap the middleware to app:

from mashapeanalytics.middleware import WsgiMiddleware as MashapeAnalytics
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response


def hello_world(request):
  return Response('Hello %(name)s!' % request.matchdict)

if __name__ == '__main__':
  config = Configurator()
  config.add_route('hello', '/hello/{name}')
  config.add_view(hello_world, route_name='hello')
  app = config.make_wsgi_app()

  app = MashapeAnalytics(app, 'SERVICE_TOKEN', 'production') # Attach middleware with environment, `production`

  server = make_server('0.0.0.0', 8080, app)
  server.serve_forever()

Note: See WSGI Configurations for a full list of configuration properties.

Pylons

Add the middleware to your ini file.

[filter:mashape_analytics]
use = egg:mashape_analytics#mashape_analytics
serviceToken = SERVICE_TOKEN
environment = production

[pipeline]
pipeline = mashape_analytics my_application

Note: See WSGI Configurations for a full list of configuration properties.

Copyright and license

Copyright Mashape Inc, 2015.

Licensed under the MIT License

analytics-agent-python's People

Contributors

kennethklee avatar orliesaurus avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

analytics-agent-python's Issues

RawPostDataException - cannot access request body

Please see note in https://docs.djangoproject.com/en/dev/topics/http/middleware/#process-view

This particular user is using django-rest-framework.

The error
RawPostDataException: You cannot access body after reading from request's data stream due to accessing request.body in your agent.

The location

/home/api/.virtualenvs/api/local/lib/python2.7/site-packages/mashapeanalytics/middleware/django_middleware.py in process_response 
def process_response(self, request, response): 
startedDateTime = request.META.get('MASHAPE_ANALYTICS.STARTED_DATETIME', datetime.utcnow()) 
requestHeaders = [{'name': re.sub('^HTTP_', '', header), 'value': value} for (header, value) in request.META.items() if header.startswith('HTTP_')] 
requestHeaderSize = self.request_header_size(request) 
requestQueryString = [{'name': name, 'value': (value[0] if len(value) > 0 else None)} for name, value in parse_qs(request.META.get('QUERY_STRING', '')).items()] 
requestContentSize = len(request.body) ... 
responseHeaders = [{'name': header, 'value': value[-1]} for (header, value) in response._headers.items()] 
responseHeadersSize = self.response_header_size(response) 
responseContentSize = len(response.content) 
alf = Alf(self.serviceToken, self.environment, self.client_address(request))

Some routes seem to skip wsgi layer

This specifically happens with Flask-Restful.

app = Flask(__name__)
app.wsgi_app = MashapeAnalytics(app.wsgi_app, 'SERVICE_TOKEN', 'production')
api = Api(app)

setup.py requires specific versions of dependencies

Installing mashape-analytics alongside other packages can be problematic if those other packages have some common dependencies but with different version requirements.

Instead of requiring specific versions like this:

install_requires=[ 'pytz==2016.4', 'requests==2.9.1', 'six==1.10.0', 'ujson==1.35', 'Werkzeug==0.11.9', ],

...can dependencies by specified as minimum versions wherever possible?

ImportError for six

From an Mac OS X, El Capitan.

Need to reference objects like such:

from six.moves import http_cookies 
SimpleCookie = http_cookies.SimpleCookie

Also similar for:

File "/Library/Python/2.7/site-packages/mashapeanalytics/alf.py", line 4, in <module> 
@six.python_2_unicode_compatible 
AttributeError: 'module' object has no attribute 'python_2_unicode_compatible'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.