WeoGeo API: Python Examples, Part 2

The Developer Documentation for WeoGeo’s API provides examples using curl.  In this blog series, I’m providing similar examples but using Python.  In WeoGeo API: Python Examples, Part 1, I showed how to invoke the “GET Dataset as WeoFile” API from Python using urllib.  This time, I’ll show the same request using httplib.

Httplib is a little more complicated but it handles authentication slightly better and, more importantly, specifying the headers will enable more sophisticated requests (that will be required for later API examples).

The curl example for the API call is below:

List Datasets API: curl example

And here is a bare-bones example of that request in Python using httplib:

import base64
import string
import httplib, urllib

usern = "your_username" # or your API key
passw = "your_password" # if API Key is used: = ""
library = "the * part of your Library's domain name in *.weogeo.com"

# Construct the Basic authentication string
auth = 'Basic ' + string.strip(base64.encodestring(usern + ':' + passw))

# List Datasets (GET) curl example
# curl -H 'Content-Type: application/xml' http://#{hostname}/datasets.weo?page=1&east=0&north=90&south=0&west=-180&scale=5

# Optional parameters:
params = urllib.urlencode({
 'page': '1',
 'per_page': '15',
 'north': '90.0',
 'south': '-90.0',
 'east': '180.0',
 'west': '-180.0',
 'date_from': '1990-01-01',
 'date_to': '2010-01-01',
 'data_type': 'RASTER',
 'min_provider_rating': '0',
 'min_dataset_rating': '0',
 'tags': 'test'
})

# create the header with the authentication
headers = {
 "Content-Type": "application/xml" ,
 "Authorization": auth,
 }

# create teh connection
conn = httplib.HTTPConnection("dandye.weogeo.com")

# Construct the request with headers amd add paramters to the URI
conn.request("GET","/datasets.weo?%s" % params, "", headers)

# call the URI
response = conn.getresponse()

# check status of result
print response.status, response.reason

# read the data retreieved
data = response.read()

# close the connection
conn.close()

# print the data retreieved
print data

Notice on Line 9 that your username and password are base64 encoded.  This is basic access authentication and encoding != encrypting.  So for added security, you may use your Library API Key in place of your username and leave your password blank (password = “”).

Again, for extra credit, try changing the extension (on line 40) from “.weo” to [".kml",".json",".rss"].

Tags:

Leave a Reply