Skip to content

Commit

Permalink
Publish isolate_http version 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
thongdn-it committed Aug 31, 2021
1 parent 889adfa commit c30288e
Show file tree
Hide file tree
Showing 15 changed files with 618 additions and 0 deletions.
75 changes: 75 additions & 0 deletions isolate_http/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
build/

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Flutter.podspec
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
10 changes: 10 additions & 0 deletions isolate_http/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: fba99f6cf9a14512e461e3122c8ddfaa25394e89
channel: stable

project_type: package
19 changes: 19 additions & 0 deletions isolate_http/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "isolate_http",
"request": "launch",
"type": "dart"
},
{
"name": "isolate_http (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
}
]
}
3 changes: 3 additions & 0 deletions isolate_http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# [1.0.0] - 01/09/2021

* Initial package.
21 changes: 21 additions & 0 deletions isolate_http/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Thong Dang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
52 changes: 52 additions & 0 deletions isolate_http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# IsolateHttp

[![Pub][pub_v_image_url]][pub_url]

IsolateHttp provides a way to launch [http package][http_pub_url] with [IsolateFlutter][isolate_flutter_pub_url].

## Usage

Performing a `GET` request:

```dart
final _response = await IsolateHttp().get('https://example.com/product',
headers: {'Authorization': 'abc='});
print(_response);
```

Performing a `POST` request:

```dart
final _response = await IsolateHttp().post('https://example.com/product',
headers: {'Authorization': 'abc='},
body: {'size': 'XL', 'price': 236},
files: [
HttpFile.fromPath('files', 'path/image_1.png')
]);
print(_response);
```

Performing a `DELETE` request:

```dart
final _response = await IsolateHttp().delete('https://example.com/product/1',
headers: {'Authorization': 'abc='});
print(_response);
```

## Author

IsolateHttp is developed by Thong Dang. You can contact me at thongdn.it@gmail.com

If you like my project, you can support me [![Buy Me A Coffee][buy_me_a_coffee_image_url]][buy_me_a_coffee_url] or star (like) for it.

Thank you! ❤️

