Import Woosmap JSON
- Project and Private Key
- Import With Woosmap Console
- Import With Woosmap API
- HTTP library for Python
- Open and Post JSON File
- Useful Links
In this tutorial you will learn how to import a native Woosmap JSON to your Project, using the Woosmap Console or the Woosmap Data API.
- Application to upload your file: Woosmap Console
- Sample JSON file: foodmarkets.json
- Python Script: woosmapjson_import.py
Project and Private Key
If you have’nt created a project yet, you need to create one. Projects are containers for your Assets and associated analytics usage. On the console home page, click on the button. You’re prompted to input the name and domains corresponding to the allowed referrers where you’ll embed Woosmap.
Each project has, at least, one associated pair of keys. These keys are used to manage your Assets data programmatically, use REST and Javascript APIs, and display the dealer/store locator widget.
There are two kinds of keys:
- Public keys
- Private keys
Public keys are used to implement Woosmap features on the client-side. They allow you to retrieve your Asset data and benefit from the read-only capabilities of Woosmap APIs. Private keys allow you to manage integrations on the server-side and perform creation of new and updates of existing Assets.
The public key is automatically generated when you add a new project to your organization, while you need to create the private key manually.
Import With Woosmap Console
Once created, you can push a native Asset dataset using the POST
method. (for testing, download this sample file: foodmarkets.json)
Please note that JSON file containing more than 500 assets, will be split to slices. On error, each data slice will independently fail to load. This may result to load only partial data.
Import With Woosmap API
If you want to have more control over your data management, you can develop a custom script or application implementing the dedicated Woosmap Data API.
Here are the prerequisites for the following sample:
- Python 2.7 or greater.
- The pip package management tool.
- Access to the internet.
- A Woosmap account.
HTTP library for Python
The Woosmap Data API is a RESTful API with endpoint https://api.woosmap.com/stores
. It takes a mandatory parameter : your private key. Therefore the url you call should look like this: htps://api.woosmap.com/stores?private_key=YOUR_PRIVATE_KEY
.
We use Requests to call the Woosmap API. Simply run this command in your terminal of choice to install it using pip:
$ pip install requests
The API provides 4 HTTP methods for interacting with resources but in our case we are interested in creating or replacing new resources so we’ll use the POST
method after executed a DELETE
. Don’t forget to replace the WOOSMAP_PRIVATE_API_KEY
with your own.
import requests
WOOSMAP_PRIVATE_API_KEY = '23713926-1af5-4321-ba54-032966f6e95d'
class Woosmap:
"""A wrapper around the Woosmap Data API."""
WOOSMAP_API_HOSTNAME = 'api.woosmap.com'
def __init__(self):
self.session = requests.Session()
def delete(self):
self.session.delete('https://{hostname}/stores/'.format(
hostname=self.WOOSMAP_API_HOSTNAME),
params={'private_key': WOOSMAP_PRIVATE_API_KEY})
def post(self, payload):
return self.session.post('https://{hostname}/stores/'.format(
hostname=self.WOOSMAP_API_HOSTNAME),
params={'private_key': WOOSMAP_PRIVATE_API_KEY},
json={'stores': payload})
def end(self):
self.session.close()
The Woosmap Data API allows you to update your stores using PUT
http verb. The update method is based on unique identifier of your data so be careful to keep the same identifier.
Also, you are able to replace in one API call the whole project Assets by calling the endpoint /stores/replace
with POST
method.
Check the full documentation for more detail.
Open and Post JSON File
The content of your Assets attributes need to be sent in the body of the HTTP POST
request.
Just open the JSON file with python native json
module and call the Woosmap API Wrapper described above with the content of the JSON as parameter.
import json
def import_assets():
WOOSMAP_JSON_FILE = 'foodmarkets.json'
with open(WOOSMAP_JSON_FILE, 'rb') as f:
try:
assets = json.loads(f.read())
woosmap_api_helper = Woosmap()
woosmap_api_helper.delete()
response = woosmap_api_helper.post(assets['stores'])
if response.status_code >= 400:
response.raise_for_status()
else:
print('Successfully imported')
except requests.exceptions.HTTPError as http_exception:
if http_exception.response.status_code >= 400:
print('API Error: {0}'.format(http_exception.response.text))
else:
print('Error requesting API: {0}'.format(http_exception))
except Exception as exception:
print('Failed importing Assets! {0}'.format(exception))
finally:
woosmap_api_helper.end()
Useful Links
- Login to Woosmap Console
- Woosmap Data API Quick Start
- Python Script : woosmapjson_import.py
- Sample JSON File : foodmarkets.json
- Sign Up for a Woosmap Account