Line data Source code
1 : import 'package:at_client/src/response/response.dart';
2 : import 'package:at_client/src/response/response_parser.dart';
3 :
4 : /// The default implementation of [ResponseParser]
5 : class DefaultResponseParser implements ResponseParser {
6 : /// Returns Response object which contains response or error based on the result.
7 : /// @param responseString - response coming from secondary server
8 : /// @returns Response
9 1 : @override
10 : Response parse(String responseString) {
11 1 : var response = Response();
12 : // if responseString starts with data: will call parseSuccessResponse
13 1 : if (responseString.startsWith('data:')) {
14 1 : parseSuccessResponse(responseString, response);
15 : // if responseString starts with data: will call parseFailureResponse
16 1 : } else if (responseString.startsWith('error:')) {
17 1 : parseFailureResponse(responseString, response);
18 : } else {
19 1 : response.response = responseString;
20 : }
21 : return response;
22 : }
23 :
24 : /// Remove data: in responseString and set remaining string to response in Response object
25 : /// @param responseString - response coming from secondary server
26 : /// @param response - Response object from parse method
27 : /// @returns void
28 1 : void parseSuccessResponse(String responseString, Response response) {
29 2 : response.response = responseString.replaceFirst('data:', '');
30 : }
31 :
32 : /// Remove error: in responseString and extract Error code and error response if any
33 : /// set isError to true
34 : /// set errorCode and errorDescription if any
35 : /// @param responseString - response coming from secondary server
36 : /// @param response - Response object from parse method
37 : /// @returns void
38 1 : void parseFailureResponse(String responseString, Response response) {
39 : // Remove error: from responseString
40 1 : responseString = responseString.replaceFirst('error:', '');
41 : // Set isError to true
42 1 : response.isError = true;
43 : // Find out whether error code exists or not
44 2 : if (responseString.isNotEmpty && responseString.startsWith('AT')) {
45 3 : response.errorCode = (responseString.split(':')[0]);
46 4 : response.errorDescription = (responseString.split(':').length > 1)
47 2 : ? responseString.split(':')[1]
48 : : '';
49 : } else {
50 1 : response.errorDescription = responseString;
51 : }
52 : }
53 : }
|