12Parker/python-migrations
092
1{"repo": "byrnereese/uphold-sdk-python", "pull_number": 2, "instance_id": "byrnereese__uphold-sdk-python-2", "issue_numbers": "", "base_commit": "87eb118e85e1d241c038012afc7e0a234f67ba83", "patch": "diff --git a/bitreserve/bitreserve.py b/bitreserve/bitreserve.py\n--- a/bitreserve/bitreserve.py\n+++ b/bitreserve/bitreserve.py\n@@ -17,10 +17,12 @@\n url = 'https://api.bitreserve.org/v1/reserve/transactions/a97bb994-6e24-4a89-b653-e0a6d0bcf634'\n \"\"\"\n \n-import urllib3\n-import certifi\n+from __future__ import print_function, unicode_literals\n+\n+import requests\n import json\n-import version\n+from .version import __version__\n+\n \n class Bitreserve(object):\n \"\"\"\n@@ -30,12 +32,10 @@ class Bitreserve(object):\n def __init__(self, host='api.bitreserve.org'):\n self.host = host\n self.version = 0\n- self.http = urllib3.PoolManager(\n- cert_reqs='CERT_REQUIRED', # Force certificate check.\n- ca_certs=certifi.where(), # Path to the Certifi bundle.\n- )\n+ self.session = requests.Session()\n self.headers = { 'Content-type' : 'application/x-www-form-urlencoded',\n- 'User-Agent' : 'bitreserve-python-sdk/' + version.__version__ }\n+ 'User-Agent' : 'bitreserve-python-sdk/' + __version__ }\n+ self.pat = None\n \n \n def auth(self, username, password):\n@@ -63,6 +63,9 @@ def auth(self, username, password):\n self.headers['Authorization'] = 'Bearer ' + self.token\n return data\n \n+ def auth_pat(self, pat):\n+ self.pat = pat\n+\n def get_me(self):\n \"\"\"\n Returns a hash containing a comprehensive summary of the current user in content. The data\n@@ -71,8 +74,7 @@ def get_me(self):\n :rtype:\n A hash containing all user's properties.\n \"\"\"\n- uri = self._build_url('/me')\n- return self._get( uri )\n+ return self._get('/me')\n \n \"\"\"\n def get_addresses(self):\n@@ -87,8 +89,7 @@ def get_contacts(self):\n :rtype:\n An array of hashes containing all the contacts of the current user's properties.\n \"\"\"\n- uri = self._build_url('/me/contacts')\n- return self._get( uri )\n+ return self._get('/me/contacts')\n \n def get_cards(self):\n \"\"\"\n@@ -109,8 +110,7 @@ def get_card(self, c):\n :rtype:\n An array of hashes containing all the cards of the current user.\n \"\"\"\n- uri = self._build_url('/me/cards/' + c)\n- return self._get( uri )\n+ return self._get('/me/cards/' + c)\n \n def get_phones(self):\n \"\"\"\n@@ -119,8 +119,7 @@ def get_phones(self):\n :rtype:\n An array of hashes containing all the phone numbers of the current user.\n \"\"\"\n- uri = self._build_url('/me/phones')\n- return self._get( uri )\n+ return self._get('/me/phones')\n \n def get_reserve_status(self):\n \"\"\"\n@@ -133,8 +132,7 @@ def get_reserve_status(self):\n :rtype:\n An array of hashes summarizing the reserve.\n \"\"\"\n- uri = self._build_url('/reserve')\n- return self._get( uri )\n+ return self._get('/reserve')\n \n def get_reserve_ledger(self):\n \"\"\"\n@@ -144,8 +142,7 @@ def get_reserve_ledger(self):\n :rtype:\n An array of ledger entries.\n \"\"\"\n- uri = self._build_url('/reserve/ledger')\n- return self._get( uri )\n+ return self._get('/reserve/ledger')\n \n def get_reserve_chain(self):\n \"\"\"\n@@ -155,8 +152,7 @@ def get_reserve_chain(self):\n :rtype:\n An array of transactions.\n \"\"\"\n- uri = self._build_url('/reserve/transactions')\n- return self._get( uri )\n+ return self._get('/reserve/transactions')\n \n def prepare_txn(self, card, to, amount, denom):\n \"\"\"\n@@ -169,23 +165,23 @@ def prepare_txn(self, card, to, amount, denom):\n :param String to The recipient of the funds. Can be in the form of a bitcoin \n address, an email address, or a Bitreserve membername.\n \n- :param Float amount The amount to send.\n+ :param Float/Decimal amount The amount to send.\n \n :param String denom The denomination to send. Permissible values are USD, GBP,\n CNY, JPY, EUR, and BTC.\n \n :rtype:\n- A string representing a handle to a transaction promise.\n+ A transaction object.\n \"\"\"\n fields = {\n- 'denomination[currency]':'USD',\n- 'denomination[amount]':0.01,\n- 'destination':'byrne+13@bitreserve.org'}\n- data = self._post('/me/cards/'+card+'/transactions/new', fields);\n- fields[\"signature\"] = data[\"signature\"]\n- return data[\"signature\"]\n-\n- def execute_txn(self, card, to, amount, denom, sig=''):\n+ 'denomination[currency]': denom,\n+ 'denomination[amount]': str(amount),\n+ 'destination': to\n+ }\n+ data = self._post('/me/cards/' + card + '/transactions', fields);\n+ return data['id']\n+\n+ def execute_txn(self, card, transaction, message=''):\n \"\"\"\n Executes a transaction. This is an atomic operation and cannot be reversed.\n When an optional sig parameter is provided a previously quoted market rate\n@@ -195,27 +191,15 @@ def execute_txn(self, card, to, amount, denom, sig=''):\n \n :param String card_id The card ID from which to draw funds.\n \n- :param String to The recipient of the funds. Can be in the form of a bitcoin \n- address, an email address, or a Bitreserve membername.\n- \n- :param Float amount The amount to send.\n-\n- :param String denom The denomination to send. Permissible values are USD, GBP,\n- CNY, JPY, EUR, and BTC.\n-\n- :param String promise (optional) The promise handle guaranteeing a previously\n- quoted market rate for the values specified.\n+ :param String transaction Id of the transaction as returned by prepare_txn.\n \n :rtype:\n- A string representing a handle to a transaction promise.\n+ A transaction object\n \"\"\"\n- fields = {\n- 'denomination[currency]':'USD',\n- 'denomination[amount]':0.01,\n- 'destination':'byrne+13@bitreserve.org'}\n- if sig != '':\n- fields['signature'] = sig\n- return self._post('/me/cards/'+card+'/transactions', fields);\n+ fields = {}\n+ if message:\n+ fields['message'] = message\n+ return self._post('/me/cards/' + card + '/transactions/' + transaction + '/commit', fields);\n \n def get_ticker(self, t=''):\n \"\"\"\n@@ -229,44 +213,52 @@ def get_ticker(self, t=''):\n An array of market rates indexed by currency.\n \"\"\"\n if t:\n- uri = self._build_url('/ticker/' + t )\n+ uri = '/ticker/' + t\n else:\n- uri = self._build_url('/ticker')\n- return self._get( uri )\n+ uri = '/ticker'\n+ return self._get(uri)\n \n \"\"\"\n HELPER FUNCTIONS\n \"\"\"\n \n def _build_url(self, uri):\n+ if uri.startswith('/oauth2'):\n+ return uri\n return '/v' + str(self.version) + uri\n \n def _post(self, uri, params):\n \"\"\"\n \"\"\"\n- url = 'https://' + self.host + uri\n+ url = 'https://' + self.host + self._build_url(uri)\n \n # You're ready to make verified HTTPS requests.\n try:\n- response = self.http.request_encode_body('POST', url, params, self.headers, False)\n- except urllib3.exceptions.SSLError as e:\n+ if self.pat:\n+ response = self.session.post(url, data=params, headers=self.headers, auth=(self.pat, 'X-OAuth-Basic'))\n+ else:\n+ response = self.session.post(url, data=params, headers=self.headers)\n+ except requests.exceptions.SSLError as e:\n # Handle incorrect certificate error.\n- print \"Failed certificate check\"\n+ print(\"Failed certificate check\")\n \n- data = json.loads(response.data)\n+ data = json.loads(response.text)\n return data\n \n def _get(self, uri):\n \"\"\"\n \"\"\"\n- url = 'https://' + self.host + uri\n+ url = 'https://' + self.host + self._build_url(uri)\n \n # You're ready to make verified HTTPS requests.\n try:\n- response = self.http.request('GET', url, headers=self.headers)\n- except urllib3.exceptions.SSLError as e:\n+ if self.pat:\n+ response = self.session.get(url, headers=self.headers, auth=(self.pat, 'X-OAuth-Basic'))\n+ else:\n+ response = self.session.get(url, headers=self.headers)\n+ except requests.exceptions.SSLError:\n # Handle incorrect certificate error.\n- print \"Failed certificate check\"\n+ print(\"Failed certificate check\")\n \n- data = json.loads(response.data)\n+ data = json.loads(response.text)\n return data\ndiff --git a/setup.py b/setup.py\nnew file mode 100644\n--- /dev/null\n+++ b/setup.py\n@@ -0,0 +1,13 @@\n+from distutils.core import setup\n+setup(\n+ name = 'bitreserve',\n+ packages = ['bitreserve'],\n+ version = '0.1',\n+ description = \"Library for bitreserve.org's API\",\n+ author = 'Jo\u00e3o Miguel Neves',\n+ author_email = 'joao@silvaneves.org',\n+ url = 'https://github.com/jneves/bitreserve-python-sdk',\n+ download_url = 'https://github.com/jneves/bitreserve-python-sdk/tarball/0.1',\n+ keywords = ['bitreserve', 'currency', 'trading', 'api'],\n+ classifiers = [],\n+)\n", "test_patch": "diff --git a/samples/test.py b/samples/test.py\n--- a/samples/test.py\n+++ b/samples/test.py\n@@ -1,35 +1,40 @@\n+from __future__ import print_function, unicode_literals\n+\n import urllib3\n import locale\n-import ConfigParser\n+try:\n+ from configparser import ConfigParser\n+except:\n+ from ConfigParser import ConfigParser\n import sys\n \n sys.path.append('.')\n from bitreserve import Bitreserve\n \n locale.setlocale(locale.LC_ALL, 'en_US')\n-Config = ConfigParser.ConfigParser()\n+Config = ConfigParser()\n Config.read('samples/config.ini')\n \n api = Bitreserve()\n api.auth( Config.get('Settings','username'), Config.get('Settings','password') )\n-print \"Getting user data...\"\n+print(\"Getting user data...\")\n me = api.get_me()\n-print \"First name: \" + me['firstName']\n-print \"Last name: \" + me['lastName']\n+print(\"First name: {}\".format(me['firstName']))\n+print(\"Last name: {}\".format(me['lastName']))\n \n-print \"\\nGetting cards...\"\n+print(\"\\nGetting cards...\")\n cards = api.get_cards()\n for card in cards:\n- print card['label'] + \": \" + card[\"available\"] + \" (\" + card[\"id\"] + \")\" \n+ print(card['label'] + \": \" + card[\"available\"] + \" (\" + card[\"id\"] + \")\")\n \n-print \"\\nGetting USD Card...\"\n+print(\"\\nGetting USD Card...\")\n usd_card = api.get_card(\"20c0ccf3-e316-40c1-8a2a-982dd92a96ca\")\n-print usd_card['label']\n+print(usd_card['label'])\n \n-print \"\\nGetting contacts...\"\n+print(\"\\nGetting contacts...\")\n contacts = api.get_contacts()\n for contact in contacts:\n- print contact['firstName'] + \" \" + contact[\"lastName\"]\n+ print(contact['firstName'] + \" \" + contact[\"lastName\"])\n \n '''\n print \"\\nGetting addresses...\"\n@@ -38,12 +43,12 @@\n print addr\n '''\n \n-print \"\\nGetting phones...\"\n+print(\"\\nGetting phones...\")\n phones = api.get_phones()\n for phone in phones:\n- print phone['internationalMasked']\n+ print(phone['internationalMasked'])\n \n-print \"\\nGetting reserve status...\"\n+print(\"\\nGetting reserve status...\")\n stats = api.get_reserve_status()\n for stat in stats:\n cur = stat[\"currency\"]\n@@ -51,11 +56,11 @@\n if norm[\"currency\"] == \"USD\":\n break\n if cur == \"USD\":\n- print cur + \": liabilities=\" + locale.currency( float(stat[\"liabilities\"]), grouping=True ) + \", assets=\" + locale.currency( float(stat[\"assets\"]), grouping=True )\n+ print(cur + \": liabilities=\" + locale.currency( float(stat[\"liabilities\"]), grouping=True ) + \", assets=\" + locale.currency( float(stat[\"assets\"]), grouping=True ))\n else:\n- print cur + \": liabilities=\" + stat[\"liabilities\"] + \" (\" + locale.currency( float(norm[\"liabilities\"]), grouping=True ) + \"), assets=\" + stat[\"assets\"] + \" (\" + locale.currency( float(norm[\"assets\"]), grouping=True ) + \")\" \n+ print(cur + \": liabilities=\" + stat[\"liabilities\"] + \" (\" + locale.currency( float(norm[\"liabilities\"]), grouping=True ) + \"), assets=\" + stat[\"assets\"] + \" (\" + locale.currency( float(norm[\"assets\"]), grouping=True ) + \")\")\n \n-print \"\\nGetting ledger (first 20 entries)...\"\n+print(\"\\nGetting ledger (first 20 entries)...\")\n entries = api.get_reserve_ledger()\n i = 0\n for entry in entries:\n@@ -63,25 +68,25 @@\n if i > 20:\n break\n if entry[\"in\"]: \n- print str(i) + \". \" + entry['type'] + \": +\" + entry[\"in\"][\"amount\"] + \" \" + entry[\"in\"][\"currency\"]\n+ print(str(i) + \". \" + entry['type'] + \": +\" + entry[\"in\"][\"amount\"] + \" \" + entry[\"in\"][\"currency\"])\n if entry[\"out\"]: \n- print str(i) + \". \" + entry['type'] + \": -\" + entry[\"in\"][\"amount\"] + \" \" + entry[\"in\"][\"currency\"]\n+ print(str(i) + \". \" + entry['type'] + \": -\" + entry[\"in\"][\"amount\"] + \" \" + entry[\"in\"][\"currency\"])\n \n-print \"\\nGetting transactions (first 20 entries)...\"\n+print(\"\\nGetting transactions (first 20 entries)...\")\n entries = api.get_reserve_chain()\n i = 0\n for entry in entries:\n i += 1\n if i > 20:\n break\n- print str(i) + \". \" + entry['origin']['amount'] + \" \" + entry[\"origin\"][\"currency\"] + \" => \" + entry['destination']['amount'] + \" \" + entry[\"destination\"][\"currency\"]\n+ print(str(i) + \". \" + entry['origin']['amount'] + \" \" + entry[\"origin\"][\"currency\"] + \" => \" + entry['destination']['amount'] + \" \" + entry[\"destination\"][\"currency\"])\n \n-print \"\\nGetting all tickers...\"\n+print(\"\\nGetting all tickers...\")\n tic = api.get_ticker()\n-print \"ok.\"\n+print(\"ok.\")\n \n tic = api.get_ticker('USD')\n-print \"EUR => USD: \" + tic['EURUSD']['bid']\n+print(\"EUR => USD: \" + tic['EURUSD']['bid'])\n \n exit(0)\n \ndiff --git a/tests.py b/tests.py\nnew file mode 100644\n--- /dev/null\n+++ b/tests.py\n@@ -0,0 +1,143 @@\n+# -*- coding: utf-8 -*-\n+from __future__ import print_function, unicode_literals\n+from unittest import TestCase, main, skip\n+from mock import Mock, patch\n+from decimal import Decimal\n+\n+from bitreserve import Bitreserve\n+\n+\n+class FakeResponse(object):\n+ status = 200\n+ text = ''\n+\n+\n+class TestAuthentication(TestCase):\n+ def setUp(self):\n+ pass\n+\n+ def test_(self):\n+ pass\n+\n+\n+class TestCurrencies(TestCase):\n+ def setUp(self):\n+ pass\n+\n+ def test_(self):\n+ pass\n+\n+\n+class TestTicker(TestCase):\n+ def setUp(self):\n+ pass\n+\n+ def test_(self):\n+ pass\n+\n+\n+class TestCard(TestCase):\n+ def setUp(self):\n+ pass\n+\n+ def test_(self):\n+ pass\n+\n+\n+class TestContact(TestCase):\n+ def setUp(self):\n+ pass\n+\n+ def test_(self):\n+ pass\n+\n+\n+class TestCurrencyPair(TestCase):\n+ def setUp(self):\n+ pass\n+\n+ def test_(self):\n+ pass\n+\n+\n+fake_transaction_response = FakeResponse()\n+fake_transaction_response.text = '''{\n+ \"id\": \"7c377eba-cb1e-45a2-8c13-9807b4139bec\",\n+ \"type\": \"transfer\",\n+ \"message\": null,\n+ \"status\": \"pending\",\n+ \"RefundedById\":null,\n+ \"createdAt\": \"2014-08-27T00:01:11.616Z\",\n+ \"denomination\": {\n+ \"amount\": \"0.1\",\n+ \"currency\": \"BTC\",\n+ \"pair\": \"BTCBTC\",\n+ \"rate\": \"1.00\"\n+ },\n+ \"origin\": {\n+ \"CardId\": \"66cf2c86-8247-4094-bbec-ca29cea8220f\",\n+ \"amount\": \"0.1\",\n+ \"base\": \"0.1\",\n+ \"commission\": \"0.00\",\n+ \"currency\": \"BTC\",\n+ \"description\": \"John Doe\",\n+ \"fee\": \"0.00\",\n+ \"rate\": \"1.00\",\n+ \"type\": \"card\",\n+ \"username\": \"johndoe\"\n+ },\n+ \"destination\": {\n+ \"amount\": \"0.1\",\n+ \"base\": \"0.1\",\n+ \"commission\": \"0.00\",\n+ \"currency\": \"BTC\",\n+ \"description\": \"foo@bar.com\",\n+ \"fee\": \"0.00\",\n+ \"rate\": \"1.00\",\n+ \"type\": \"email\"\n+ },\n+ \"params\": {\n+ \"currency\": \"BTC\",\n+ \"margin\": \"0.00\",\n+ \"pair\": \"BTCBTC\",\n+ \"rate\": \"1.00\",\n+ \"ttl\": 30000,\n+ \"type\": \"invite\"\n+ }\n+}'''\n+\n+ \n+class TestTransaction(TestCase):\n+ def setUp(self):\n+ self.api = Bitreserve()\n+ #self.api.auth('user', 'password')\n+\n+ @patch('requests.Session.post', Mock(return_value=fake_transaction_response))\n+ def test_prepare_txn(self):\n+ res = self.api.prepare_txn(\n+ '66cf2c86-8247-4094-bbec-ca29cea8220f',\n+ 'foo@bar.com',\n+ Decimal('1.00'),\n+ 'BTC'\n+ )\n+ self.assertEqual(res, '7c377eba-cb1e-45a2-8c13-9807b4139bec')\n+\n+ @patch('requests.Session.post', Mock(return_value=fake_transaction_response))\n+ def test_execute_txn(self):\n+ res = self.api.execute_txn(\n+ '66cf2c86-8247-4094-bbec-ca29cea8220f',\n+ '7c377eba-cb1e-45a2-8c13-9807b4139bec',\n+ )\n+ self.assertEqual(res['id'], '7c377eba-cb1e-45a2-8c13-9807b4139bec')\n+\n+ \n+class TestUser(TestCase):\n+ def setUp(self):\n+ pass\n+\n+ def test_(self):\n+ pass\n+\n+\n+if __name__ == '__main__':\n+ main()\n", "problem_statement": "", "hints_text": "", "created_at": "2015-07-15T20:08:46Z"}2 