From c283681b4fa859460e6cb0372c87f517d6a24dce Mon Sep 17 00:00:00 2001 From: Ryan Young Date: Sat, 12 Oct 2019 16:28:58 -0700 Subject: [PATCH] Switch to NiceHash's demo API implementation --- nuxhash/nicehash.py | 64 ++++-------------------------------------- tests/test_nicehash.py | 12 -------- 2 files changed, 6 insertions(+), 70 deletions(-) diff --git a/nuxhash/nicehash.py b/nuxhash/nicehash.py index 4983c85..eeb42e7 100644 --- a/nuxhash/nicehash.py +++ b/nuxhash/nicehash.py @@ -1,71 +1,19 @@ -import json -import logging +from nuxhash.nhrest.python import nicehash as nh -from requests import Request, Session - -API2_ENDPOINT = 'https://api2.nicehash.com/main/api/v2/' -TIMEOUT = 20 - - -class NicehashException(Exception): - """An exceptional reponse returned by the NiceHash service.""" - pass - - -class APIError(NicehashException): - - def __init__(self, result): - self.result = result - - def __str__(self): - return f"NH: {self.result['error']}" - - -class BadResponse(NicehashException): - - def __str__(self): - return 'Bad JSON response' - - -def api2_get(*path, **params): - return Request('GET', f"{API2_ENDPOINT}{'/'.join(path)}", - params=params).prepare() - - -def api2_post(body, *path, **params): - return Request('POST', f"{API2_ENDPOINT}{'/'.join(path)}", - data=body, params=params).prepare() - - -def send(prepped_request): - with Session() as session: - response = session.send(prepped_request, timeout=TIMEOUT) - try: - json_response = response.json() - except (ValueError, KeyError): - raise BadResponse - else: - if 'error' in json_response: - raise APIError(json_response) - else: - return json_response - - -def unpaid_balance(address): - result = api_call('stats.provider', { 'addr': address }) - balances = result['stats'] - return sum([float(b['balance']) for b in balances]) +HOST = 'https://api2.nicehash.com' def simplemultialgo_info(nx_settings): - response = send(api2_get('public', 'simplemultialgo', 'info')) + api = nh.public_api(HOST) + response = api.get_multialgo_info() pay_factor = 1e-9 # GH -> H/s/day return {algorithm['algorithm'].lower(): float(algorithm['paying'])*pay_factor for algorithm in response['miningAlgorithms']} def stratums(nx_settings): - response = send(api2_get('mining', 'algorithms')) + api = nh.public_api(HOST) + response = api.get_algorithms() ports = {algorithm['algorithm'].lower(): algorithm['port'] for algorithm in response['miningAlgorithms']} region = nx_settings['nicehash']['region'] diff --git a/tests/test_nicehash.py b/tests/test_nicehash.py index fa6394a..0f36683 100644 --- a/tests/test_nicehash.py +++ b/tests/test_nicehash.py @@ -4,18 +4,6 @@ from nuxhash.settings import DEFAULT_SETTINGS -class TestNHApi(TestCase): - - def test_get_request(self): - response = nh.send(nh.api2_get('exchangeRate', 'list')) - self.assertIn('list', response) - - exchange = response['list'][0] - self.assertIn('exchangeRate', exchange) - self.assertIn('toCurrency', exchange) - self.assertIn('fromCurrency', exchange) - - class TestNHMultialgo(TestCase): def setUp(self):