-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
55 lines (43 loc) · 2.22 KB
/
test_client.py
File metadata and controls
55 lines (43 loc) · 2.22 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
#!/usr/bin/env python3
from time import sleep
import requests
from configuration import *
from xsdata.formats.dataclass.serializers import XmlSerializer
from xsdata.formats.dataclass.serializers.config import SerializerConfig
from test_create_ojp_request import test_create_ojp_trip_request_simple_3
from map_ojp_to_ojp import map_ojp_trip_result_to_ojp_fare_request, parse_ojp
ns_map = {'': 'http://www.siri.org.uk/siri', 'ojp': 'http://www.vdv.de/ojp'}
if __name__ == '__main__':
for i in range(600):
try:
ojp_trip_request = test_create_ojp_trip_request_simple_3()
serializer_config = SerializerConfig(ignore_default_attributes=True, pretty_print=True)
serializer = XmlSerializer(config=serializer_config)
if (HTTPS):
if HTTP_PORT == '':
url = f"https://{HTTP_HOST}/{HTTP_SLUG}"
else:
url = f"https://{HTTP_HOST}:{HTTP_PORT}/{HTTP_SLUG}"
else:
url = f"http://{HTTP_HOST}:{HTTP_PORT}/{HTTP_SLUG}"
access_token = OJP_FARE_TOKEN
headers = {'Authorization': 'Bearer ' + access_token, 'Content-Type': 'application/xml; charset=utf-8'}
ojp_trip_request_xml = serializer.render(ojp_trip_request, ns_map=ns_map)
r = requests.post(url, data=ojp_trip_request_xml.encode("utf-8"), headers=headers, verify=False)
r.encoding = r.apparent_encoding
print(r.text)
sleep(1)
ojp_trip_result = parse_ojp(r.text)
ojp_fare_request = map_ojp_trip_result_to_ojp_fare_request(ojp_trip_result)
ojp_fare_request_xml = serializer.render(ojp_fare_request, ns_map=ns_map)
r = requests.post(url, data=ojp_fare_request_xml.encode("utf-8"), headers=headers, verify=False)
r.encoding = r.apparent_encoding
print(r.text)
except Exception as e:
print(str(e))
# faulty file sent to OJPFare must be handled gracefully
print("Testing faulty OJPFare request")
ojp_fare_request_xml = "<xml></xml>"
r = requests.post(url, data=ojp_fare_request_xml.encode("utf-8"), headers=headers, verify=False)
r.encoding = r.apparent_encoding
print(r.text)