injectable_http_service 1.2.0 copy "injectable_http_service: ^1.2.0" to clipboard
injectable_http_service: ^1.2.0 copied to clipboard

outdated

Simple http service to use with injectable.

Usage #

const http = HttpServiceImpl();

final myModel = await http.get<MyModel>('example.com', MyModel.fromJson);

or for DI with injectable package:

@module  
abstract class RegisterModule {  
  @Injectable(as: HttpService)
  HttpServiceImpl get httpService;  
}  

You can even extend it:

@named 
@Injectable(as: HttpService)
class OpenWeatherHttpServiceImpl extends JsonHttpServiceImpl {

  @override
  FutureOr<AppHttpRequest> beforeHook(String url, HttpVerb verb,  Object? body, Map<String, String>? headers) async {
    final req = await super.beforeHook(url, verb, body, headers);
    final uri = req.uri.replace(queryParameters: {...req.uri.queryParameters}
      ..putIfAbsent('appid', () => '********'));
      
    return AppHttpRequest(
      uri: uri,
      body: req.body,
      headers: req.headers
    );
  }
}

To support any responce formats you can extend HttpServiceBase

class JsonHttpServiceImpl extends HttpServiceBase<Map<String, dynamic>> {
  @override
  List<Map<String, dynamic>> parseListResult(Response response) {
    return jsonDecode(response.body);
  }

  @override
  Map<String, dynamic> parseResult(Response response) {
    return jsonDecode(response.body);
  }
}
5
likes
0
pub points
0%
popularity

Publisher

unverified uploader

Simple http service to use with injectable.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

http

More

Packages that depend on injectable_http_service