This tutorial shows how a Custom Connectors is written and registered with Squirro. The tutorial uses the Python WsgiService framework to build the necessary REST web service.
Code Block |
---|
language | py |
---|
linenumbers | true |
---|
|
from pprint import pprint
import datetime
import json
import logging
import requests
import threading
import time
import uuid
import wsgiservice
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger(__name__)
sources = {}
@wsgiservice.mount('/config')
class ConfigResource(wsgiservice.Resource):
def POST(self, config, callback):
source_id = str(uuid.uuid4())
title = u'Source {0}'.format(source_id)
sources[source_id] = {
'title': title,
'config': config,
'callback': callback,
}
return {
'source': '/sources/{0}'.format(source_id),
'title': title,
}
@wsgiservice.mount('/sources/{source_id}')
def DELETE(self, source_id):
log.debug('Deleting source: %r', source_id)
if source_id in sources:
del sources[source_id]
wsgiservice.raise_204(self)
def push_items():
while True:
now = datetime.datetime.utcnow().isoformat()
for source_id, source in sources.iteritems():
callback = source['callback']
item = {
'title': 'Random item at {0}'.format(now),
'link': 'http://example.com/{0}'.format(str(uuid.uuid4())),
}
res = requests.post(callback, json.dumps([item]), headers={'Content-Type': 'application/json'})
res.raise_for_status()
time.sleep(5)
app = wsgiservice.get_app(globals())
if __name__ == '__main__':
from wsgiref.simple_server import make_server
print "Running on port 8010"
thread = threading.Thread(target=push_items)
thread.daemon = True
thread.start()
make_server('', 8010, app).serve_forever() |
Create subscription
Code Block |
---|
from squirro_client import SquirroClient
c = SquirroClient(None, None, cluster='http://localhost')
c.authenticate(refresh_token='77dc4d54af7df265d7cdb60cc4404367c7b54f7594668f623d17de0e5a40a0f270cbfe8d2e4c5fa6898d38a8878ad0220ae3302b4976a0bfc204bd1959c69d51')
print c.new_subscription('jDphQJh1SLyVolAvhjrNFg', 'default', 'dummy', config={'count': '4'}) |
Registration
Code Block |
---|
language | text |
---|
title | /etc/squirro/topic.ini |
---|
|
[provider_dummy]
endpoint = http://localhost:8010/config
endpoint_validate = http://localhost:8010/validate
supports_preview = True
link =
title = |
Todo
...
page can now be found at Writing a Custom Data Loader Plugin and How To Write a Custom 1-Click Connector on the Squirro Docs site.