Created
October 22, 2022 08:26
-
-
Save shiqinwen/4bedc9c1b9f1dc28d03b6131f12fea5e to your computer and use it in GitHub Desktop.
An Client To Call G2Rail Mobile Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'dart:convert'; | |
import 'dart:io'; | |
import 'package:crypto/crypto.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
import 'package:meta/meta.dart'; | |
class SearchCriteria { | |
String from; | |
String to; | |
String date; | |
String time; | |
int adult; | |
int child; | |
SearchCriteria(from, to, date, time, adult, child) { | |
this.from = from; | |
this.to = to; | |
this.date = date; | |
this.time = time; | |
this.adult = adult; | |
this.child = child; | |
} | |
String toQuery() { | |
return "from=$from&to=$to&date=$date&time=$time&adult=$adult&child=$child"; | |
} | |
Map<String, dynamic> toMap() { | |
return { | |
"from": from, | |
"to": to, | |
"date": date, | |
"time": time, | |
"adult": adult, | |
"child": child | |
}; | |
} | |
} | |
class GrailApiClient { | |
final baseUrl; | |
final apiKey; | |
final secret; | |
final http.Client httpClient; | |
GrailApiClient({@required this.httpClient, | |
@required this.baseUrl, | |
@required this.apiKey, | |
@required this.secret}) | |
: assert(httpClient != null); | |
Map<String, String> getAuthorizationHeaders(Map<String, dynamic> params) { | |
var timestamp = DateTime.now(); | |
params['t'] = (timestamp.millisecondsSinceEpoch ~/ 1000).toString(); | |
params['api_key'] = apiKey; | |
var sortedKeys = params.keys.toList() | |
..sort((a, b) => a.compareTo(b)); | |
StringBuffer buffer = StringBuffer(""); | |
sortedKeys.forEach((key) { | |
if (params[key] is List || params[key] is Map) return; | |
buffer.write('$key=${params[key].toString()}'); | |
}); | |
buffer.write(secret); | |
String hashString = buffer.toString(); | |
String authorization = md5.convert(utf8.encode(hashString)).toString(); | |
return { | |
"From": apiKey, | |
"Content-Type": 'application/json', | |
"Authorization": authorization, | |
"Date": HttpDate.format(timestamp), | |
"Api-Locale": "zh-TW" | |
}; | |
} | |
Future<dynamic> getSolutions(from, to, date, time, adult, child) async { | |
final criteria = SearchCriteria(from, to, date, time, adult, child); | |
final solutionUrl = | |
'$baseUrl/api/v2/online_solutions/?${criteria.toQuery()}'; | |
final solutionResponse = await this | |
.httpClient | |
.get(Uri.parse(solutionUrl), headers: getAuthorizationHeaders(criteria.toMap())); | |
if (solutionResponse.statusCode != 200) { | |
throw Exception('error getting solutions'); | |
} | |
final solutionsJson = jsonDecode(solutionResponse.body); | |
return solutionsJson; | |
} | |
Future<dynamic> getAsyncResult(String asyncKey) async { | |
final asyncResultURl = '$baseUrl/api/v2/async_results/$asyncKey'; | |
final asyncResult = await this.httpClient.get(Uri.parse(asyncResultURl), | |
headers: getAuthorizationHeaders({"async_key": asyncKey})); | |
return {"data": jsonDecode(utf8.decode(asyncResult.bodyBytes))}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main() { | |
var client = GrailApiClient(httpClient: http.Client(), baseUrl: "----", apiKey: "---", secret: "---"); | |
var asyncKey = client.getSolutions("BERLIN", "FRANKFURT", "2022-10-28", "10:00", 1, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment