Skip to content

WeoGeo API: Python Examples, Part 4 (PUTting XML)

January 21, 2010
by DanDye

In this blog series, we’ve looked at WeoGeo API examples using the GET and POST HTTP Verbs.  These were used to GET a List of Datasets and to POST a request for new Tokens in a WeoGeo Library.  Today, we’re going to PUT an XML file in order to update the Name, Description, and Tags associated with a Dataset.

First, let’s take a look at the dataset to be updated.  This Listing on my WeoGeo Library has dummy descriptive data for the name, description, and tags:

Panel 4: Preview with dummy data

Using the GET Dataset as WeoFile API in a web-browser, we can view this descriptive data as XML:

GET Dataset as WeoFile

For the new values, I’ve created an XML file that contains the dataset tag and child nodes for each of the fields that I want updated:

new values in xml

I’ve saved that file locally to my local file system and will read it from the following Python script:

[python]
import base64
import string
import httplib, urllib

# Programmer defined variables
usern = "your api key"
passw = "" # Null string when API Key is used
library = "yourLibraryDomain" # the %s part of %s.weogeo.com
token = "the dataset token"
new_xml = "./data/new_xml.xml"  #path in my file-system
# No more programmer changes

# set up the authentication string
auth = ‘Basic ‘ + string.strip(base64.encodestring(usern + ‘:’ + passw))

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

# read the XML data into body and print it
body = open(new_xml).read()
print body

# create the connection
conn = httplib.HTTPSConnection("%s.weogeo.com" % library)

# Construct the request with headers and XML in the body
conn.request("PUT","/datasets/%s.weo" % token, body, headers)

# call the URI
response = conn.getresponse()

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

# store the response in data
data = response.read()

# close the connection
conn.close()

# output the results
print data
[/python]

This is very similar to the last example but note the following changes:

  1. the HTTP verb PUT is used in place of POST
  2. the variable body is used on line 29 where a null string was used before

Calling this Python script from the console outputs the following:

[shell]
S:\>python put_example.py
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<name>Counties in the Southeast</name>
<description>
<![CDATA[
<h1>Big Header<h1>
Some <i>HTML</i> elements like <b>bold text</b> and
<a href="http://blogs.weogeo.com/dandye">links</a> are allowed.
]]>
</description>
<tags>TIGER shapefile political_boundaries counties</tags>
</dataset>
200 OK

S:\>
[/shell]

The 200 response indicates that all went well with the update and we can verify that by looking at the updated listing in the Preview Panel:

Panel 4: Preview with updated data


Comments are closed.