Commit 808a09a0 authored by okhin's avatar okhin
Browse files

Load calls from IDs

parent 2291f71d
Pipeline #9 skipped
#!/usr/bin/env python
import sqlite3
import datetime
import uuid
import json
from bottle import route, run, request, abort, install, get, post
from bottle_sqlite import SQLitePlugin
......@@ -8,6 +11,60 @@ import jwt
def sanitize_phonenumber(number):
This function is used to sanitize a phone number.
If it starts with a +, it will be removed and replaced by 00.
Any chars who do notbelong to [0-9] will be stripped.
If the number doesn't starts with 00, a TypeError will be raised
if number[0] == '+':
number = '00' + number[1:]
number = ''.join([c for c in number if c in '0123456789'])
if not number.startswith('00'):
raise TypeError('{} is not a valid international number, it should start with 00')
return number
class Call(json.JSONEncoder):
This Class is used to manage operatiosn on a call, to print it and dump it.
history = {}
def __init__(self, caller, callee, owner):
self.caller = caller
self.callee = callee
self.owner = owner = uuid.uuid4()
except Exception as e:
raise e
def url(self):
return ''.join(['/calls/',])
def default(self, o):
We need to implement this to be able to JSONEncode
return { 'caller': self.caller
, 'callee': self.callee
, 'callid': self.callee
, 'url': self.url()
, 'history': self.history
, 'owner': self.owner }
def load(cls, callid, db):
results = db.execute('SELECT caller, callee, owner, callid, history, state FROM calls WHERE callid = ?;', (callid,))
result = results.fetchone()
object = cls(result[0], result[1], result[2]) = result[3]
object.history = json.loads(result[4])
return object
return None
# We need a decorator to check if our query is authenticated.
# We will store an API key and SECRET in ur database, the client
# needs to have both of them.
......@@ -18,10 +75,10 @@ def authenticated(f):
def wrapped(db, *args, **kwargs):
# Let's get the JWT token. It should be a params (from get or post or whatev')
if 'token' not in request.params:
abort(403, "No token found in the query")
abort(401, "No token found in the query")
# We want the api id in the params to.
if 'api' not in request.params:
abort(403, "No api id found in the params")
abort(401, "No api id found in the params")
# Now, let's get the token on our side
results = db.execute('SELECT token FROM users WHERE api = ?', (request.params['api'],)).fetchall()
......@@ -29,6 +86,8 @@ def authenticated(f):
token = results[0][0]
auth_token = jwt.decode(request.params['token'], token)
assert auth_token['api'] == request.params['api']
for key in auth_token:
request.params[key] = auth_token[key]
except (jwt.exceptions.InvalidTokenError, AssertionError) as e:
abort(403, e)
except Exception as e:
......@@ -36,9 +95,18 @@ def authenticated(f):
return f(db, *args, **kwargs)
return wrapped
def call(db):
return 'Ohai'
def calls(db):
Return the list of calls associated to the connected user.
The call has a status, caller, callee and history (status change+timestamp)
results = db.execute('SELECT callid FROM calls WHERE owner = ?;', (request.params['api'],))
calls = []
for call in results.fetchall():
calls.append(Calls.load(call[0], db))
head = {'call': '/calls/', 'user': request.params['api'], 'hits': len(calls)}
return {'head': head, 'data': calls}
run(host='localhost', port=8080, debug=True)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment