mss-python 0.1.1__py3-none-any.whl → 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mss_python/client.py ADDED
@@ -0,0 +1,111 @@
1
+ import requests
2
+ import xmltodict
3
+ import xml.etree.ElementTree as ET
4
+
5
+ from .request.methods import Method
6
+ from .request.types_mss import Root, BaseType, Credentials, Search, MethodName, Literal
7
+
8
+
9
+ # maybe recursion should be used instead of doing if checks?
10
+ def change_structure(root: Root, child: BaseType):
11
+ if child.tag == "root":
12
+ return child
13
+
14
+ if child.tag == "request":
15
+ root.request = child
16
+
17
+ elif child.tag == "search":
18
+ root.request.search = child
19
+
20
+ elif child.tag == "order":
21
+ root.request.order = child
22
+
23
+ elif child.tag == "options":
24
+ root.request.options = child
25
+
26
+ elif child.tag == "logging":
27
+ root.request.logging = child
28
+
29
+ elif child.tag == "header":
30
+ root.header = child
31
+
32
+ return root
33
+
34
+
35
+ class Client:
36
+ def __init__(
37
+ self, cred: Credentials, lang: list, elements: list | None = None
38
+ ) -> None:
39
+ self.cred = cred
40
+ self.lang = Search(lang)
41
+
42
+ def request(
43
+ self,
44
+ url: str,
45
+ method_name: MethodName,
46
+ data: BaseType | None = None,
47
+ _print: Literal[True, False] = False,
48
+ ) -> dict:
49
+ """
50
+ Makes a post request to the given URL with the specified data and method name.
51
+
52
+ data: ElementTree with the data required in the XML
53
+ url: Service URL
54
+ method: Method to be executed by the service
55
+ """
56
+
57
+ method = Method(method_name)
58
+
59
+ root = method.get_base_xml(self.cred, self.lang)
60
+
61
+ if data is None:
62
+ root = change_structure(root, data)
63
+
64
+ if _print is True:
65
+ print(ET.tostring(root.request.to_xml(), encoding="unicode"))
66
+
67
+ response = requests.post(
68
+ url,
69
+ data=ET.tostring(root.to_xml(), encoding="unicode"),
70
+ headers={"Content-Type": "application/xml"},
71
+ )
72
+
73
+ responseRoot = xmltodict.parse(response.content)
74
+ error = responseRoot["root"]["header"]["error"]
75
+ if int(error["code"]) > 0:
76
+ raise Exception(
77
+ f'MSS returned an error: code {error["code"]}, message: "{error["message"]}"'
78
+ )
79
+
80
+ return refine_response(responseRoot["root"], method_name)
81
+
82
+
83
+ def refine_response(resp: dict, meth: MethodName) -> dict:
84
+ if meth == MethodName.GetHotelList:
85
+ return refine_getHotelList(resp)
86
+
87
+ return resp
88
+
89
+
90
+ def refine_getHotelList(resp: dict) -> dict:
91
+ if resp["result"] is None:
92
+ return resp
93
+ elif "hotel" not in resp["result"] or resp["result"]["hotel"] is None:
94
+ return resp
95
+ elif resp["result"]["hotel"] is list:
96
+ resp["result"]["hotel"] = [resp["result"]["hotel"]]
97
+
98
+ for hotel in resp["result"]["hotel"]:
99
+ ensure_list_value(hotel, "pictures", "picture")
100
+ ensure_list_value(hotel, "gallery", "picture")
101
+ ensure_list_value(hotel, "feature_view", "feature")
102
+ ensure_list_value(hotel, "pos", "id_pos")
103
+
104
+ return resp
105
+
106
+
107
+ def ensure_list_value(hotel: dict, parent_fld: str, child_fld: str) -> None:
108
+ if parent_fld not in hotel:
109
+ hotel[parent_fld] = {child_fld: []}
110
+ elif hotel[parent_fld][child_fld] is list:
111
+ hotel[parent_fld][child_fld] = [hotel[parent_fld][child_fld]]
File without changes
@@ -1,6 +1,4 @@
1
- from __future__ import annotations
2
- import xml.etree.ElementTree as ET
3
- from request.types_mss import * # might need change
1
+ from enum import Enum
4
2
 
5
3
 
6
4
  class MethodName(Enum):
@@ -28,22 +26,3 @@ class MethodName(Enum):
28
26
  GetHotelPictureGroups = "getHotelPictureGroups"
29
27
  GetOptionalServices = "getOptionalServices"
30
28
  ValidateCoupon = "validateCoupon"
