Skip to content

Commit f64e8ef

Browse files
SP-656 Python SDK - Should work with PrivateKeyPath
1 parent 7fcafec commit f64e8ef

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/bitpay/client.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ def create_pos_client(self, pos_token: str, environment: Environment = Environme
8181
return Client(bitpay_client, token_container, GuidGenerator())
8282

8383
@staticmethod
84-
def create_client_by_private_key( # type: ignore
85-
private_key: str,
84+
def create_client( # type: ignore
85+
private_key_or_private_key_path: str,
8686
token_container: TokenContainer,
8787
environment: Environment = Environment.PROD,
8888
proxy: Optional[str] = None,
8989
):
9090
try:
9191
base_url = Client.get_base_url(environment)
92-
ec_key = Client.get_ec_key(private_key)
92+
ec_key = Client.get_ec_key(private_key_or_private_key_path)
9393
bitpay_client = BitPayClient(base_url, ec_key, proxy)
9494
guid_generator = GuidGenerator()
9595

@@ -103,42 +103,44 @@ def create_client_by_config_file_path(config_file_path: str): # type: ignore
103103
raise BitPayException("Configuration file not found")
104104

105105
try:
106-
read_file = open(config_file_path, "r")
107-
json_data = json.loads(read_file.read())
106+
with open(config_file_path, "r") as read_file:
107+
json_data = json.loads(read_file.read())
108108

109109
environment = json_data["BitPayConfiguration"]["Environment"]
110110
config = json_data["BitPayConfiguration"]["EnvConfig"][environment]
111111
proxy = config["proxy"]
112-
ec_key = config["PrivateKey"]
112+
private_key_path = config["PrivateKeyPath"]
113+
if private_key_path is None:
114+
private_key_or_private_key_path = config["PrivateKey"]
115+
else:
116+
private_key_or_private_key_path = private_key_path
117+
113118
token_container = TokenContainer()
114119
token_container.add_pos(config["ApiTokens"].get("pos", None))
115120
token_container.add_merchant(config["ApiTokens"].get("merchant", None))
116121
token_container.add_payout(config["ApiTokens"].get("payout", None))
117-
read_file.close()
118122

119123
if "test" == environment.lower():
120124
environment = Environment.TEST
121125
else:
122126
environment = Environment.PROD
123127

124-
return Client.create_client_by_private_key(
125-
ec_key, token_container, environment, proxy
128+
return Client.create_client(
129+
private_key_or_private_key_path, token_container, environment, proxy
126130
)
127131
except Exception as exe:
128132
raise BitPayException("Error when reading configuration file. " + str(exe))
129133

130134
@staticmethod
131-
def get_ec_key(private_key: Optional[str]) -> str:
132-
if private_key is None:
135+
def get_ec_key(private_key_or_private_key_path: Optional[str]) -> str:
136+
if private_key_or_private_key_path is None:
133137
raise BitPayException("Private Key file not found")
134138

135-
if os.path.exists(private_key):
136-
read_file = open(private_key, "r")
137-
plain_private_key = read_file.read()
138-
read_file.close()
139-
return plain_private_key
139+
if os.path.exists(private_key_or_private_key_path):
140+
with open(private_key_or_private_key_path, "r") as read_file:
141+
return read_file.read()
140142

141-
return private_key
143+
return private_key_or_private_key_path
142144

143145
@staticmethod
144146
def get_base_url(environment: Environment) -> str:

0 commit comments

Comments
 (0)