|
| 1 | +import 'dart:convert'; |
| 2 | +import 'auth_session.dart'; |
| 3 | +import 'transport.dart'; |
| 4 | + |
| 5 | +class APIMethods { |
| 6 | + final AuthSession _authSession; |
| 7 | + |
| 8 | + APIMethods(this._authSession); |
| 9 | + |
| 10 | + Future<T> ok<T>(Future<SDKResponse<T>> future) async { |
| 11 | + var response = await future; |
| 12 | + if (response.ok) { |
| 13 | + return response.result; |
| 14 | + } else { |
| 15 | + throw Exception( |
| 16 | + 'Invalid SDK response ${response.statusCode}/${response.statusText}/${response.decodedRawResult}'); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + Future<SDKResponse<T>> get<T>( |
| 21 | + T Function(dynamic responseData, String contentType) responseHandler, |
| 22 | + String path, |
| 23 | + [dynamic queryParams, |
| 24 | + dynamic body]) async { |
| 25 | + var headers = await _getHeaders(); |
| 26 | + return _authSession.transport.request( |
| 27 | + responseHandler, |
| 28 | + HttpMethod.get, |
| 29 | + '${_authSession.apiPath}$path', |
| 30 | + queryParams, |
| 31 | + body, |
| 32 | + headers, |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + Future<SDKResponse> head(String path, |
| 37 | + [dynamic queryParams, dynamic body]) async { |
| 38 | + var headers = await _getHeaders(); |
| 39 | + dynamic responseHandler(dynamic responseData, String contentType) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + return _authSession.transport.request(responseHandler, HttpMethod.head, |
| 44 | + '${_authSession.apiPath}$path', queryParams, body, headers); |
| 45 | + } |
| 46 | + |
| 47 | + Future<SDKResponse<T>> delete<T>( |
| 48 | + T Function(dynamic responseData, String contentType) responseHandler, |
| 49 | + String path, |
| 50 | + [dynamic queryParams, |
| 51 | + dynamic body, |
| 52 | + T responseInstance]) async { |
| 53 | + var headers = await _getHeaders(); |
| 54 | + return _authSession.transport.request(responseHandler, HttpMethod.delete, |
| 55 | + '${_authSession.apiPath}$path', queryParams, body, headers); |
| 56 | + } |
| 57 | + |
| 58 | + Future<SDKResponse<T>> post<T>( |
| 59 | + T Function(dynamic responseData, String contentType) responseHandler, |
| 60 | + String path, |
| 61 | + [dynamic queryParams, |
| 62 | + dynamic body, |
| 63 | + T responseInstance]) async { |
| 64 | + var headers = await _getHeaders(); |
| 65 | + var requestBody = body == null ? null : jsonEncode(body); |
| 66 | + return _authSession.transport.request(responseHandler, HttpMethod.post, |
| 67 | + '${_authSession.apiPath}$path', queryParams, requestBody, headers); |
| 68 | + } |
| 69 | + |
| 70 | + Future<SDKResponse<T>> put<T>( |
| 71 | + T Function(dynamic responseData, String contentType) responseHandler, |
| 72 | + String path, |
| 73 | + [dynamic queryParams, |
| 74 | + dynamic body, |
| 75 | + T responseInstance]) async { |
| 76 | + var headers = await _getHeaders(); |
| 77 | + return _authSession.transport.request(responseHandler, HttpMethod.put, |
| 78 | + '${_authSession.apiPath}$path', queryParams, body, headers); |
| 79 | + } |
| 80 | + |
| 81 | + Future<SDKResponse<T>> patch<T>( |
| 82 | + T Function(dynamic responseData, String contentType) responseHandler, |
| 83 | + String path, |
| 84 | + [dynamic queryParams, |
| 85 | + dynamic body, |
| 86 | + T responseInstance]) async { |
| 87 | + var headers = await _getHeaders(); |
| 88 | + var requestBody; |
| 89 | + if (body != null) { |
| 90 | + body.removeWhere((key, value) => value == null); |
| 91 | + requestBody = jsonEncode(body); |
| 92 | + } |
| 93 | + return _authSession.transport.request(responseHandler, HttpMethod.patch, |
| 94 | + '${_authSession.apiPath}$path', queryParams, requestBody, headers); |
| 95 | + } |
| 96 | + |
| 97 | + Future<Map<String, String>> _getHeaders() async { |
| 98 | + var headers = <String, String>{ |
| 99 | + 'x-looker-appid': _authSession.transport.settings.agentTag |
| 100 | + }; |
| 101 | + headers.addAll(_authSession.authenticate()); |
| 102 | + return headers; |
| 103 | + } |
| 104 | +} |
0 commit comments