work 0.1.6

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 55

work

pub package

  • 封装http业务接口协议,提供有限的扩展功能,隔离http底层实现方法(当前基于dio),与公司http规范紧密结合,规范团队成员接口编写和使用方式。
  • 核心设计理念为封装http接口的请求数据和响应数据的序列化和反序列化,接口调用处不能出现任何解析http数据的代码。 装配和解析代码应该全部由Work类完成,接口调用处使用一致的调用方式,无需关心http的实现方式和接口参数名称和类型, 仅仅需要注意参数顺序和实现的Work类保持一致。
  • 此设计的缺点是丢弃了一些不常用的http底层实现工具的灵活扩展功能, 优点是规范团队接口编写方式,统一项目http接口代码风格。

Usage

实现公司http规范基类

模仿simple_work.dart中的样板来实现一个Work基类,用于关联自己公司的固定接口规范。

SimpleWork类为一个样例实现,所使用的公司接口规范如下

所有接口响应数据格式

{
  "state":true, // 业务的成功失败标志
  "errorCode":0, // 错误码
  "message":null, // 业务消息字符串,可以是成功时用于显示的信息,也可以是失败时的提示信息
  "result": {}  // 真正响应的有效业务数据,任意类型
}

增加接口

继承公司http规范基类,比如SimpleWork<T>

<T>为真正需要返回的数据模型类

示例


class LoginWork extends SimpleWork<User> {
  @override
  User onExtractResult(resultData,SimpleWorkData<User> data) => User.fromJson(resultData);
  // 解析响应数据

  @override
  void onFillParams(Map<String, dynamic> data, List params) {
    // 装配请求参数,data为最终要发送的参数集合,params为[Work]调用处端传入的参数列表
    data["username"] = params[0]
      ..["password"] = params[1]
      ..["device"] = Platform.isIOS ? "Ios" : "Android";
  }

  @override
  String onUrl(List params) => "https://xxx/user/login";  
  // 地址必须为完整地址,没有baseUrl支持,项目代码必须集中拼接和管理所有接口地址,便于维护和查找

  @override
  HttpMethod get httpMethod => HttpMethod.post; // 使用post请求
}

调用接口


// 有序的参数传入,位置可选参数,[params]为任务参数列表,[retry]为重试次数,
// [onProgress]为进度监听器,目前仅上传和下载任务有效。
LoginWork().start(["user1","123456"]).then((data){
   // start方法返回Future<T> ,T为[SimpleWorkData]类

   if (data.success){
      // 登录成功
      final user = data.result;
   }else{
      // 登录失败
   }
});

支持请求类型

  • HttpMethod中的类型,getpostputdeleteheaduploaddownload
  • 其中upload 基于postmultipart/form-data实现,参数中的文件需要用FileUploadFileInfo类型包装,支持文件列表
  • download默认使用get请求,且由于download特殊性,需要使用独立于其他Work的实现方式,参考SimpleDownloadWork

其他Work生命周期函数

Work中还有很多生命周期方法,用于做有限的接口扩展和定制,原则是接口数据处理由接口自己(即Work)处理。

[0.1.6] - 2018/11/12

  • 延长请求超时时间

[0.1.5] - 2018/11/7

  • 修改log输出位置

[0.1.4] - 2018/11/7

  • 增加请求参数忽略null值的处理

[0.1.3] - 2018/10/18

  • 修复SimpleWorkonExtractResultonDefaultResult丢失data参数的问题

[0.1.2] - 2018/10/8

  • 增加输出日志的tag

[0.1.1] - 2018/9/28

  • 增加上传请求对File类型的支持

[0.1.0] - 2018/9/26

  • 首次完成提交

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  work: ^0.1.6

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:work/work.dart';
  
Version Uploaded Documentation Archive
0.1.6 Nov 12, 2018 Go to the documentation of work 0.1.6 Download work 0.1.6 archive
0.1.5 Nov 7, 2018 Go to the documentation of work 0.1.5 Download work 0.1.5 archive
0.1.4 Nov 7, 2018 Go to the documentation of work 0.1.4 Download work 0.1.4 archive
0.1.3 Oct 18, 2018 Go to the documentation of work 0.1.3 Download work 0.1.3 archive
0.1.2 Oct 8, 2018 Go to the documentation of work 0.1.2 Download work 0.1.2 archive
0.1.1 Sep 28, 2018 Go to the documentation of work 0.1.1 Download work 0.1.1 archive
0.1.0 Sep 27, 2018 Go to the documentation of work 0.1.0 Download work 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
30
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
50
Overall:
Weighted score of the above. [more]
55
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, other

Primary library: package:work/work.dart with components: io.

Maintenance issues and suggestions

The description contains too many non-ASCII characters. (-20 points)

The site uses English as it's primary language. Please use a description that primarily contains characters used when writing English.

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use work.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
dio ^1.0.6 1.0.9
meta ^1.1.6 1.1.6
mime ^0.9.6+2 0.9.6+2
Transitive dependencies
cookie_jar 0.0.7