This repository was archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathhsformat.py
More file actions
164 lines (143 loc) · 5.89 KB
/
Copy pathhsformat.py
File metadata and controls
164 lines (143 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#
# The MIT License (MIT)
#
# Copyright (C) 2014 hellosign.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
import json
class HSFormat(object):
''' Authentication object using HelloSign's access token '''
def __init__(self):
''' Initialziation of the object
Args:
'''
@classmethod
def fakef(self):
return 'Hello'
@staticmethod
def format_file_params(files):
'''
Utility method for formatting file parameters for transmission
'''
files_payload = {}
if files:
for idx, filename in enumerate(files):
files_payload["file[" + str(idx) + "]"] = open(filename, 'rb')
return files_payload
@staticmethod
def format_file_url_params(file_urls):
'''
Utility method for formatting file URL parameters for transmission
'''
file_urls_payload = {}
if file_urls:
for idx, fileurl in enumerate(file_urls):
file_urls_payload["file_url[" + str(idx) + "]"] = fileurl
return file_urls_payload
@staticmethod
def format_logo_params(logo):
'''
Utility method for formatting custom logo parameters for API Apps
'''
logo_payload = {}
logo_payload["custom_logo_file"] = open(logo, 'rb')
return logo_payload
@staticmethod
def format_param_list(listed_params, output_name):
'''
Utility method for formatting lists of parameters for api consumption
Useful for email address lists, etc
Args:
listed_params (list of values) - the list to format
output_name (str) - the parameter name to prepend to each key
'''
output_payload = {}
if listed_params:
for index, item in enumerate(listed_params):
output_payload[str(output_name) + "[" + str(index) + "]" ] = item
return output_payload
@staticmethod
def format_dict_list(list_of_dicts, output_name, key=None):
'''
Utility method for formatting lists of dictionaries for api consumption.
Takes something like [{name: val1, email: val2},{name: val1, email: val2}] for signers
and outputs:
signers[0][name] : val1
signers[0][email] : val2
...
Args:
list_of_dicts (list of dicts) - the list to format
output_name (str) - the parameter name to prepend to each key
key (str, optional) - Used for substituting a key present in the dictionaries for the index. The above might become signers['Lawyer']['name'] instead of using a numerical index if the key "role_name" was specified.
'''
output_payload = {}
if list_of_dicts:
for index, dictionary in enumerate(list_of_dicts):
index_or_key = dictionary[key] if key else str(index)
base_name = output_name + '[' + index_or_key + ']'
for (param, value) in dictionary.items():
if param != key: #key params are stripped
output_payload[base_name + '[' + param + ']'] = value
return output_payload
@staticmethod
def format_single_dict(dictionary, output_name):
'''
Currently used for metadata fields
'''
output_payload = {}
if dictionary:
for (k, v) in dictionary.items():
output_payload[output_name + '[' + k + ']'] = v
return output_payload
@staticmethod
def format_custom_fields(list_of_custom_fields):
'''
Custom fields formatting for submission
'''
output_payload = {}
if list_of_custom_fields:
# custom_field: {"name": value}
for custom_field in list_of_custom_fields:
for key, value in custom_field.items():
output_payload["custom_fields[" + key + "]"] = value
return output_payload
@staticmethod
def format_signing_options(listed_params, output_name):
'''
Utility method for formatting signing options for api consumption
Args:
listed_params (list of values) - the list to format
'''
if not listed_params:
return {}
return {output_name: json.dumps(listed_params)}
@staticmethod
def strip_none_values(dictionary):
if dictionary:
return dict((key, value) for (key, value) in dictionary.items() if value)
@staticmethod
def format_json_data(options):
'''
Utility method to convert to JSON data were required. Several of the signing parameters require JSON data
Args:
options (list of options) - the list of options passed to convert to JSON data
'''
json_data = json.dumps(options).replace('null', '')
return json_data