amondnet / flagr.dart
1
import 'dart:async';
2
import 'dart:io';
3
import 'dart:convert';
4
import 'package:dio/dio.dart';
5
import 'package:built_collection/built_collection.dart';
6
import 'package:built_value/serializer.dart';
7

8
import 'package:flagr/model/create_variant_request.dart';
9
import 'package:flagr/model/variant.dart';
10
import 'package:flagr/model/error.dart';
11
import 'package:flagr/model/put_variant_request.dart';
12

13
class VariantApi {
14
  final Dio _dio;
15
  Serializers _serializers;
16

17 0
  VariantApi(this._dio, this._serializers);
18

19
  ///
20
  ///
21
  ///
22 0
  Future<Response<Variant>> createVariant(
23
    int flagID,
24
    CreateVariantRequest body, {
25
    CancelToken cancelToken,
26
    Map<String, String> headers,
27
  }) async {
28
    String _path = "/flags/{flagID}/variants"
29 0
        .replaceAll("{" r'flagID' "}", flagID.toString());
30

31 0
    Map<String, dynamic> queryParams = {};
32 0
    Map<String, String> headerParams = Map.from(headers ?? {});
33
    dynamic bodyData;
34

35 0
    queryParams.removeWhere((key, value) => value == null);
36 0
    headerParams.removeWhere((key, value) => value == null);
37

38 0
    List<String> contentTypes = ["application/json"];
39

40 0
    var serializedBody = _serializers.serialize(body);
41 0
    var jsonbody = json.encode(serializedBody);
42
    bodyData = jsonbody;
43

44 0
    return _dio
45 0
        .request(
46
      _path,
47
      queryParameters: queryParams,
48
      data: bodyData,
49 0
      options: Options(
50 0
        method: 'post'.toUpperCase(),
51
        headers: headerParams,
52 0
        extra: {
53 0
          'secure': [],
54
        },
55
        contentType:
56 0
            contentTypes.isNotEmpty ? contentTypes[0] : "application/json",
57
      ),
58
      cancelToken: cancelToken,
59
    )
60 0
        .then((response) {
61 0
      var serializer = _serializers.serializerForType(Variant);
62 0
      var data = _serializers.deserializeWith<Variant>(serializer,
63 0
          response.data is String ? jsonDecode(response.data) : response.data);
64

65 0
      return Response<Variant>(
66
        data: data,
67 0
        headers: response.headers,
68 0
        request: response.request,
69 0
        redirects: response.redirects,
70 0
        statusCode: response.statusCode,
71 0
        statusMessage: response.statusMessage,
72 0
        extra: response.extra,
73
      );
74
    });
75
  }
76

77
  ///
78
  ///
79
  ///
80 0
  Future<Response> deleteVariant(
81
    int flagID,
82
    int variantID, {
83
    CancelToken cancelToken,
84
    Map<String, String> headers,
85
  }) async {
86
    String _path = "/flags/{flagID}/variants/{variantID}"
87 0
        .replaceAll("{" r'flagID' "}", flagID.toString())
88 0
        .replaceAll("{" r'variantID' "}", variantID.toString());
89

90 0
    Map<String, dynamic> queryParams = {};
91 0
    Map<String, String> headerParams = Map.from(headers ?? {});
92
    dynamic bodyData;
93

94 0
    queryParams.removeWhere((key, value) => value == null);
95 0
    headerParams.removeWhere((key, value) => value == null);
96

97 0
    List<String> contentTypes = [];
98

99 0
    return _dio.request(
100
      _path,
101
      queryParameters: queryParams,
102
      data: bodyData,
103 0
      options: Options(
104 0
        method: 'delete'.toUpperCase(),
105
        headers: headerParams,
106 0
        extra: {
107 0
          'secure': [],
108
        },
109
        contentType:
110 0
            contentTypes.isNotEmpty ? contentTypes[0] : "application/json",
111
      ),
112
      cancelToken: cancelToken,
113
    );
114
  }
115

116
  ///
117
  ///
118
  ///
119 0
  Future<Response<List<Variant>>> findVariants(
120
    int flagID, {
121
    CancelToken cancelToken,
122
    Map<String, String> headers,
123
  }) async {
124
    String _path = "/flags/{flagID}/variants"
125 0
        .replaceAll("{" r'flagID' "}", flagID.toString());
126

127 0
    Map<String, dynamic> queryParams = {};
128 0
    Map<String, String> headerParams = Map.from(headers ?? {});
129
    dynamic bodyData;
130

131 0
    queryParams.removeWhere((key, value) => value == null);
132 0
    headerParams.removeWhere((key, value) => value == null);
133

134 0
    List<String> contentTypes = [];
135

136 0
    return _dio
137 0
        .request(
138
      _path,
139
      queryParameters: queryParams,
140
      data: bodyData,
141 0
      options: Options(
142 0
        method: 'get'.toUpperCase(),
143
        headers: headerParams,
144 0
        extra: {
145 0
          'secure': [],
146
        },
147
        contentType:
148 0
            contentTypes.isNotEmpty ? contentTypes[0] : "application/json",
149
      ),
150
      cancelToken: cancelToken,
151
    )
152 0
        .then((response) {
153
      final FullType type =
154
          const FullType(BuiltList, const [const FullType(Variant)]);
155 0
      BuiltList<Variant> dataList = _serializers.deserialize(
156 0
          response.data is String ? jsonDecode(response.data) : response.data,
157
          specifiedType: type);
158 0
      var data = dataList.toList();
159

160 0
      return Response<List<Variant>>(
161
        data: data,
162 0
        headers: response.headers,
163 0
        request: response.request,
164 0
        redirects: response.redirects,
165 0
        statusCode: response.statusCode,
166 0
        statusMessage: response.statusMessage,
167 0
        extra: response.extra,
168
      );
169
    });
170
  }
171

172
  ///
173
  ///
174
  ///
175 0
  Future<Response<Variant>> putVariant(
176
    int flagID,
177
    int variantID,
178
    PutVariantRequest body, {
179
    CancelToken cancelToken,
180
    Map<String, String> headers,
181
  }) async {
182
    String _path = "/flags/{flagID}/variants/{variantID}"
183 0
        .replaceAll("{" r'flagID' "}", flagID.toString())
184 0
        .replaceAll("{" r'variantID' "}", variantID.toString());
185

186 0
    Map<String, dynamic> queryParams = {};
187 0
    Map<String, String> headerParams = Map.from(headers ?? {});
188
    dynamic bodyData;
189

190 0
    queryParams.removeWhere((key, value) => value == null);
191 0
    headerParams.removeWhere((key, value) => value == null);
192

193 0
    List<String> contentTypes = ["application/json"];
194

195 0
    var serializedBody = _serializers.serialize(body);
196 0
    var jsonbody = json.encode(serializedBody);
197
    bodyData = jsonbody;
198

199 0
    return _dio
200 0
        .request(
201
      _path,
202
      queryParameters: queryParams,
203
      data: bodyData,
204 0
      options: Options(
205 0
        method: 'put'.toUpperCase(),
206
        headers: headerParams,
207 0
        extra: {
208 0
          'secure': [],
209
        },
210
        contentType:
211 0
            contentTypes.isNotEmpty ? contentTypes[0] : "application/json",
212
      ),
213
      cancelToken: cancelToken,
214
    )
215 0
        .then((response) {
216 0
      var serializer = _serializers.serializerForType(Variant);
217 0
      var data = _serializers.deserializeWith<Variant>(serializer,
218 0
          response.data is String ? jsonDecode(response.data) : response.data);
219

220 0
      return Response<Variant>(
221
        data: data,
222 0
        headers: response.headers,
223 0
        request: response.request,
224 0
        redirects: response.redirects,
225 0
        statusCode: response.statusCode,
226 0
        statusMessage: response.statusMessage,
227 0
        extra: response.extra,
228
      );
229
    });
230
  }
231
}

Read our documentation on viewing source code .

Loading