Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/joutvhu/dioxide

Dioxide is a type conversion dio client generator.
https://github.com/joutvhu/dioxide

build-runner dart dio http source-gen

Last synced: 12 days ago
JSON representation

Dioxide is a type conversion dio client generator.

Awesome Lists containing this project

README

        

# Dioxide

Dioxide is a type conversion [dio](https://github.com/flutterchina/dio/) client generator.

## Using

### Add dependencies

```yaml
dependencies:
dioxide: ^lastVersion

dev_dependencies:
build_runner: ^2.2.0
dioxide_generator: ^lastVersion
```

### Define and Generate your API

```dart
import 'package:json_annotation/json_annotation.dart';
import 'package:dio/dio.dart';
import 'package:dioxide/dioxide.dart';

part 'example.g.dart';

@RestApi(baseUrl: "http://localhost:8080/api/Task")
abstract class RestClient {
factory RestClient(Dio dio, {String baseUrl}) = _RestClient;

@GetRequest()
Future> getTasks();
}

@JsonSerializable()
class Task {
String? id;
String? name;
String? avatar;
String? createdAt;

Task({this.id, this.name, this.avatar, this.createdAt});

factory Task.fromJson(Map json) => _$TaskFromJson(json);

Map toJson() => _$TaskToJson(this);
}
```

then run the generator

```shell
# dart
pub run build_runner build

# flutter
flutter pub run build_runner build
```

## More

### Type Conversion

> Before you use the type conversion, please make sure that a ` factory Task.fromJson(Map json)` must be provided for each model class. `json_serializable` is the recommanded to be used as the serialization tool.

```dart
@GetRequest("/tasks") Future> getTasks();

@JsonSerializable()
class Task {
String name;
Task({this.name});
factory Task.fromJson(Map json) => _$TaskFromJson(json);
}
```

### HTTP Methods

The HTTP methods in the below sample are supported.

```dart
@GetRequest("/tasks/{id}")
Future getTask(@Path("id") String id);

@GetRequest('/demo')
Future queries(@Queries() Map queries);

@GetRequest("/search")
Future namedExample(
@Query("name") String name,
@Query("tag") String tag,
);

@PatchRequest("/tasks/{id}")
Future updateTaskPart(@Path() String id, @Body() Map map);

@PutRequest("/tasks/{id}")
Future updateTask(@Path() String id, @Body() Task task);

@DeleteRequest("/tasks/{id}")
Future deleteTask(@Path() String id);

@PostRequest("/tasks")
Future createTask(@Body() Task task);

@PostRequest("/post")
Future createNewTaskFromFile(@Part() File file);

@PostRequest("/post")
@FormUrlEncoded()
Future postUrlEncodedFormData(@Field() String hello);
```

### Get original HTTP response

```dart
@GetRequest("/tasks/{id}")
Future> getTask(@Path("id") String id);

@GetRequest("/tasks")
Future>> getTasks();
```

### Get dio Response

```dart
@GetRequest("/file/{fileId}")
@DioResponseType(ResponseType.stream)
Future> getTask(@Path("fileId") String id);
```

### HTTP Header

* Add a HTTP header from the parameter of the method

```dart
@GetRequest("/tasks")
Future getTasks(@Header("Content-Type") String contentType );
```

* Add static HTTP headers

```dart
@GetRequest("/tasks")
@Headers({
"Content-Type" : "application/json",
"Custom-Header" : "Your header"
})
Future getTasks();
```

### Multiple endpoints support

If you want to use multiple endpoints to your `RestClient`, you should pass your base url when you initiate `RestClient`. Any value defined in `RestApi` will be ignored.

```dart
@RestApi(baseUrl: "this url will be ignored if baseUrl is passed")
abstract class RestClient {
factory RestClient(Dio dio, {String baseUrl}) = _RestClient;
}

final client = RestClient(dio, baseUrl: "your base url");
```

If you want to use the base url from `dio.option.baseUrl`, which has lowest priority, please don't pass any parameter to `RestApi` annotation and `RestClient`'s structure method.

### Set request timeout

```dart
@RestApi(baseUrl: 'http://localhost:8080/api/Report')
@RequestTimeout(connectTimeout: 5000, sendTimeout: 5000, receiveTimeout: 5000)
abstract class ReportClient {
@GetRequest("/generate")
@RequestTimeout(sendTimeout: 15000, receiveTimeout: 30000)
@DioResponseType(ResponseType.stream)
Future> getTask(@Queries() Map queries);
}
```