|
1 | 1 | import 'package:functions_client/src/constants.dart';
|
2 | 2 | import 'package:functions_client/src/types.dart';
|
3 | 3 | import 'package:http/http.dart' as http;
|
| 4 | +import 'package:http/http.dart'; |
4 | 5 | import 'package:yet_another_json_isolate/yet_another_json_isolate.dart';
|
5 | 6 |
|
6 | 7 | class FunctionsClient {
|
@@ -42,15 +43,67 @@ class FunctionsClient {
|
42 | 43 | String functionName, {
|
43 | 44 | Map<String, String>? headers,
|
44 | 45 | Map<String, dynamic>? body,
|
| 46 | + HttpMethod method = HttpMethod.post, |
45 | 47 | ResponseType responseType = ResponseType.json,
|
46 | 48 | }) async {
|
47 | 49 | final bodyStr = body == null ? null : await _isolate.encode(body);
|
48 | 50 |
|
49 |
| - final response = await (_httpClient?.post ?? http.post)( |
50 |
| - Uri.parse('$_url/$functionName'), |
51 |
| - headers: <String, String>{..._headers, if (headers != null) ...headers}, |
52 |
| - body: bodyStr, |
53 |
| - ); |
| 51 | + late final Response response; |
| 52 | + |
| 53 | + switch (method) { |
| 54 | + case HttpMethod.post: |
| 55 | + response = await (_httpClient?.post ?? http.post)( |
| 56 | + Uri.parse('$_url/$functionName'), |
| 57 | + headers: <String, String>{ |
| 58 | + ..._headers, |
| 59 | + if (headers != null) ...headers |
| 60 | + }, |
| 61 | + body: bodyStr, |
| 62 | + ); |
| 63 | + break; |
| 64 | + |
| 65 | + case HttpMethod.get: |
| 66 | + response = await (_httpClient?.get ?? http.get)( |
| 67 | + Uri.parse('$_url/$functionName'), |
| 68 | + headers: <String, String>{ |
| 69 | + ..._headers, |
| 70 | + if (headers != null) ...headers |
| 71 | + }, |
| 72 | + ); |
| 73 | + break; |
| 74 | + |
| 75 | + case HttpMethod.put: |
| 76 | + response = await (_httpClient?.put ?? http.put)( |
| 77 | + Uri.parse('$_url/$functionName'), |
| 78 | + headers: <String, String>{ |
| 79 | + ..._headers, |
| 80 | + if (headers != null) ...headers |
| 81 | + }, |
| 82 | + body: bodyStr, |
| 83 | + ); |
| 84 | + break; |
| 85 | + |
| 86 | + case HttpMethod.delete: |
| 87 | + response = await (_httpClient?.delete ?? http.delete)( |
| 88 | + Uri.parse('$_url/$functionName'), |
| 89 | + headers: <String, String>{ |
| 90 | + ..._headers, |
| 91 | + if (headers != null) ...headers |
| 92 | + }, |
| 93 | + ); |
| 94 | + break; |
| 95 | + |
| 96 | + case HttpMethod.patch: |
| 97 | + response = await (_httpClient?.patch ?? http.patch)( |
| 98 | + Uri.parse('$_url/$functionName'), |
| 99 | + headers: <String, String>{ |
| 100 | + ..._headers, |
| 101 | + if (headers != null) ...headers |
| 102 | + }, |
| 103 | + body: bodyStr, |
| 104 | + ); |
| 105 | + break; |
| 106 | + } |
54 | 107 |
|
55 | 108 | final dynamic data;
|
56 | 109 | if (responseType == ResponseType.json) {
|
|
0 commit comments