Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

Commit

Permalink
Cleaned up my first draft of the put method a little bit. Fixes #16.
Browse files Browse the repository at this point in the history
  • Loading branch information
palewire committed Apr 10, 2011
1 parent 87d2919 commit 456f73c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
17 changes: 15 additions & 2 deletions documentcloud.py
Expand Up @@ -68,6 +68,7 @@ def put(self):
* access
* published_url
Returns nothing.
"""
params = dict(
title=self.title,
Expand Down Expand Up @@ -454,6 +455,8 @@ def put(self, method, params):
"""
Post changes back to DocumentCloud
"""
if not self.username and not self.password:
raise CredentialsMissingError("This is a private method. You must provide a username and password when you initialize the DocumentCloud client to attempt this type of request.")
# Assemble the URL
url = self.BASE_URI + method
# Prepare the params
Expand All @@ -465,8 +468,16 @@ def put(self, method, params):
encoded_credentials = base64.encodestring(credentials).replace("\n", "")
header = 'Basic %s' % encoded_credentials
request.add_header('Authorization', header)
response = urllib2.urlopen(request)
print response.code
# Make the request
try:
response = urllib2.urlopen(request)
except urllib2.HTTPError, e:
if e.code == 404:
raise DoesNotExistError("The resource you've requested does not exist or is unavailable without the proper credentials.")
elif e.code == 401:
raise CredentialsFailedError("The resource you've requested requires proper credentials.")
else:
raise e

def fetch(self, method, params=None):
"""
Expand Down Expand Up @@ -537,6 +548,7 @@ def search(self, query):
"""
page = 1
document_list = []
# Loop through all the search pages and fetch everything
while True:
results = self._get_search_page(query, page=page, per_page=1000)
if results:
Expand Down Expand Up @@ -632,6 +644,7 @@ def __init__(self, username=None, password=None):
private = DocumentCloud(DOCUMENTCLOUD_USERNAME, DOCUMENTCLOUD_PASSWORD)
bad = DocumentCloud("Bad", "Login")
obj = private.documents.get(u'15144-mitchrpt')
print obj.resources.related_article
#print obj.title
#obj.title = 'The Mitchell Report (w00t!)'
#print obj.title
Expand Down
10 changes: 10 additions & 0 deletions test.py
@@ -1,4 +1,14 @@
#! /usr/bin/env python
"""
Tests out the DocumentCloud API.
Most requests require authentication, which I'm not sure how deal with properly
in this circumstance. For the time being, I'm importing latimes.com credentials
and fiddling around with junk files I've placed in there. Obviously, that means
this test suite will only work on my computer, which seems like a problem.
If you know how I ought to sort this sort of thing out, please let me know.
"""
import random
import string
import unittest
Expand Down

0 comments on commit 456f73c

Please sign in to comment.