[//]: # (reference links)

[http_pub_url]: https://pub.dev/packages/http
[isolate_flutter_pub_url]: https://pub.dev/packages/isolate_flutter
[pub_url]: https://pub.dev/packages/isolate_http
[pub_v_image_url]: https://img.shields.io/pub/v/isolate_http.svg
[buy_me_a_coffee_image_url]: https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png
[buy_me_a_coffee_url]: https://www.buymeacoffee.com/thongdn.it
14 changes: 14 additions & 0 deletions isolate_http/example/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:isolate_http/isolate_http.dart';

void main(List<String> args) async {
// https://developers.google.com/books
final _response = await IsolateHttp().get(
'https://www.googleapis.com/auth/books/v1/volumes',
query: {'q': 'flutter'});
if (_response.statusCode == 200) {
final _bodyJson = _response.bodyJson;
print(_bodyJson);
} else {
print('Request failed with status: ${_response.statusCode}.');
}
}
7 changes: 7 additions & 0 deletions isolate_http/lib/isolate_http.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library isolate_http;

export 'src/http_file.dart';
export 'src/http_method.dart';
export 'src/isolate_http.dart';
export 'src/isolate_http_request.dart';
export 'src/isolate_http_response.dart';
68 changes: 68 additions & 0 deletions isolate_http/lib/src/http_file.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'package:http/http.dart';
import 'package:http_parser/http_parser.dart';

/// A file to be uploaded as part of a [IsolateHttp].
class HttpFile {
/// The name of the form field for the file.
final String field;

/// Byte array of the file.
final List<int> bytes;

/// The path to a file on disk.
final String filePath;

/// The encoding to use when translating [value] into bytes is taken from
/// [contentType] if it has a charset set. Otherwise, it defaults to UTF-8.
/// [contentType] currently defaults to `text/plain; charset=utf-8`, but in
/// the future may be inferred from [filename].
final String value;

/// The basename of the file.
final String filename;

/// The content-type of the file.
final MediaType contentType;

HttpFile._(this.field,
{this.bytes, this.filePath, this.value, this.filename, this.contentType});

/// Creates a new [HttpFile] from a byte array.
factory HttpFile.fromBytes(String field, List<int> bytes,
{String filename, MediaType contentType}) {
return HttpFile._(field,
bytes: bytes, filename: filename, contentType: contentType);
}

/// Creates a new [HttpFile] from a path to a file on disk.
factory HttpFile.fromPath(String field, String filePath,
{String filename, MediaType contentType}) {
return HttpFile._(field,
filePath: filePath, filename: filename, contentType: contentType);
}

/// Creates a new [HttpFile] from a string.
factory HttpFile.fromString(String field, String value,
{String filename, MediaType contentType}) {
return HttpFile._(field,
value: value, filename: filename, contentType: contentType);
}

/// Convert [HttpFile] to [MultipartFile].
///
/// A file to be uploaded as part of a [MultipartRequest].
/// This doesn't need to correspond to a physical file.
Future<MultipartFile> toMultipartFile() async {
if (bytes != null) {
return MultipartFile.fromBytes(field, bytes,
filename: filename, contentType: contentType);
} else if (filePath != null) {
return MultipartFile.fromPath(field, filePath,
filename: filename, contentType: contentType);
} else if (value != null) {
return MultipartFile.fromString(field, value,
filename: filename, contentType: contentType);
}
return null;
}
}
20 changes: 20 additions & 0 deletions isolate_http/lib/src/http_method.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
abstract class HttpMethod {
/// [get] is used to request data from a specified resource.
static const get = 'GET';

/// [post] is used to send data to a server to create/update a resource.
static const post = 'POST';

/// [head] is almost identical to GET, but without the response body.
static const head = 'HEAD';

/// [put] is used to send data to a server to create/update a resource.
///
/// The difference between POST and PUT is that PUT requests are idempotent.
/// That is, calling the same PUT request multiple times will always produce the same result.
/// In contrast, calling a POST request repeatedly have side effects of creating the same resource multiple times.
static const put = 'PUT';

/// The [delete] method deletes the specified resource.
static const delete = 'DELETE';
}
90 changes: 90 additions & 0 deletions isolate_http/lib/src/isolate_http.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import 'package:http/http.dart';

import 'package:isolate_flutter/isolate_flutter.dart';

import 'package:isolate_http/src/http_file.dart';
import 'package:isolate_http/src/http_method.dart';
import 'package:isolate_http/src/isolate_http_request.dart';
import 'package:isolate_http/src/isolate_http_response.dart';

/// Isolate Http
///
/// [head], [get], [post], [put], [delete], [send]
class IsolateHttp {
const IsolateHttp();

/// Sends an HTTP HEAD request with the given headers to the given URL.
Future<IsolateHttpResponse> head(String url,
{Map<String, String> query, Map<String, String> headers}) async {
final _isolateHttpRequest = IsolateHttpRequest(url,
method: HttpMethod.head, query: query, headers: headers);
return send(_isolateHttpRequest);
}

/// Sends an HTTP GET request with the given headers to the given URL.
Future<IsolateHttpResponse> get(String url,
{Map<String, String> query, Map<String, String> headers}) async {
final _isolateHttpRequest = IsolateHttpRequest(url,
method: HttpMethod.get, query: query, headers: headers);
return send(_isolateHttpRequest);
}

/// Sends an HTTP POST request with the given headers and body to the given URL.
Future<IsolateHttpResponse> post(String url,
{Map<String, String> query,
Map<String, String> headers,
Map<String, dynamic> body,
List<HttpFile> files}) async {
final _isolateHttpRequest = IsolateHttpRequest(url,
method: HttpMethod.post,
query: query,
headers: headers,
body: body,
files: files);
return send(_isolateHttpRequest);
}

/// Sends an HTTP PUT request with the given headers and body to the given URL.
Future<IsolateHttpResponse> put(String url,
{Map<String, String> query,
Map<String, String> headers,
Map<String, dynamic> body,
List<HttpFile> files}) async {
final _isolateHttpRequest = IsolateHttpRequest(url,
method: HttpMethod.put,
query: query,
headers: headers,
body: body,
files: files);
return send(_isolateHttpRequest);
}

/// Sends an HTTP DELETE request with the given headers to the given URL.
Future<IsolateHttpResponse> delete(String url,
{Map<String, String> query,
Map<String, String> headers,
Map<String, dynamic> body}) async {
final _isolateHttpRequest = IsolateHttpRequest(url,
method: HttpMethod.delete, query: query, headers: headers, body: body);
return send(_isolateHttpRequest);
}

/// Sends an [IsolateHttpRequest] and returns the [IsolateHttpResponse].
Future<IsolateHttpResponse> send(IsolateHttpRequest request) async {
final _isolateHttpResponse =
await IsolateFlutter.createAndStart(_call, request);
return _isolateHttpResponse;
}
}

Future<IsolateHttpResponse> _call(IsolateHttpRequest isolateHttpRequest) async {
final _request = await isolateHttpRequest.toRequest();
if (_request != null) {
final streamedResponse = await _request.send();
final httpResponse = await Response.fromStream(streamedResponse);
final _isolateHttpResponse = IsolateHttpResponse(
httpResponse.body, httpResponse.statusCode, httpResponse.headers);
return _isolateHttpResponse;
}
return null;
}
Loading

0 comments on commit c30288e

Please sign in to comment.