Skip to content
This repository was archived by the owner on May 13, 2023. It is now read-only.

Commit c623152

Browse files
gabfeudoGabriele Feudo
andauthored
feat: custom HTTP method for function invoke (#16)
Co-authored-by: Gabriele Feudo <[email protected]>
1 parent 222e803 commit c623152

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

lib/src/functions_client.dart

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:functions_client/src/constants.dart';
22
import 'package:functions_client/src/types.dart';
33
import 'package:http/http.dart' as http;
4+
import 'package:http/http.dart';
45
import 'package:yet_another_json_isolate/yet_another_json_isolate.dart';
56

67
class FunctionsClient {
@@ -42,15 +43,67 @@ class FunctionsClient {
4243
String functionName, {
4344
Map<String, String>? headers,
4445
Map<String, dynamic>? body,
46+
HttpMethod method = HttpMethod.post,
4547
ResponseType responseType = ResponseType.json,
4648
}) async {
4749
final bodyStr = body == null ? null : await _isolate.encode(body);
4850

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+
}
54107

55108
final dynamic data;
56109
if (responseType == ResponseType.json) {

lib/src/types.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ enum ResponseType {
55
blob,
66
}
77

8+
enum HttpMethod {
9+
get,
10+
post,
11+
put,
12+
delete,
13+
patch,
14+
}
15+
816
class FunctionInvokeOptions {
917
final Map<String, String>? headers;
1018
final dynamic body;

0 commit comments

Comments
 (0)