def test_datasets_list(): """Listing datasets works""" body = ''' [ { "owner": "testuser", "id": "ds1", "created": "2015-09-19", "modified": "2015-09-19" }, { "owner": "testuser", "id": "ds2", "created": "2015-09-19", "modified": "2015-09-19" } ] ''' responses.add( responses.GET, 'https://api.mapbox.com/datasets/v1/{0}?access_token={1}'.format( username, access_token), match_querystring=True, body=body, status=200, content_type='application/json') response = Datasets(access_token=access_token).list() assert response.status_code == 200 assert [item['id'] for item in response.json()] == ['ds1', 'ds2']
def test_datasets_create(): """Creating a named and described dataset works.""" def request_callback(request): payload = json.loads(request.body.decode()) resp_body = { 'owner': username, 'id': 'new', 'name': payload['name'], 'description': payload['description'], 'created': '2015-09-19', 'modified': '2015-09-19' } headers = {} return (200, headers, json.dumps(resp_body)) responses.add_callback( responses.POST, 'https://api.mapbox.com/datasets/v1/{0}?access_token={1}'.format( username, access_token), match_querystring=True, callback=request_callback) response = Datasets(access_token=access_token).create( name='things', description='a collection of things') assert response.status_code == 200 assert response.json()['name'] == 'things' assert response.json()['description'] == 'a collection of things'
def test_datasets_create(): """Creating a named and described dataset works.""" def request_callback(request): payload = json.loads(request.body.decode()) resp_body = { 'owner': username, 'id': 'new', 'name': payload['name'], 'description': payload['description'], 'created': '2015-09-19', 'modified': '2015-09-19'} headers = {} return (200, headers, json.dumps(resp_body)) responses.add_callback( responses.POST, 'https://api.mapbox.com/datasets/v1/{0}?access_token={1}'.format( username, access_token), match_querystring=True, callback=request_callback) response = Datasets(access_token=access_token).create( name='things', description='a collection of things') assert response.status_code == 200 assert response.json()['name'] == 'things' assert response.json()['description'] == 'a collection of things'
def test_dataset_list_features(): """Features retrieval work""" responses.add( responses.GET, 'https://api.mapbox.com/datasets/v1/{0}/{1}/features?access_token={2}'. format(username, 'test', access_token), match_querystring=True, body=json.dumps({'type': 'FeatureCollection'}), status=200, content_type='application/json') response = Datasets(access_token=access_token).list_features('test') assert response.status_code == 200 assert response.json()['type'] == 'FeatureCollection'
def test_dataset_list_features(): """Features retrieval work""" responses.add( responses.GET, 'https://api.mapbox.com/datasets/v1/{0}/{1}/features?access_token={2}'.format( username, 'test', access_token), match_querystring=True, body=json.dumps({'type': 'FeatureCollection'}), status=200, content_type='application/json') response = Datasets(access_token=access_token).list_features('test') assert response.status_code == 200 assert response.json()['type'] == 'FeatureCollection'
def test_read_feature(): """Feature read works.""" responses.add( responses.GET, 'https://api.mapbox.com/datasets/v1/{0}/{1}/features/{2}?access_token={3}'.format( username, 'test', '1', access_token), match_querystring=True, body=json.dumps({'type': 'Feature', 'id': '1'}), status=200, content_type='application/json') response = Datasets(access_token=access_token).read_feature('test', '1') assert response.status_code == 200 assert response.json()['type'] == 'Feature' assert response.json()['id'] == '1'
def test_dataset_read(): """Dataset name and description reading works.""" responses.add( responses.GET, 'https://api.mapbox.com/datasets/v1/{0}/{1}?access_token={2}'.format( username, 'test', access_token), match_querystring=True, body=json.dumps( {'name': 'things', 'description': 'a collection of things'}), status=200, content_type='application/json') response = Datasets(access_token=access_token).read_dataset('test') assert response.status_code == 200 assert response.json()['name'] == 'things' assert response.json()['description'] == 'a collection of things'
def test_delete_feature(): """Deletes a feature.""" responses.add( responses.DELETE, 'https://api.mapbox.com/datasets/v1/{0}/{1}/features/{2}?access_token={3}' .format(username, 'test', '1', access_token), match_querystring=True, status=204) response = Datasets(access_token=access_token).delete_feature('test', '1') assert response.status_code == 204
def test_update_feature(): """Feature update works.""" def request_callback(request): payload = json.loads(request.body.decode()) assert payload == {'type': 'Feature'} return (200, {}, "") responses.add_callback( responses.PUT, 'https://api.mapbox.com/datasets/v1/{0}/{1}/features/{2}?access_token={3}' .format(username, 'test', '1', access_token), match_querystring=True, callback=request_callback) response = Datasets(access_token=access_token).update_feature( 'test', '1', {'type': 'Feature'}) assert response.status_code == 200
def test_batch_update_features(): """Features update works""" def request_callback(request): payload = json.loads(request.body) assert payload['put'] == [{'type': 'Feature'}] assert payload['delete'] == ['1'] return (200, {}, "") responses.add_callback( responses.POST, 'https://api.mapbox.com/datasets/v1/{0}/{1}/features?access_token={2}'.format( username, 'test', access_token), match_querystring=True, callback=request_callback) response = Datasets(access_token=access_token).batch_update_features( 'test', put=[{'type': 'Feature'}], delete=['1']) assert response.status_code == 200
def test_datasets_service_properties(): """Get expected username and baseuri.""" datasets = Datasets(access_token=access_token) assert datasets.username == username assert datasets.baseuri == 'https://api.mapbox.com/datasets/v1'
def test_class_attrs(): """Get expected class attr values""" serv = Datasets() assert serv.api_name == 'datasets' assert serv.api_version == 'v1'
# Mapbox API credentials can be created here https://account.mapbox.com/ # The token must provide CRUD (create read update delete) privileges username = '******' access_token = '{insert your own Mapbox API here}' # IEC API credentials ELusername = '******' ELpassword = '******' ELaccess_token = '{insert your own IEC API token here}' # Set the HTTP Basic Auth header for the IEC API headers = {'Authorization': 'bearer ' + ELaccess_token} # Lets see how many datasets we have at Mapbox and ensure our credentials work # https://docs.mapbox.com/api/maps/#datasets datasets = Datasets(access_token=access_token) listing_resp = datasets.list() if listing_resp.status_code != 200: raise ApiError('GET dataset failed {}'.format(resp.status_code)) for dataset in listing_resp.json(): print('Found Mapbox dataset: {}'.format(dataset['name'])) # Now lets create a test feature in Cape Town for each of our datasets # This can be any GeoJson object - http://geojson.org/ #feature = {'type': 'Feature', 'id': '100001', 'properties': {'name': 'Just a Test Feature with fake ID'}, # 'geometry': {'type': 'Point', 'coordinates': [18.4, -33.9]}} #resp = datasets.update_feature(dataset['id'], '100001', feature) #print('Created feature response: {} '.format(str(resp.status_code))) # While testing your environment - we stop the engine here # When ready comment these two lines out to run the actual engine
import base64 import json import os import time from mapbox.services.datasets import Datasets import requests from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) idlayer = 'cin2p1rmm0bqvvhm47cjvcsor' access_token = 'pk.eyJ1Ijoic2Vkb25hY2hhbWJlciIsImEiOiJjaW13Zmp3cGswMzd0d2tsdXBnYmVjNmRjIn0.PlcjviLrxQht-_tBEbQQeg' secretkey = 'sk.eyJ1Ijoic2Vkb25hY2hhbWJlciIsImEiOiJjaW5qYXFjeHYweG5hdWlranFxZHpxYXhrIn0.2oaLx8HQUCtSzef6ozEaiQ' datasets = Datasets() #tokenurl = "https://www.mapbox.com/core/tokens/v1?_="+str(int(time.time())) #r = requests.get(tokenurl); datasets.session.params['access_token'] = access_token datasets.list().json() response = Datasets(access_token=secretkey).read_dataset(idlayer).json() collection = datasets.list_features(idlayer).json() for q in collection: