Skip to content

Commit

Permalink
Merge pull request #2 from cordmata/master
Browse files Browse the repository at this point in the history
use requests.Session
  • Loading branch information
cordmata committed Apr 23, 2014
2 parents b43fe43 + d2dc41d commit b8a29e4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 68 deletions.
53 changes: 18 additions & 35 deletions handle/client.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from os.path import join
from urllib import quote

import requests

NO_CACHE = {"Cache-Control": "no-cache"}


class Client(object):
"""This is a client for interacting with the ASU Handle
Administration Web Service."""
"""Client for interacting with the ASU Handle Web Service."""

def __init__(self, url, username, password):
self.baseurl = url.rstrip("/")
self.auth = (username, password)
self.baseurl = url.rstrip('/') + '/'
self.session = requests.Session()
self.session.auth = (username, password)
self.session.verify = False
self.session.headers.update({
'Cache-Control': 'no-cache',
'User-Agent': 'asu-handle-client'
})

def create(self, handle, target):
"""
Expand All @@ -32,12 +32,9 @@ def create(self, handle, target):
Example: '2286.9/af4y7fkd'
"""
resp = requests.post(
self.URL(handle),
data={"target": target},
headers=NO_CACHE,
auth=self.auth,
verify=False

resp = self.session.post(
self.baseurl + handle, data={"target": target}
)
if resp.status_code is 201:
return resp.headers.get("location")
Expand All @@ -50,13 +47,9 @@ def read(self, handle):
@param handle: in the form of 'prefix/suffix'
@return: URL the handle will resolve to.
"""
resp = requests.get(
self.URL(handle),
auth=self.auth,
headers=NO_CACHE,
verify=False
)
resp = self.session.get(self.baseurl + handle)
if resp.status_code is 204:
return resp.headers.get("location")
raise HandleError(resp.status_code)
Expand All @@ -69,13 +62,10 @@ def update(self, handle, target):
@param target: Valid URL of the new target.
@return: The handle that was either updated or created.
"""
resp = requests.put(
self.URL(handle),
params={"target": target},
auth=self.auth,
headers=NO_CACHE,
verify=False
resp = self.session.put(
self.baseurl + handle, params={"target": target}
)
status = resp.status_code
if status is 201 or status is 204:
Expand All @@ -86,17 +76,10 @@ def delete(self, handle):
"""Remove the handle completely from the registry.
@param handle: The handle to remove."""
resp = requests.delete(
self.URL(handle),
auth=self.auth,
headers=NO_CACHE,
verify=False
)
resp = self.session.delete(self.baseurl + handle)
if resp.status_code is not 204:
raise HandleError(resp.status_code)

def URL(self, handle):
return join(self.baseurl, quote(handle))

class HandleError(Exception):

Expand Down
40 changes: 16 additions & 24 deletions handle/tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import unittest

from mock import Mock, patch

import client
from mock import Mock
from client import Client, HandleError

HANDLE = "2286/test"
Expand All @@ -15,63 +13,57 @@
"doesNot", "matter"
)


class HandleClientTest(unittest.TestCase):

@patch("client.requests")
def test_create(self, requests):
def test_create(self):
response = Mock()
response.status_code = 201
response.headers = {"location": HANDLE_URL}
requests.post = Mock(return_value=response)
CLIENT.session.post = Mock(return_value=response)
hdl = CLIENT.create(HANDLE, TARGET)
self.assertEqual(hdl, HANDLE_URL)

@patch("client.requests")
def test_read(self, requests):
def test_read(self):
response = Mock()
response.status_code = 204
response.headers = {"location": TARGET}
requests.get = Mock(return_value=response)
CLIENT.session.get = Mock(return_value=response)
target = CLIENT.read(HANDLE)
self.assertEqual(target, TARGET)

@patch("client.requests")
def test_update(self, requests):
def test_update(self):
response = Mock()
response.status_code = 204
response.headers = {"location": HANDLE_URL}
requests.put = Mock(return_value=response)
CLIENT.session.put = Mock(return_value=response)
hdl = CLIENT.update(HANDLE, TARGET)
self.assertEqual(hdl, HANDLE_URL)

@patch("client.requests")
def test_delete(self, requests):
def test_delete(self):
response = Mock()
response.status_code = 204
requests.delete = Mock(return_value=response)
CLIENT.session.delete = Mock(return_value=response)
target = CLIENT.delete("2286/test")

@patch("client.requests")
def test_not_found(self, requests):
def test_not_found(self):
response = Mock()
response.status_code = 404
requests.get = Mock(return_value=response)
CLIENT.session.get = Mock(return_value=response)
with self.assertRaisesRegexp(HandleError, "Handle not found."):
CLIENT.read(HANDLE)

@patch("client.requests")
def test_bad_request(self, requests):
def test_bad_request(self):
response = Mock()
response.status_code = 400
requests.put = Mock(return_value=response)
CLIENT.session.put = Mock(return_value=response)
with self.assertRaisesRegexp(HandleError, "Bad req*"):
CLIENT.update(HANDLE, BAD_TARGET)

@patch("client.requests")
def test_unauthorized(self, requests):
def test_unauthorized(self):
response = Mock()
response.status_code = 403
requests.post = Mock(return_value=response)
CLIENT.session.post = Mock(return_value=response)
with self.assertRaisesRegexp(HandleError, "You don't*"):
CLIENT.create(HANDLE, BAD_TARGET)

Expand Down
21 changes: 12 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from setuptools import setup, find_packages

setup(
name = "py-handleclient",
version = "0.5",
packages = find_packages(),
install_requires = [
'requests >= 0.14'
name="py-handleclient",
version="0.6",
packages=find_packages(),
install_requires=[
'requests >= 2.0'
],
author = "Matt Cordial",
author_email = "[email protected]",
description = "A client which communicates with the Handle Administration Web Service.",
keywords = "cnri handle server web-service",
author="Matt Cordial",
author_email="[email protected]",
description=(
"A client which communicates with the Handle Administration "
"Web Service."
),
keywords="cnri handle server web-service",
)

0 comments on commit b8a29e4

Please sign in to comment.