-
Notifications
You must be signed in to change notification settings - Fork 131
/
request.py
288 lines (245 loc) · 8.06 KB
/
request.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from coapthon import defines
from coapthon.messages.message import Message
from coapthon.messages.option import Option
__author__ = 'Giacomo Tanganelli'
class Request(Message):
"""
Class to handle the Requests.
"""
def __init__(self):
"""
Initialize a Request message.
"""
super(Request, self).__init__()
@property
def uri_path(self):
"""
Return the Uri-Path of a request
:rtype : String
:return: the Uri-Path
"""
value = []
for option in self.options:
if option.number == defines.OptionRegistry.URI_PATH.number:
value.append(str(option.value) + '/')
value = "".join(value)
value = value[:-1]
return value
@uri_path.setter
def uri_path(self, path):
"""
Set the Uri-Path of a request.
:param path: the Uri-Path
"""
path = path.strip("/")
tmp = path.split("?")
path = tmp[0]
paths = path.split("/")
for p in paths:
option = Option()
option.number = defines.OptionRegistry.URI_PATH.number
option.value = p
self.add_option(option)
if len(tmp) > 1:
query = tmp[1]
self.uri_query = query
@uri_path.deleter
def uri_path(self):
"""
Delete the Uri-Path of a request.
"""
self.del_option_by_number(defines.OptionRegistry.URI_PATH.number)
@property
def uri_query(self):
"""
Get the Uri-Query of a request.
:return: the Uri-Query
:rtype : String
:return: the Uri-Query string
"""
value = []
for option in self.options:
if option.number == defines.OptionRegistry.URI_QUERY.number:
value.append(str(option.value))
return "&".join(value)
@uri_query.setter
def uri_query(self, value):
"""
Adds a query.
:param value: the query
"""
del self.uri_query
queries = value.split("&")
for q in queries:
option = Option()
option.number = defines.OptionRegistry.URI_QUERY.number
option.value = str(q)
self.add_option(option)
@uri_query.deleter
def uri_query(self):
"""
Delete a query.
"""
self.del_option_by_number(defines.OptionRegistry.URI_QUERY.number)
@property
def accept(self):
"""
Get the Accept option of a request.
:return: the Accept value or None if not specified by the request
:rtype : String
"""
for option in self.options:
if option.number == defines.OptionRegistry.ACCEPT.number:
return option.value
return None
@accept.setter
def accept(self, value):
"""
Add an Accept option to a request.
:param value: the Accept value
"""
if value in defines.Content_types.values():
option = Option()
option.number = defines.OptionRegistry.ACCEPT.number
option.value = value
self.add_option(option)
@accept.deleter
def accept(self):
"""
Delete the Accept options of a request.
"""
self.del_option_by_number(defines.OptionRegistry.ACCEPT.number)
@property
def if_match(self):
"""
Get the If-Match option of a request.
:return: the If-Match values or [] if not specified by the request
:rtype : list
"""
value = []
for option in self.options:
if option.number == defines.OptionRegistry.IF_MATCH.number:
value.append(option.value)
return value
@if_match.setter
def if_match(self, values):
"""
Set the If-Match option of a request.
:param values: the If-Match values
:type values : list
"""
assert isinstance(values, list)
for v in values:
option = Option()
option.number = defines.OptionRegistry.IF_MATCH.number
option.value = v
self.add_option(option)
@if_match.deleter
def if_match(self):
"""
Delete the If-Match options of a request.
"""
self.del_option_by_number(defines.OptionRegistry.IF_MATCH.number)
@property
def if_none_match(self):
"""
Get the if-none-match option of a request.
:return: True, if if-none-match is present
:rtype : bool
"""
for option in self.options:
if option.number == defines.OptionRegistry.IF_NONE_MATCH.number:
return True
return False
def add_if_none_match(self):
"""
Add the if-none-match option to the request.
"""
option = Option()
option.number = defines.OptionRegistry.IF_NONE_MATCH.number
option.value = None
self.add_option(option)
@if_none_match.deleter
def if_none_match(self):
"""
Delete the if-none-match option in the request.
"""
self.del_option_by_number(defines.OptionRegistry.IF_NONE_MATCH.number)
@property
def proxy_uri(self):
"""
Get the Proxy-Uri option of a request.
:return: the Proxy-Uri values or None if not specified by the request
:rtype : String
"""
for option in self.options:
if option.number == defines.OptionRegistry.PROXY_URI.number:
return option.value
return None
@proxy_uri.setter
def proxy_uri(self, value):
"""
Set the Proxy-Uri option of a request.
:param value: the Proxy-Uri value
"""
option = Option()
option.number = defines.OptionRegistry.PROXY_URI.number
option.value = str(value)
self.add_option(option)
@proxy_uri.deleter
def proxy_uri(self):
"""
Delete the Proxy-Uri option of a request.
"""
self.del_option_by_number(defines.OptionRegistry.PROXY_URI.number)
@property
def proxy_schema(self):
"""
Get the Proxy-Schema option of a request.
:return: the Proxy-Schema values or None if not specified by the request
:rtype : String
"""
for option in self.options:
if option.number == defines.OptionRegistry.PROXY_SCHEME.number:
return option.value
return None
@proxy_schema.setter
def proxy_schema(self, value):
"""
Set the Proxy-Schema option of a request.
:param value: the Proxy-Schema value
"""
option = Option()
option.number = defines.OptionRegistry.PROXY_SCHEME.number
option.value = str(value)
self.add_option(option)
@proxy_schema.deleter
def proxy_schema(self):
"""
Delete the Proxy-Schema option of a request.
"""
self.del_option_by_number(defines.OptionRegistry.PROXY_SCHEME.number)
@property
def uri_query(self):
"""
Get the uri_query option of a request.
:return: the uri_query values or None if not specified by the request
:rtype : String
"""
for option in self.options:
if option.number == defines.OptionRegistry.URI_QUERY.number:
return option.value
return None
@uri_query.setter
def uri_query(self, value):
"""
Set the uri_query option of a request.
:param value: the uri_query value
"""
option = Option()
option.number = defines.OptionRegistry.URI_QUERY.number
option.value = str(value)
self.add_option(option)
@uri_query.deleter
def uri_query(self):
self.del_option_by_number(defines.OptionRegistry.URI_QUERY.number)