Skip to content

Instantly share code, notes, and snippets.

@nishadhka
Last active August 23, 2018 04:49
Show Gist options
  • Save nishadhka/46a9a1964e156f84efb927354caca07e to your computer and use it in GitHub Desktop.
Save nishadhka/46a9a1964e156f84efb927354caca07e to your computer and use it in GitHub Desktop.
usefull_tools/Ushahidi_API_use_cases.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2018-08-22T23:15:00.587910Z",
"end_time": "2018-08-22T23:15:00.598083Z"
}
},
"cell_type": "markdown",
"source": "# Ushahidi(USH) [API](http://github.ushahidi.org/platform/docs/api/index.html#users-&-roles-users-get) use cases\n## Access to API \n1. Ushahidi gives API access with a valid account in the instance, the rights are based on account type "
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "# Following 4 values need to be replaced for new instancehttps://keralarescuemap.api.ushahidi.io/api/v3/forms/2 based on @batpat\nimport requests\n\nUSHAHIDI_URL = \"https://keralarescuemap.api.ushahidi.io/oauth/token\"\nUSHAHIDI_USERNAME = \"your email address\"\nUSHAHIDI_PASSWORD = \"your password\"\nUSHAHIDI_SECRET = \"35e7f0bca957836d05ca0492211b0ac707671261\"\n\ndef get_access_token():\n oauth_url = USHAHIDI_URL\n oauth_d = {\n \"grant_type\": \"password\",\n \"scope\": \"apikeys posts country_codes media forms api tags savedsearches sets users stats layers config messages notifications webhooks contacts roles permissions csv tos dataproviders\",\n \"client_id\": \"ushahidiui\",\n \"client_secret\": USHAHIDI_SECRET,\n \"password\": USHAHIDI_PASSWORD, \n \"username\": USHAHIDI_USERNAME \n }\n\n oauth_creds = requests.post(oauth_url, json=oauth_d).text\n print(oauth_creds)\n access_token = json.loads(oauth_creds)['access_token']\n\n return access_token \n\n\naccess_token = get_access_token()\n\nheaders = {\n 'Authorization': 'Bearer %s' % access_token\n}",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"collapsed": true,
"trusted": true,
"ExecuteTime": {
"start_time": "2018-08-22T23:36:32.611607Z",
"end_time": "2018-08-22T23:36:32.620286Z"
}
},
"cell_type": "markdown",
"source": "## On Forms\n1. Forms are the basic input tool for USH, it is where the crowd(user) enter the data\n\n### to list the forms"
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "post_url = 'https://keralarescuemap.api.ushahidi.io/api/v3/forms'\nout = requests.get(post_url, headers=headers)\n#data = json.loads(out.text)\nprint(out.text)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2018-08-23T00:00:36.138444Z",
"end_time": "2018-08-23T00:00:36.147005Z"
}
},
"cell_type": "markdown",
"source": "### to list the attributes in all the forms or particular forms\n1. USH makes the each questions in form to a key like attributes\n1. This attributes is essential for createing a CLI to ushahidi for a particular form or to make a POST to ushahidi\n1. To have a view on the question and its respective key, this script may be [useful] (https://gist.github.com/nishadhka/b1d5bd4ecd1c73ea52be4ab2521f62d4)"
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "#for all the attributes\npost_url = 'http://sample_ushahidinstance/platform/api/v3/forms/attributes/'\n#for particular form's attributes\npost_url = 'http://sample_ushahidinstance/platform/api/v3/forms/attributes/form_id'\nout = requests.get(post_url, headers=headers)\ndata = json.loads(out.text)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "## to list users",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2018-08-23T00:09:05.407213Z",
"end_time": "2018-08-23T00:09:05.468514Z"
},
"trusted": true
},
"cell_type": "code",
"source": "post_url = 'https://keralarescuemap.api.ushahidi.io/api/v3/users'\nout = requests.get(post_url, headers=headers)\n#data = json.loads(out.text)\nprint(out.text)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "## On data searches",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "## On data export",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python2",
"display_name": "Python 2",
"language": "python"
},
"toc": {
"threshold": 4,
"number_sections": true,
"toc_cell": false,
"toc_window_display": true,
"toc_section_display": "block",
"sideBar": true,
"navigate_menu": true,
"moveMenuLeft": true,
"widenNotebook": false,
"colors": {
"hover_highlight": "#DAA520",
"selected_highlight": "#FFD700",
"running_highlight": "#FF0000",
"wrapper_background": "#FFFFFF",
"sidebar_border": "#EEEEEE",
"navigate_text": "#333333",
"navigate_num": "#000000"
},
"nav_menu": {
"width": "252px",
"height": "49px"
}
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"name": "python",
"pygments_lexer": "ipython2",
"version": "2.7.14",
"file_extension": ".py",
"codemirror_mode": {
"version": 2,
"name": "ipython"
}
},
"gist": {
"id": "46a9a1964e156f84efb927354caca07e",
"data": {
"description": "usefull_tools/Ushahidi_API_use_cases.ipynb",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/46a9a1964e156f84efb927354caca07e"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment