LCOV - code coverage report
Current view: top level - lib - client.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 37 43 86.0 %
Date: 2020-05-04 13:08:30 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:async';
       2             : import 'dart:convert';
       3             : import 'package:contentstack/contentstack.dart';
       4             : import 'package:http/http.dart' as http;
       5             : import 'package:contentstack/src/stack.dart';
       6             : 
       7             : ///@nodoc
       8             : class HttpClient extends http.BaseClient {
       9             :   final http.Client _client;
      10             :   final Stack stack;
      11             :   final Map<String, String> stackHeaders;
      12             : 
      13           1 :   factory HttpClient(Map<String, String> headers,
      14             :       {http.Client client, Stack stack}) {
      15           1 :     final stackClient = client ?? http.Client();
      16           1 :     return HttpClient._internal(stackClient, headers, stack);
      17             :   }
      18             : 
      19           1 :   HttpClient._internal(this._client, this.stackHeaders, this.stack);
      20             : 
      21           1 :   @override
      22             :   Future<http.StreamedResponse> send(http.BaseRequest request) {
      23           2 :     return _client.send(request);
      24             :   }
      25             : 
      26             :   ///@nodoc
      27           1 :   Future<T> sendRequest<T, K>(Uri uri) async {
      28           2 :     stackHeaders['Content-Type'] = 'application/json';
      29           2 :     stackHeaders['sdk'] = 'contentstack-dart-v0.1.0';
      30           3 :     final response = await http.get(uri, headers: stackHeaders);
      31             :     Object bodyJson;
      32             :     try {
      33           2 :       bodyJson = jsonDecode(response.body);
      34           0 :     } on FormatException {
      35           0 :       final contentType = response.headers['content-type'];
      36           0 :       if (contentType != null && !contentType.contains('application/json')) {
      37           0 :         throw Exception(
      38             :             "Returned value was not JSON. Did the uri end with '.json'?");
      39             :       }
      40             :       rethrow;
      41             :     }
      42           2 :     if (response.statusCode == 200) {
      43           2 :       final Map bodyJson = jsonDecode(response.body);
      44           2 :       if (T == EntryModel && bodyJson.containsKey('entry')) {
      45           2 :         return fromJson<T, K>(bodyJson['entry']);
      46           2 :       } else if (K == EntryModel && bodyJson.containsKey('entries')) {
      47           2 :         return fromJson<T, K>(bodyJson['entries']);
      48           2 :       } else if (T == AssetModel && bodyJson.containsKey('asset')) {
      49           2 :         return fromJson<T, K>(bodyJson['asset']);
      50           2 :       } else if (K == AssetModel && bodyJson.containsKey('assets')) {
      51           2 :         return fromJson<T, K>(bodyJson['assets']);
      52           2 :       }else if (T == SyncResult && bodyJson.containsKey('items')) {
      53           1 :         return fromJson<T, K>(bodyJson);
      54             :       } else {
      55           1 :         return fromJson<T, K>(bodyJson);
      56             :       }
      57             :     } else {
      58             :       //Error.fromJson(bodyJson);
      59             :       return bodyJson;
      60             :     }
      61             :   }
      62             : 
      63           0 :   @override
      64           0 :   void close() => _client.close();
      65             : 
      66             :   ///////////////////////////////////////
      67             :   // generic objects as well as List of generic objects (from a JSON list response).
      68             :   // First, you need to have a function that checks the type of the generic object
      69             :   // and returns the result of the corresponding fromJson call
      70             :   // code taken from:
      71             :   // https://stackoverflow.com/questions/56271651/how-to-pass-a-generic-type-as-a-parameter-to-a-future-in-flutter
      72             :   ///////////////////////////////////////
      73             : 
      74           1 :   static T fromJson<T, K>(dynamic json) {
      75           1 :     if (json is Iterable) {
      76           1 :       return _fromJsonList<K>(json) as T;
      77           1 :     } else if (T == AssetModel) {
      78           1 :       return AssetModel.fromJson(json) as T;
      79           1 :     } else if (T == EntryModel) {
      80           1 :       return EntryModel.fromJson(json) as T;
      81           1 :     } else if (T == SyncResult) {
      82           1 :       return SyncResult.fromJson(json) as T;
      83             :     } else {
      84             :       return json;
      85             :     }
      86             :   }
      87             : 
      88           1 :   static List<K> _fromJsonList<K>(List jsonList) {
      89             :     if (jsonList == null) {
      90             :       return null;
      91             :     }
      92             : 
      93           1 :     final output = <K>[];
      94             :     // ignore: prefer_final_in_for_each
      95           2 :     for (Map<String, dynamic> json in jsonList) {
      96           2 :       output.add(fromJson(json));
      97             :     }
      98             :     return output;
      99             :   }
     100             : }

Generated by: LCOV version 1.14