Jexia and Python
Get to know all the basic pieces of code to get the advantage of Jexia through Python.
Step One
Create API key in the interface https://app.jexia.com/project/<your_project>/keys
Step Two
Create two simple datasets https://app.jexia.com/project/<your_project>/datasets
Dataset comments:
And dataset posts:
Step Three
Create a Policy for the user of application https://app.jexia.com/project/<your_project>/policies
Step Four
Let’s play with API!
To use Jexia’s API you will need to install only one python package - requests. After that you need to define authentication variables:
# Variables
KEY = '<you_key>'
SECRET = '<you_secret>'
PROJECT = '<your_project>'
And download the required modules:
# Modules
import requests
import random
import pprint
pp = pprint.PrettyPrinter(indent=4)
Code snippets what you will be able to do after
Get a token as an application:
token = requests.post(url='https://%s.app.jexia.com/auth' % PROJECT, json={'method': 'apk', 'key': KEY, 'secret': SECRET}).json()['access_token']
APP_TOKEN = {"Authorization": "Bearer %s" % token}
Signup new user:
requests.post(url='https://%s.app.jexia.com/ums/signup' % PROJECT, json={'email': 'user1@example.com', 'password': 'test'}, headers=APP_TOKEN).json()
{'id': '5bf48bc3-0606-4a71-9543-916c3b0fe10e', 'email': 'user1@example.com', 'active': True, 'created_at': '2019-08-27T14:36:56.791808Z', 'updated_at': '2019-08-27T14:36:56.791808Z'}
Get token as user:
token = requests.post(url='https://%s.app.jexia.com/auth' % PROJECT, json={'method': 'ums', 'email': 'user1@example.com', 'password': 'test'}).json()['access_token']
USER_TOKEN = {"Authorization": "Bearer %s" % token}
Fetch the current user:
requests.get(url='https://%s.app.jexia.com/ums/user' % PROJECT, headers=USER_TOKEN).json()
{'id': '5bf48bc3-0606-4a71-9543-916c3b0fe10e', 'email': 'user1@example.com', 'active': True, 'created_at': '2019-08-27T14:36:56.791808Z', 'updated_at': '2019-08-27T14:36:56.791808Z'}
Change password for current user:
requests.post(url='https://%s.app.jexia.com/ums/changepassword' % PROJECT, json={'old_password': 'test', 'new_password': 'test'}, headers=USER_TOKEN).json()
{'id': '5bf48bc3-0606-4a71-9543-916c3b0fe10e', 'email': 'user1@example.com', 'active': True, 'created_at': '2019-08-27T14:36:56.791808Z', 'updated_at': '2019-08-27T14:36:56.791808Z'}
Delete the current user:
requests.delete(url='https://%s.app.jexia.com/ums/user' % PROJECT, json={'password': 'test'}, headers=USER_TOKEN).json()
{'id': '5bf48bc3-0606-4a71-9543-916c3b0fe10e', 'email': 'user1@example.com', 'active': True, 'created_at': '2019-08-27T14:36:56.791808Z', 'updated_at': '2019-08-27T14:36:56.791808Z'}
Insert 5 posts to dataset posts :
for i in range(5):
... requests.post(url='https://%s.app.jexia.com/ds/posts' % PROJECT, json={'text': 'Post number %s' % i, 'author': 'John%s' % i}, headers=USER_TOKEN).json()
...
[{'id': '594f74be-5f7e-4269-9bc6-d6df72a62edf', 'created_at': '2019-08-28T11:32:53.154305Z', 'updated_at': '2019-08-28T11:32:53.154305Z', 'text': 'Post number 0', 'author': 'John0'}]
[{'id': '4ce5b356-3094-427a-a701-a173e55b78fa', 'created_at': '2019-08-28T11:32:53.351919Z', 'updated_at': '2019-08-28T11:32:53.351919Z', 'text': 'Post number 1', 'author': 'John1'}]
[{'id': '0c8b9918-2aa7-4a5d-ab8d-761ed8f53e9c', 'created_at': '2019-08-28T11:32:53.549922Z', 'updated_at': '2019-08-28T11:32:53.549922Z', 'text': 'Post number 2', 'author': 'John2'}]
[{'id': 'ec68c1c4-4fda-4427-9863-836e207361f9', 'created_at': '2019-08-28T11:32:53.734192Z', 'updated_at': '2019-08-28T11:32:53.734192Z', 'text': 'Post number 3', 'author': 'John3'}]
[{'id': '0b394b0b-03c4-4d42-ba5a-fcbc48e65ddc', 'created_at': '2019-08-28T11:32:53.914495Z', 'updated_at': '2019-08-28T11:32:53.914495Z', 'text': 'Post number 4', 'author': 'John4'}]
Insert 10 comments to dataset comments :
for i in range(10):
... requests.post(url='https://%s.app.jexia.com/ds/comments' % PROJECT, json={'text': 'text number %s' % i, 'likes': i, 'dislikes': i}, headers=USER_TOKEN).json()
...
[{'id': '996cb836-a572-475f-b22f-56eefe671944', 'created_at': '2019-08-27T16:16:48.691483Z', 'updated_at': '2019-08-27T16:16:48.691483Z', 'text': 'text number 0', 'likes': 0, 'dislikes': 0}]
[{'id': '7d0ac5bf-d156-413b-9ae1-41de9fdbf459', 'created_at': '2019-08-27T16:16:48.882627Z', 'updated_at': '2019-08-27T16:16:48.882627Z', 'text': 'text number 1', 'likes': 1, 'dislikes': 1}]
[{'id': '7c155b22-7204-4135-b580-fe03d4ad6ad9', 'created_at': '2019-08-27T16:16:49.070513Z', 'updated_at': '2019-08-27T16:16:49.070513Z', 'text': 'text number 2', 'likes': 2, 'dislikes': 2}]
[{'id': 'f100659e-b104-4e2b-9285-5869647273af', 'created_at': '2019-08-27T16:16:49.265496Z', 'updated_at': '2019-08-27T16:16:49.265496Z', 'text': 'text number 3', 'likes': 3, 'dislikes': 3}]
[{'id': 'b90a3040-b6cc-4681-a76a-f76c45646dbf', 'created_at': '2019-08-27T16:16:49.455575Z', 'updated_at': '2019-08-27T16:16:49.455575Z', 'text': 'text number 4', 'likes': 4, 'dislikes': 4}]
[{'id': '27885dd8-b656-47c0-bd0c-e9f15b31f5ab', 'created_at': '2019-08-27T16:16:49.649834Z', 'updated_at': '2019-08-27T16:16:49.649834Z', 'text': 'text number 5', 'likes': 5, 'dislikes': 5}]
[{'id': 'dc172552-be95-40e0-8428-e7a235cb0443', 'created_at': '2019-08-27T16:16:49.831176Z', 'updated_at': '2019-08-27T16:16:49.831176Z', 'text': 'text number 6', 'likes': 6, 'dislikes': 6}]
[{'id': '469a39aa-f5f1-42be-b77f-e36540572fd9', 'created_at': '2019-08-27T16:16:50.006983Z', 'updated_at': '2019-08-27T16:16:50.006983Z', 'text': 'text number 7', 'likes': 7, 'dislikes': 7}]
[{'id': 'b8a16111-3d45-461a-aa01-190358fefb64', 'created_at': '2019-08-27T16:16:50.192772Z', 'updated_at': '2019-08-27T16:16:50.192772Z', 'text': 'text number 8', 'likes': 8, 'dislikes': 8}]
[{'id': 'f4bb59bc-bf2f-49f1-81e9-38e7336792a7', 'created_at': '2019-08-27T16:16:50.388515Z', 'updated_at': '2019-08-27T16:16:50.388515Z', 'text': 'text number 9', 'likes': 9, 'dislikes': 9}]
Get data from dataset posts, and sort results ascending by field author :
pp.pprint(requests.get(url='https://%s.app.jexia.com/ds/posts?outputs=["text", "author"]&order={"direction":"asc","fields":["author"]}' % PROJECT, headers=USER_TOKEN).json())
[ { 'author': 'John0',
'id': '594f74be-5f7e-4269-9bc6-d6df72a62edf',
'text': 'Post number 0'},
{ 'author': 'John1',
'id': '4ce5b356-3094-427a-a701-a173e55b78fa',
'text': 'Post number 1'},
{ 'author': 'John2',
'id': '0c8b9918-2aa7-4a5d-ab8d-761ed8f53e9c',
'text': 'Post number 2'},
{ 'author': 'John3',
'id': 'ec68c1c4-4fda-4427-9863-836e207361f9',
'text': 'Post number 3'},
{ 'author': 'John4',
'id': '0b394b0b-03c4-4d42-ba5a-fcbc48e65ddc',
'text': 'Post number 4'}]
Get data from dataset comments, where field likes has value > 3 and has no value 9 or value 10, and sort the results descending by filed dislikes :
pp.pprint(requests.get(url='https://%s.app.jexia.com/ds/comments?outputs=["text", "likes", "dislikes"]&cond=[{"field":"likes"}, ">", 3, "and", {"field":"likes"}, "not in", [9, 10]]&order={"direction":"desc","fields":["dislikes"]}' % PROJECT, headers=USER_TOKEN).json())
[ { 'dislikes': 8,
'id': 'b8a16111-3d45-461a-aa01-190358fefb64',
'likes': 8,
'text': 'text number 8'},
{ 'dislikes': 7,
'id': '469a39aa-f5f1-42be-b77f-e36540572fd9',
'likes': 7,
'text': 'text number 7'},
{ 'dislikes': 6,
'id': 'dc172552-be95-40e0-8428-e7a235cb0443',
'likes': 6,
'text': 'text number 6'},
{ 'dislikes': 5,
'id': '27885dd8-b656-47c0-bd0c-e9f15b31f5ab',
'likes': 5,
'text': 'text number 5'},
{ 'dislikes': 4,
'id': 'b90a3040-b6cc-4681-a76a-f76c45646dbf',
'likes': 4,
'text': 'text number 4'}]
Get the data filter with pagination. At first 2nd page, then 3rd page:
pp.pprint(requests.get(url='https://%s.app.jexia.com/ds/comments?outputs=["text", "likes", "dislikes"]&cond=[{"field":"likes"}, "!=", 10]&range={"limit": 2, "offset": 2}' % PROJECT, headers=USER_TOKEN).json())
[ { 'dislikes': 1,
'id': '7d0ac5bf-d156-413b-9ae1-41de9fdbf459',
'likes': 1,
'text': 'text number 1'},
{ 'dislikes': 2,
'id': '7c155b22-7204-4135-b580-fe03d4ad6ad9',
'likes': 2,
'text': 'text number 2'}]
pp.pprint(requests.get(url='https://%s.app.jexia.com/ds/comments?outputs=["text", "likes", "dislikes"]&cond=[{"field":"likes"}, "!=", 10]&range={"limit": 2, "offset": 3}' % PROJECT, headers=USER_TOKEN).json())
[ { 'dislikes': 2,
'id': '7c155b22-7204-4135-b580-fe03d4ad6ad9',
'likes': 2,
'text': 'text number 2'},
{ 'dislikes': 4,
'id': 'b90a3040-b6cc-4681-a76a-f76c45646dbf',
'likes': 4,
'text': 'text number 4'}]
Get the post by regex query:
requests.get(url='https://%s.app.jexia.com/ds/posts?outputs=["text", "author"]&cond=[{"field":"text"},"regex",".*[[:space:]]num[a-z]{3}[[:space:]]3"]' % PROJECT, headers=USER_TOKEN).text
'[{"text":"Post number 3","author":"John3","id":"ec68c1c4-4fda-4427-9863-836e207361f9"}]\n'
Update one field (text ) and reset all other fields to default values:
requests.put(url='https://%s.app.jexia.com/ds/comments?outputs=["text", "likes", "dislikes"]&cond=[{"field":"id"}, "=", "f100659e-b104-4e2b-9285-5869647273af"]' % PROJECT, json={'text': 'this is the best commit ever'}, headers=USER_TOKEN).json()
[{'text': 'this is the best commit ever', 'likes': None, 'dislikes': None, 'id': 'f100659e-b104-4e2b-9285-5869647273af'}]
Update just only one field, without touching others:
requests.patch(url='https://%s.app.jexia.com/ds/comments?outputs=["text", "likes", "dislikes"]&cond=[{"field":"id"}, "=", "f100659e-b104-4e2b-9285-5869647273af"]' % PROJECT, json={'likes': 3}, headers=USER_TOKEN).json()
[{'text': 'this is the best commit ever', 'likes': 3, 'dislikes': None, 'id': 'f100659e-b104-4e2b-9285-5869647273af'}]
Attach comments which have < 4 likes to the post with id 594f74be-5f7e-4269-9bc6-d6df72a62edf :
requests.put(url='https://%s.app.jexia.com/ds/posts?outputs=["text", "author", "comments"]&action=attach&cond=[{"field":"id"},"=","594f74be-5f7e-4269-9bc6-d6df72a62edf"]&action_resource=comments&action_cond=[{"field":"likes"},"<",4]' % PROJECT, headers=USER_TOKEN).text
''
Get the post data with all the attached comments:
pp.pprint(requests.get(url='https://%s.app.jexia.com/ds/posts?outputs=["text", "author", "comments"]&cond=[{"field":"id"},"=","594f74be-5f7e-4269-9bc6-d6df72a62edf"]' % PROJECT, headers=USER_TOKEN).json())
[ { 'author': 'John0',
'comments': [ { 'created_at': '2019-08-27T16:16:49.265496Z',
'dislikes': None,
'id': 'f100659e-b104-4e2b-9285-5869647273af',
'likes': 3,
'text': 'this is the best commit ever',
'updated_at': '2019-08-28T10:21:14.642732Z'},
{ 'created_at': '2019-08-27T16:16:48.691483Z',
'dislikes': 0,
'id': '996cb836-a572-475f-b22f-56eefe671944',
'likes': 0,
'text': 'text number 0',
'updated_at': '2019-08-27T16:16:48.691483Z'},
{ 'created_at': '2019-08-27T16:16:48.882627Z',
'dislikes': 1,
'id': '7d0ac5bf-d156-413b-9ae1-41de9fdbf459',
'likes': 1,
'text': 'text number 1',
'updated_at': '2019-08-27T16:16:48.882627Z'},
{ 'created_at': '2019-08-27T16:16:49.070513Z',
'dislikes': 2,
'id': '7c155b22-7204-4135-b580-fe03d4ad6ad9',
'likes': 2,
'text': 'text number 2',
'updated_at': '2019-08-27T16:16:49.070513Z'}],
'id': '594f74be-5f7e-4269-9bc6-d6df72a62edf',
'text': 'Post number 0'}]
Signup the second user:
requests.post(url='https://%s.app.jexia.com/ums/signup' % PROJECT, json={'email': 'user2@example.com', 'password': 'test'}, headers=APP_TOKEN).json()
{'id': '6ccbd49a-7c70-43bc-861b-c0fb0dd0ef2f', 'email': 'user2@example.com', 'active': True, 'created_at': '2019-08-28T12:18:52.885495Z', 'updated_at': '2019-08-28T12:18:52.885495Z'}
Get the token for second user:
token = requests.post(url='https://%s.app.jexia.com/auth' % PROJECT, json={'method': 'ums', 'email': 'user2@example.com', 'password': 'test'}).json()['access_token']
USER_TOKEN = {"Authorization": "Bearer %s" % token}
Get the posts with an access token for the second user:
pp.pprint(requests.get(url='https://%s.app.jexia.com/ds/posts?outputs=["text", "author"]&order={"direction":"asc","fields":["author"]}' % PROJECT, headers=USER_2_TOKEN).json())
[ { 'author': 'John0',
'id': '594f74be-5f7e-4269-9bc6-d6df72a62edf',
'text': 'Post number 0'},
{ 'author': 'John1',
'id': '4ce5b356-3094-427a-a701-a173e55b78fa',
'text': 'Post number 1'},
{ 'author': 'John2',
'id': '0c8b9918-2aa7-4a5d-ab8d-761ed8f53e9c',
'text': 'Post number 2'},
{ 'author': 'John3',
'id': 'ec68c1c4-4fda-4427-9863-836e207361f9',
'text': 'Post number 3'},
{ 'author': 'John4',
'id': '0b394b0b-03c4-4d42-ba5a-fcbc48e65ddc',
'text': 'Post number 4'}]