-
Notifications
You must be signed in to change notification settings - Fork 11
/
soap_service.dart
89 lines (78 loc) · 3.08 KB
/
soap_service.dart
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
// Package imports:
import 'package:http/http.dart' as http;
import 'package:xml/xml.dart';
// Project imports:
import 'package:notredame/constants/urls.dart';
import 'package:notredame/features/app/signets-api/models/signets_errors.dart';
import 'package:notredame/utils/api_exception.dart';
mixin SoapService {
static const String tag = "SoapService";
static const String tagError = "$tag - Error";
/// Build the default body for communicate with the SignetsAPI.
/// [firstElementName] should be the SOAP operation of the request.
static XmlBuilder buildBasicSOAPBody(
String firstElementName, String username, String password) {
final builder = XmlBuilder();
builder.processing('xml', 'version="1.0" encoding="utf-8"');
builder.element("soap:Envelope", namespaces: {
"http://www.w3.org/2001/XMLSchema-instance": "xsi",
"http://www.w3.org/2001/XMLSchema": "xsd",
"http://schemas.xmlsoap.org/soap/envelope/": "soap"
}, nest: () {
builder.element("soap:Body", nest: () {
// Details of the envelope
builder.element(firstElementName, nest: () {
builder.namespace(Urls.signetsOperationBase);
builder.element("codeAccesUniversel", nest: username);
builder.element("motPasse", nest: password);
});
});
});
return builder;
}
/// Build the basic headers for a SOAP request on.
static Map<String, String> _buildHeaders(String soapAction) =>
{"Content-Type": "text/xml", "SOAPAction": soapAction};
static String _operationResponseTag(String operation) => "${operation}Result";
/// Send a SOAP request to SignetsAPI using [body] as envelope then return
/// the response.
/// Will throw a [ApiException] if an error is returned by the api.
static Future<XmlElement> sendSOAPRequest(
http.Client client, XmlDocument body, String operation) async {
// Send the envelope
final response = await client.post(Uri.parse(Urls.signetsAPI),
headers: _buildHeaders(Urls.signetsOperationBase + operation),
body: body.toXmlString());
final responseBody = XmlDocument.parse(response.body)
.findAllElements(_operationResponseTag(operation))
.first;
// Throw exception if the error tag contains a blocking error
if (responseBody
.findElements(SignetsError.signetsErrorSoapTag)
.isNotEmpty &&
responseBody
.findElements(SignetsError.signetsErrorSoapTag)
.first
.innerText
.isNotEmpty) {
switch (responseBody
.findElements(SignetsError.signetsErrorSoapTag)
.first
.innerText) {
case SignetsError.scheduleNotAvailable:
case SignetsError.scheduleNotAvailableF:
// Don't do anything.
break;
case SignetsError.credentialsInvalid:
default:
throw ApiException(
prefix: tagError,
message: responseBody
.findElements(SignetsError.signetsErrorSoapTag)
.first
.innerText);
}
}
return responseBody;
}
}