Skip to content

Commit 82d5abe

Browse files
Juliana MashonJuliana Mashon
Juliana Mashon
authored and
Juliana Mashon
committed
Updated function return configuration
1 parent eedce48 commit 82d5abe

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

api_functions.py

+23-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
13
from api_connect import ApiConnect
24

35
class ApiFunctions():
@@ -7,50 +9,53 @@ def __init__(self):
79
self.echo = True
810
self.verbose = True
911

10-
def __return_config(self, return_value, json_val=False): # TODO: which functions return json()
12+
def __return_config(self, funct_name, return_value, is_json=False):
1113
"""Configure echo and verbosity of function returns."""
1214

13-
if self.echo is True and self.verbose is True:
15+
if self.echo is True:
1416
print('-' * 100)
15-
if json_val is True:
16-
print(f'FUNCTION: {return_value}\n')
17-
return print(return_value)
17+
print(f'FUNCTION: {funct_name}\n')
18+
if is_json is True:
19+
readable_json = json.dumps(return_value, indent=4)
20+
print(readable_json)
1821
else:
19-
print(f'FUNCTION: {return_value}\n')
20-
return print(return_value)
21-
elif self.echo is True and self.verbose is False:
22-
print('-' * 100)
23-
return print(return_value)
24-
elif self.echo is False and self.verbose is False:
25-
return return_value
22+
print(return_value)
23+
elif self.echo is False:
24+
pass
2625
else:
2726
print('-' * 100)
2827
return print("ERROR: Incompatible return configuration.")
2928

29+
return return_value
30+
3031
def get_token(self, email, password, server='https://my.farm.bot'):
3132
token_str = self.api_connect.get_token(email, password, server)
32-
return token_str
33+
return self.__return_config("get_token()", token_str, is_json=True)
3334

3435
# data = get_info() and like functions will assign 'data' JSON object
3536
# data["name"] will access the field "name" and return the field value
3637

3738
def get_info(self, endpoint, id=None):
38-
return self.api_connect.get(endpoint, id)
39+
endpoint_data = self.api_connect.get(endpoint, id)
40+
return self.__return_config("get_info()", endpoint_data, is_json=True)
3941

4042
def set_info(self, endpoint, field, value, id=None):
4143
new_value = {
4244
field: value
4345
}
4446

4547
self.api_connect.patch(endpoint, id, new_value)
46-
return self.api_connect.get(endpoint, id)
48+
49+
endpoint_data = self.api_connect.get(endpoint, id)
50+
return self.__return_config("set_info()", endpoint_data, is_json=True)
4751

4852
def env(self, id=None, field=None, new_val=None): # TODO: Fix
4953
if id is None:
50-
data = self.api_connect.get('farmware_envs', id=None)
54+
env_data = self.api_connect.get('farmware_envs', id=None)
5155
else:
52-
data = self.api_connect.get('farmware_envs', id)
53-
# return ...
56+
env_data = self.api_connect.get('farmware_envs', id)
57+
58+
return self.__return_config("env()", env_data, is_json=True)
5459

5560
def log(self, message, type=None, channel=None):
5661
log_message = {

0 commit comments

Comments
 (0)