-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAPIController.py
More file actions
131 lines (97 loc) · 4.15 KB
/
APIController.py
File metadata and controls
131 lines (97 loc) · 4.15 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
# -*- coding: utf-8 -*-
"""
FlowrouteMessagingLib.Controllers.APIController
Copyright Flowroute, Inc. 2016
"""
import requests
from requests.auth import HTTPBasicAuth
from FlowrouteMessagingLib.APIHelper import APIHelper
from FlowrouteMessagingLib.Configuration import Configuration
from FlowrouteMessagingLib.APIException import APIException
class APIController(object):
"""
A Controller to access Endpoints in the FlowrouteMessagingLib API.
Args:
username (str): Username for authentication
password (str): password for authentication
"""
def __init__(self, username, password):
self.__username = username
self.__password = password
def create_message(self, message):
"""Does a POST request to /messages.
Send a message.
Args:
message (Message): Message Object to send.
Returns:
dict: Response from the API.
Raises:
APIException: When an error occurs while fetching the data from
the remote API. This exception includes the HTTP Response
code, an error message, and the HTTP body that was received in
the request.
"""
# The base uri for api requests
query_builder = Configuration.BASE_URI
# Prepare query string for API call
query_builder += "/messages"
# Validate and preprocess url
_query_url = APIHelper.clean_url(query_builder)
# Prepare headers
_headers = {
'accept': 'application/vnd.api+json',
'content-type': 'application/vnd.api+json; charset=utf-8'
}
# Prepare and execute request
response = requests.post(_query_url,
headers=_headers,
auth=HTTPBasicAuth(self.__username, self.__password),
data=APIHelper.json_serialize(message))
json_content = APIHelper.json_deserialize(response.content)
print("Resp: {}".format(response.json()))
# Error handling using HTTP status codes
if response.status_code == 401:
raise APIException("UNAUTHORIZED", 401, json_content)
elif response.status_code == 403:
raise APIException("FORBIDDEN", 403, json_content)
elif response.status_code < 200 or response.status_code > 206: # 200 = HTTP OK
raise APIException("HTTP Response Not OK", response.status_code,
json_content)
return json_content
def get_message_lookup(self, record_id):
"""Does a GET request to /messages/{record_id}.
Lookup a Message by MDR.
Args:
record_id (str): Unique MDR ID
Returns:
dict: Response from the API.
Raises:
APIException:
When an error occurs while fetching the data from
the remote API. This exception includes the HTTP Response
code, an error message, and the HTTP body that was received in
the request.
"""
# The base uri for api requests
query_builder = Configuration.BASE_URI
# Prepare query string for API call
query_builder += "/messages/{record_id}"
# Process optional template parameters
query_builder = APIHelper.append_url_with_template_parameters(
query_builder, {
"record_id": record_id,
})
# Validate and preprocess url
query_url = APIHelper.clean_url(query_builder)
# Prepare headers
headers = {"user-agent": "Flowroute Messaging SDK 1.0", }
# Prepare and invoke the API call request to fetch the response
response = requests.get(
url=query_url,
auth=(self.__username, self.__password))
json_content = APIHelper.json_deserialize(response.content)
# Error handling using HTTP status codes
if response.status_code < 200 or response.status_code > 206: # 200 = HTTP OK
raise APIException("HTTP Response Not OK", response.status_code,
json_content)
return json_content