GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Client.verify_invoice_params()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
1
from bitpay.exceptions import *
2
from bitpay import key_utils
3
import requests
4
import json
5
import re
6
7
class Client:
8
  def __init__(self, api_uri="https://bitpay.com", insecure=False, pem=key_utils.generate_pem(), tokens={}):
9
    self.uri = api_uri
10
    self.verify = not(insecure)
11
    self.pem = pem
12
    self.client_id = key_utils.get_sin_from_pem(pem)
13
    self.tokens = tokens
14
    self.user_agent = 'bitpay-python'
15
16
  def pair_pos_client(self, code):
17
    if re.match("^\w{7,7}$", code) is None:
18
      raise BitPayArgumentError("pairing code is not legal")
19
    payload = {'id': self.client_id, 'pairingCode': code}
20
    response = self.unsigned_request('/tokens', payload)
21
    if response.ok:
22
      self.tokens = self.token_from_response(response.json())
23
      return self.tokens 
24
    self.response_error(response)
25
26
  def create_token(self, facade):
27
    payload = {'id': self.client_id, 'facade': facade}
28
    response = self.unsigned_request('/tokens', payload)
29
    if response.ok:
30
      self.tokens = self.token_from_response(response.json())
31
      return response.json()['data'][0]['pairingCode']
32
    self.response_error(response)
33
34
  def create_invoice(self, params):
35
    self.verify_invoice_params(params['price'], params['currency'])
36
    payload = json.dumps(params)
37
    uri = self.uri + "/invoices"
38
    xidentity = key_utils.get_compressed_public_key_from_pem(self.pem)
39
    xsignature = key_utils.sign(uri + payload, self.pem)
40
    headers = {"content-type": "application/json", 'accept': 'application/json', 'X-Identity': xidentity, 'X-Signature': xsignature, 'X-accept-version': '2.0.0'}
41
    try:
42
      response = requests.post(uri, data=payload, headers=headers, verify=self.verify)
43
    except Exception as pro:
44
      raise BitPayConnectionError(pro.args)
45
    if response.ok:
46
      return response.json()['data']
47
    self.response_error(response)
48
49
  def get_invoice(self, invoice_id): 
50
    uri = self.uri + "/invoices/" + invoice_id
51
    try:
52
      response = requests.get(uri, verify=self.verify)
53
    except Exception as pro:
54
      raise BitPayConnectionError(pro.args)
55
    if response.ok:
56
      return response.json()['data']
57
    self.response_error(response)
58
59
  def verify_tokens(self):
60
    """
61
    Deprecated, will be made private in 2.4
62
    """
63
    xidentity = key_utils.get_compressed_public_key_from_pem(self.pem)
64
    url = self.uri + "/tokens"
65
    xsignature = key_utils.sign(self.uri + "/tokens", self.pem)
66
    headers = {"content-type": "application/json", 'accept': 'application/json', 'X-Identity': xidentity, 'X-Signature': xsignature, 'X-accept-version': '2.0.0'}
67
    response = requests.get(self.uri + "/tokens", headers=headers, verify=self.verify)
68
    if response.ok:
69
      allTokens = response.json()['data']
70
      selfKeys = self.tokens.keys() 
71
      matchedTokens = [token for token in allTokens for key in selfKeys if token.get(key) == self.tokens.get(key)]
72
      if not matchedTokens:
73
        return False
74
      return True
75
76
  def token_from_response(self, responseJson):
77
    """
78
    Deprecated, will be made private in 2.4
79
    """
80
    token = responseJson['data'][0]['token']
81
    facade = responseJson['data'][0]['facade']
82
    return {facade: token}
83
    raise BitPayBitPayError('%(code)d: %(message)s' % {'code': response.status_code, 'message': response.json()['error']})
84
85
  def verify_invoice_params(self, price, currency):
86
    """
87
    Deprecated, will be made private in 2.4
88
    """
89
    if re.match("^[A-Z]{3,3}$", currency) is None:
90
      raise BitPayArgumentError("Currency is invalid.")
91
    try: 
92
      float(price)
93
    except:
94
      raise BitPayArgumentError("Price must be formatted as a float")
95
  def response_error(self, response):
96
    raise BitPayBitPayError('%(code)d: %(message)s' % {'code': response.status_code, 'message': response.json()['error']})
97
98
  def unsigned_request(self, path, payload=None):
99
    """
100
    generic bitpay usigned wrapper
101
    passing a payload will do a POST, otherwise a GET
102
    """
103
    headers = {"content-type": "application/json", "accept": "application/json", "X-accept-version": "2.0.0"}
104
    try:
105
      if payload:
106
        response = requests.post(self.uri + path, verify=self.verify, data=json.dumps(payload), headers=headers)
107
      else:
108
        response = requests.get(self.uri + path, verify=self.verify, headers=headers)
109
    except Exception as pro:
110
      raise BitPayConnectionError('Connection refused')
111
    return response
112
113
  def unsigned_get_request(self, path, payload=None):
114
    """
115
    Deprecated, will be removed in 2.4
116
    """
117
    return self.unsigned_request('/tokens', payload)
118