31
-
32
-
33
- class Method:
34
- def __init__(self, name: MethodName):
35
- self.name = name
36
-
37
- def get_base_xml(self, cred: Credentials, lang: Search) -> Root:
38
- """
39
- Returns an XML (ElementTree) with the mandatory elements needed for the request.
40
-
41
- cred: XML Credentials Element (username, password, source)
42
-
43
- lang: XML Search Element with the language element defined
44
- """
45
-
46
- header = Header(cred, self.name)
47
- req = Request(lang)
48
- root = Root(header, req)
49
- return root
@@ -0,0 +1,23 @@
1
+ from __future__ import annotations
2
+
3
+ from .method_names import MethodName
4
+ from .types_mss import Credentials, Search, Root, Header, Request
5
+
6
+
7
+ class Method:
8
+ def __init__(self, name: MethodName):
9
+ self.name = name
10
+
11
+ def get_base_xml(self, cred: Credentials, lang: Search) -> Root:
12
+ """
13
+ Returns an XML (ElementTree) with the mandatory elements needed for the request.
14
+
15
+ cred: XML Credentials Element (username, password, source)
16
+
17
+ lang: XML Search Element with the language element defined
18
+ """
19
+
20
+ header = Header(cred, self.name)
21
+ req = Request(lang)
22
+ root = Root(header, req)
23
+ return root
@@ -4,7 +4,8 @@ from enum import Enum, IntFlag
4
4
  from typing import Literal
5
5
  import xml.etree.ElementTree as ET
6
6
  from abc import ABC
7
- from methods import MethodName
7
+
8
+ from .method_names import MethodName
8
9
 
9
10
 
10
11
  def get_child_xml(value, field, root):
@@ -35,8 +36,8 @@ class BaseType(ABC):
35
36
  root = ET.Element(self.tag)
36
37
  # root.append(self.search.to_xml())
37
38
 
38
- for field, value in vars(self).items():
39
- get_child_xml(value, field, root)
39
+ for field_, value in vars(self).items():
40
+ get_child_xml(value, field_, root)
40
41
 
41
42
  return root
42
43
 
@@ -624,7 +625,7 @@ class SpecialTheme(Enum):
624
625
  ThemeIDBicycleTouring = 86
625
626
  ThemeIDEBike = 87
626
627
 
627
- ##These values were theme_bit
628
+ ## These values were theme_bit
628
629
  # Hiking = 1
629
630
  # Cycling = 2
630
631
  # Family = 4
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mss-python
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Summary: MSS API wrapper for Python projects
5
5
  Home-page: https://github.com/bookingsuedtirol/mss-python
6
6
  Requires-Python: >=3.7, <4
@@ -0,0 +1,11 @@
1
+ mss_python/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ mss_python/client.py,sha256=DEMAGATE08MW42Ldtepk95RV6funmyyuFbN9XFhiwPk,3202
3
+ mss_python/request/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ mss_python/request/method_names.py,sha256=DP22Tra-F86FP-6771q7bDwuX6ojPCidFPzztl4UThI,1030
5
+ mss_python/request/methods.py,sha256=pu_U6z8ANktuv0r3P6MmLKW66fwRhiDCp261vNxqrYE,664
6
+ mss_python/request/types_mss.py,sha256=Xq9YYA5-f_j2Gt7bCcHSC6qEzHs0hswpoXm2IGB9SdA,22354
7
+ mss_python-0.1.3.dist-info/LICENSE,sha256=SxLvAIuAvi2G5v6pRCopG3gAH9SLL_-xACJ0QBz_Blw,1074
8
+ mss_python-0.1.3.dist-info/METADATA,sha256=QHC1KmbqxFdqD8QY-9HHEkc692274XYUHh-AjhUQ1X0,1642
9
+ mss_python-0.1.3.dist-info/WHEEL,sha256=Mdi9PDNwEZptOjTlUcAth7XJDFtKrHYaQMPulZeBCiQ,91
10
+ mss_python-0.1.3.dist-info/top_level.txt,sha256=-5Wm4NpowVgq9ImH61vhAiBQFoE21fg4r-7Pn7mOYyE,11
11
+ mss_python-0.1.3.dist-info/RECORD,,
@@ -0,0 +1 @@
1
+ mss_python
@@ -1,8 +0,0 @@
1
- request/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- request/methods.py,sha256=ZJfqxUTxGbWwos4eIswntJflZU-ESsMsk1esbzuMOp0,1656
3
- request/types_mss.py,sha256=fDz9sAkjm3VRtVOuE-zZVBkVQXg_UW5mG1MT5kYVtNA,22344
4
- mss_python-0.1.1.dist-info/LICENSE,sha256=SxLvAIuAvi2G5v6pRCopG3gAH9SLL_-xACJ0QBz_Blw,1074
5
- mss_python-0.1.1.dist-info/METADATA,sha256=uN3QRyZLQ0eIDSijzLmVF2R6JKkZO8WA1Y96YVufvAY,1642
6
- mss_python-0.1.1.dist-info/WHEEL,sha256=Mdi9PDNwEZptOjTlUcAth7XJDFtKrHYaQMPulZeBCiQ,91
7
- mss_python-0.1.1.dist-info/top_level.txt,sha256=JDL91wFJSRflnNxOORJn3CuoEnpFVsBerOlmjdGdTEk,8
8
- mss_python-0.1.1.dist-info/RECORD,,
@@ -1 +0,0 @@
1
- request
File without changes