From 5ec54957ed1fb0a77150e36f9483abbca44616c0 Mon Sep 17 00:00:00 2001
From: Julian Coy <julian.calvin.coy@gmail.com>
Date: Wed, 31 May 2017 11:40:27 -0400
Subject: [PATCH] Added delegate support, merged changes from ashkulz, and
 updated version

---
 .travis.yml           |  3 ++-
 README.rst            |  2 ++
 godaddypy/__init__.py |  2 +-
 godaddypy/account.py  | 16 ++++++++++++----
 godaddypy/client.py   |  2 +-
 requirements.txt      |  2 --
 test_requirements.txt |  3 +++
 tests/test_Client.py  | 13 +++++++++++++
 8 files changed, 34 insertions(+), 9 deletions(-)
 create mode 100644 test_requirements.txt

diff --git a/.travis.yml b/.travis.yml
index d2f3798..789ff62 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,6 +4,7 @@ python:
   - 3.4
 script: nosetests tests
 install:
-  - pip install -r requirements.txt
+  - pip install -r requirements.txt --user
+  - pip install -r test_requirements.txt --user
 notifications:
   email: false
diff --git a/README.rst b/README.rst
index d5ce5e4..8f6897d 100644
--- a/README.rst
+++ b/README.rst
@@ -32,7 +32,9 @@ Examples
     >>> from godaddypy import Client, Account
     >>>
     >>> my_acct = Account(api_key='PUBLIC_KEY', api_secret='SECRET_KEY')
+    >>> delegate_acct = Account(api_key='PUBLIC_KEY', api_secret='SECRET_KEY', delegate='X-Shopper-Id')
     >>> client = Client(my_acct)
+    >>> delegate_client = Client(delegate_acct)
     >>>
     >>> client.get_domains()
     ['domain1.example', 'domain2.example']
diff --git a/godaddypy/__init__.py b/godaddypy/__init__.py
index 422ff5f..2d9653f 100644
--- a/godaddypy/__init__.py
+++ b/godaddypy/__init__.py
@@ -1,5 +1,5 @@
 from .client import Client
 from .account import Account
 
-__version__ = '2.1.2'
+__version__ = '2.2.0'
 __all__ = ['Client', 'Account', '__version__']
diff --git a/godaddypy/account.py b/godaddypy/account.py
index cab2dac..c6161ff 100644
--- a/godaddypy/account.py
+++ b/godaddypy/account.py
@@ -11,7 +11,7 @@ class Account(object):
 
     _SSO_KEY_TEMPLATE = 'sso-key {api_key}:{api_secret}'
 
-    def __init__(self, api_key, api_secret):
+    def __init__(self, api_key, api_secret, delegate=None):
         """Create a new `godadypy.Account` object.
 
         :type api_key: str
@@ -23,8 +23,16 @@ class Account(object):
 
         self._api_key = api_key
         self._api_secret = api_secret
+        if delegate is not None:
+            self._delegate = delegate
 
-    def get_auth_headers(self):
-        return {
+    def get_headers(self):
+        headers = {
             'Authorization': self._SSO_KEY_TEMPLATE.format(api_key=self._api_key,
-                                                           api_secret=self._api_secret)}
+                                                           api_secret=self._api_secret)
+        }
+
+        if self._delegate is not None:
+            headers['X-Shopper-Id'] = self._delegate
+
+        return headers
diff --git a/godaddypy/client.py b/godaddypy/client.py
index d3b235d..1f3d78a 100644
--- a/godaddypy/client.py
+++ b/godaddypy/client.py
@@ -49,7 +49,7 @@ class Client(object):
         return url
 
     def _get_headers(self):
-        return self.account.get_auth_headers()
+        return self.account.get_headers()
 
     def _get_json_from_response(self, url, json=None, **kwargs):
         return self._request_submit(requests.get, url=url, json=json, **kwargs).json()
diff --git a/requirements.txt b/requirements.txt
index 174d8e2..fa4ced4 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1 @@
 requests>=2.4
-mock>=2.0.0
-callee>=0.2.1
diff --git a/test_requirements.txt b/test_requirements.txt
new file mode 100644
index 0000000..0b3debf
--- /dev/null
+++ b/test_requirements.txt
@@ -0,0 +1,3 @@
+mock>=2.0.0
+callee>=0.2.1
+nose>=1.3.7
diff --git a/tests/test_Client.py b/tests/test_Client.py
index aab0ab6..7f5ae47 100644
--- a/tests/test_Client.py
+++ b/tests/test_Client.py
@@ -65,6 +65,19 @@ class TestClient:
 
         put_mock.assert_called_once_with(callee.String(), json=[fake_records[0]])
 
+    def test_account_with_delegate(self):
+        _DELEGATE_ID = '1234987234jdsfasdf'
+        _PRIVATE_KEY = 'blahdeyblah'
+        _PUBLIC_KEY = 'hooeybalooooooeryasdfasdfsdfs'
+
+        acct = Account(api_key=_PUBLIC_KEY, api_secret=_PRIVATE_KEY, delegate=_DELEGATE_ID)
+
+        assert acct.get_headers().has_key('X-Shopper-Id')
+        assert acct.get_headers().has_key('Authorization')
+        assert acct.get_headers()['X-Shopper-Id'] == _DELEGATE_ID
+        assert acct.get_headers()['Authorization'] == Account._SSO_KEY_TEMPLATE.format(api_secret=_PRIVATE_KEY,
+                                                                                       api_key=_PUBLIC_KEY)
+
     def test_build_record_url_happy_path(self):
         domains = ['test.com', 'apple.com', 'google.com', 'aol.com']
         names = ['@', None, 'someName', None]
-- 
GitLab