Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
162 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
part of katana; | ||
|
||
/// HTTP requests can be sent. | ||
/// | ||
/// Wrapper for http package. | ||
class Api { | ||
const Api._(); | ||
|
||
/// Sends an HTTP DELETE request with the given headers to the given URL. | ||
/// | ||
/// This automatically initializes a new [http.Client] and closes that client once the request is complete. If you're planning on making multiple requests to the same server, you should use a single [http.Client] for all of those requests. | ||
Future<ApiResponse?> delete( | ||
String path, { | ||
Map<String, String>? headers, | ||
Object? body, | ||
Encoding? encoding, | ||
}) async { | ||
final url = Uri.tryParse(path); | ||
if (url == null) { | ||
return null; | ||
} | ||
return http.delete( | ||
url, | ||
headers: headers, | ||
body: body, | ||
encoding: encoding, | ||
); | ||
} | ||
|
||
/// Sends an HTTP GET request with the given headers to the given URL. | ||
/// | ||
/// This automatically initializes a new [http.Client] and closes that client once the request is complete. If you're planning on making multiple requests to the same server, you should use a single [http.Client] for all of those requests. | ||
Future<ApiResponse?> get(String path, {Map<String, String>? headers}) async { | ||
final url = Uri.tryParse(path); | ||
if (url == null) { | ||
return null; | ||
} | ||
return http.get( | ||
url, | ||
headers: headers, | ||
); | ||
} | ||
|
||
/// Sends an HTTP HEAD request with the given headers to the given URL. | ||
/// | ||
/// This automatically initializes a new [http.Client] and closes that client once the request is complete. If you're planning on making multiple requests to the same server, you should use a single [http.Client] for all of those requests. | ||
Future<ApiResponse?> head(String path, {Map<String, String>? headers}) async { | ||
final url = Uri.tryParse(path); | ||
if (url == null) { | ||
return null; | ||
} | ||
return http.head( | ||
url, | ||
headers: headers, | ||
); | ||
} | ||
|
||
/// Sends an HTTP PATCH request with the given headers and body to the given URL. | ||
/// | ||
/// [body] sets the body of the request. It can be a [String], a [List] or a [Map<String, String>]. If it's a String, it's encoded using [encoding] and used as the body of the request. The content-type of the request will default to "text/plain". | ||
/// | ||
/// If [body] is a List, it's used as a list of bytes for the body of the request. | ||
/// | ||
/// If [body] is a Map, it's encoded as form fields using [encoding]. The content-type of the request will be set to "application/x-www-form-urlencoded"; this cannot be overridden. | ||
/// | ||
/// [encoding] defaults to [utf8]. | ||
Future<ApiResponse?> patch( | ||
String path, { | ||
Map<String, String>? headers, | ||
Object? body, | ||
Encoding? encoding, | ||
}) async { | ||
final url = Uri.tryParse(path); | ||
if (url == null) { | ||
return null; | ||
} | ||
return http.patch( | ||
url, | ||
headers: headers, | ||
body: body, | ||
encoding: encoding, | ||
); | ||
} | ||
|
||
/// Sends an HTTP POST request with the given headers and body to the given URL. | ||
/// | ||
/// [body] sets the body of the request. It can be a [String], a [List] or a [Map<String, String>]. If it's a String, it's encoded using [encoding] and used as the body of the request. The content-type of the request will default to "text/plain". | ||
/// | ||
/// If [body] is a List, it's used as a list of bytes for the body of the request. | ||
/// | ||
/// If [body] is a Map, it's encoded as form fields using [encoding]. The content-type of the request will be set to "application/x-www-form-urlencoded"; this cannot be overridden. | ||
/// | ||
/// [encoding] defaults to [utf8]. | ||
Future<ApiResponse?> post( | ||
String path, { | ||
Map<String, String>? headers, | ||
Object? body, | ||
Encoding? encoding, | ||
}) async { | ||
final url = Uri.tryParse(path); | ||
if (url == null) { | ||
return null; | ||
} | ||
return http.post( | ||
url, | ||
headers: headers, | ||
body: body, | ||
encoding: encoding, | ||
); | ||
} | ||
|
||
/// Sends an HTTP PUT request with the given headers and body to the given URL. | ||
/// | ||
/// [body] sets the body of the request. It can be a [String], a [List] or a [Map<String, String>]. If it's a String, it's encoded using [encoding] and used as the body of the request. The content-type of the request will default to "text/plain". | ||
/// | ||
/// If [body] is a List, it's used as a list of bytes for the body of the request. | ||
/// | ||
/// If [body] is a Map, it's encoded as form fields using [encoding]. The content-type of the request will be set to "application/x-www-form-urlencoded"; this cannot be overridden. | ||
/// | ||
/// [encoding] defaults to [utf8]. | ||
Future<ApiResponse?> put( | ||
String path, { | ||
Map<String, String>? headers, | ||
Object? body, | ||
Encoding? encoding, | ||
}) async { | ||
final url = Uri.tryParse(path); | ||
if (url == null) { | ||
return null; | ||
} | ||
return http.put( | ||
url, | ||
headers: headers, | ||
body: body, | ||
encoding: encoding, | ||
); | ||
} | ||
} | ||
|
||
/// An HTTP response where the entire response body is known in advance. | ||
typedef ApiResponse = http.Response; | ||
|
||
/// An HTTP request where the entire request body is known in advance. | ||
typedef ApiResquest = http.Request; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ dependencies: | |
uuid: ^3.0.4 | ||
sprintf: ^6.0.0 | ||
encrypt: ^5.0.1 | ||
http: ^0.13.5 | ||
|
||
dev_dependencies: | ||
test: ^1.21.3 |