0

I have a simple Flask Rest-Plus app that is serving as the micro-service front end for a Keen.io implementation. I've set up a custom virtual environment for the app and installed keen via pip install keen. I can save events via the python console within that environment; however, when I try to do the same from a Flask endpoint, it throws the following error message:

[2017-08-03 12:31:02,784] ERROR in app: Exception on/api/analytics/track/pageview [POST]
Traceback (most recent call last):
  File "/home/garrett/env/event-api/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/garrett/env/event-api/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/garrett/env/event-api/local/lib/python2.7/site-packages/flask_restplus/api.py", line 313, in wrapper
    resp = resource(*args, **kwargs)
  File "/home/garrett/env/event-api/local/lib/python2.7/site-packages/flask/views.py", line 84, in view
    return self.dispatch_request(*args, **kwargs)
  File "/home/garrett/env/event-api/local/lib/python2.7/site-packages/flask_restplus/resource.py", line 44, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/home/garrett/dev/swiftshopper/event-api/api/analytics/endpoints/track.py", line 29, in post
    track_event("pageview", payload)
  File "/home/garrett/dev/swiftshopper/event-api/api/analytics/utils/keen.py", line 10, in track_event
    keen.add_event("signups", {
AttributeError: 'module' object has no attribute 'add_event'

The code for calling it:

import keen

# This is your actual Project ID and Write Key
keen.project_id = "..."
keen.write_key = "..."

# Wrapper around event API
def track_event(event_name, payload):
    #keen.add_event(event_name, payload)
    keen.add_event("signups", {
      "username": "lloyd",
      "referred_by": "harry"
    })

Appreciate support / ideas!

Ivan Kolesnikov
  • 1,787
  • 1
  • 29
  • 45

2 Answers2

1

Couldn't get it to work, but it's pretty easy to just re-create the endpoint using requests and the web api:

import requests
import json

# This is your actual Project ID and Write Key
PROJECT_ID = "..."
MASTER_KEY = "..."

headers = {
    "Content-Type": "application/json",
    "Authorization": MASTER_KEY
}

# Event endpoint
KEEN_URI = "https://api.keen.io/3.0/projects/{:s}/events/".format(PROJECT_ID)

# Wrapper around event API
def track_event(event_name, payload):
    # Build URI
    URI = KEEN_URI + event_name

    # Make request
    r = requests.post(URI, headers=headers, data=json.dumps(payload))
    response = r.json()

    print(response)
0

I don't see anything wrong with your code. I've used Keen in multiple flask projects too. What I am wondering about is the stuff I can't see.

Since it is an error with the 'module' object, this might help: AttributeError: 'module' object has no attribute

I'm also wondering if something is going wrong with the payload.

tbarn
  • 348
  • 1
  • 9
  • I'm also happy to share some code of keen working in a Flask project if you would like. I don't have a simple example in front of me now, but I can later. – tbarn Aug 03 '17 at 19:13