Fakultas Ilmu Komputer UI

api.dart 18.4 KB
Newer Older
1
import 'dart:convert';
2
import 'dart:io';
Habel Christiando Tobing's avatar
Habel Christiando Tobing committed
3
import 'package:fundex_app/pages/business/EquityOfferings.dart';
4
import 'package:fundex_app/storage/storage.dart';
5
6
7
8
import 'package:http/http.dart' show Client;

class Api {
  Client client = Client();
9
10
11
  final secureStorage = SecureStorage();

  Future login(String email, String password) async {
12
    final apiURL =
13
        Uri.parse('https://canary.fundex.id/api/v1.1/mobile/auth/login');
14
15
16
    var body = jsonEncode({'email': email, 'password': password});
    var headers = {'Content-type': 'application/json'};
    var apiResult = await client.post(apiURL, headers: headers, body: body);
17
    if (apiResult.statusCode == 200) {
18
19
      var jsonObject = json.decode(apiResult.body);
      var jsonData = (jsonObject as Map<String, dynamic>)['data'];
20
21
      var token = jsonData['token'];
      await secureStorage.writeSecureData('token', token);
22
    }
23
    return apiResult.statusCode;
24
  }
25
26

  Future<int> register(String email, String password) async {
27
28
    var apiURL =
        Uri.parse('https://canary.fundex.id/api/v1.1/mobile/auth/register');
29
30
31
32
33
34
35
36
37
38
    Map<String, dynamic> map = {
      'email': email,
      'password': password,
      'role': 'investee',
      'is_company': true
    };
    var jsonBody = jsonEncode(map);
    var apiResult = await client.post(apiURL, body: jsonBody);
    return apiResult.statusCode;
  }
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

  Future otpGet(String token) async {
    var apiURL =
        Uri.parse('https://canary.fundex.id/api/v1.1/auth/otp-login-request');
    var body = jsonEncode({'token': token});
    var headers = {'Content-type': 'application/json'};
    await client.post(apiURL, headers: headers, body: body);
  }

  Future otpInput(String token, String otp) async {
    var apiURL = Uri.parse('https://canary.fundex.id/api/v1.1/auth/verify');
    var body = jsonEncode({'token': token, 'otp': otp});
    var headers = {'Content-type': 'application/json'};
    var apiResult = await client.post(apiURL, headers: headers, body: body);
    if (apiResult.statusCode == 200) {
      print('pinter');
      var jsonObject = json.decode(apiResult.body);
      var accessToken = (jsonObject as Map<String, dynamic>)['access_token'];
      await secureStorage.writeSecureData('accessToken', accessToken);
      print(await secureStorage.readSecureData('accessToken'));
59
60
61
62
63
64
65
66
67
68
69
70
71
      var jsonData = (jsonObject as Map<String, dynamic>)['data'];
      var userInfo = (jsonData as Map<String, dynamic>)['user_info'];
      var userName = (userInfo as Map<String, dynamic>)['name'];
      var userRole = (userInfo as Map<String, dynamic>)['role'];
      var userProfilePic =
          (userInfo as Map<String, dynamic>)['profile_picture'];
      await secureStorage.writeSecureData('userName', userName);
      print('username: ' + await secureStorage.readSecureData('userName'));
      await secureStorage.writeSecureData('userRole', userRole);
      print('userRole: ' + await secureStorage.readSecureData('userRole'));
      await secureStorage.writeSecureData('userProfilePic', userProfilePic);
      print('userProfilePic: ' +
          await secureStorage.readSecureData('userProfilePic'));
72
73
74
75
76
77
    } else if (apiResult.statusCode == 400) {
      print("Failed");
    } else {
      print("Unknown");
    }
  }
78
79

  Future patchMember(String name, int addressId, String phone) async {
80
    var apiURL = Uri.parse('https://canary.fundex.id/api/v1.1/member');
81
82
    var token = await secureStorage.readSecureData('accessToken');
    final apiResult = await client.patch(apiURL,
83
84
85
86
87
88
        headers: <String, String>{
          'Content-type': 'application/json',
          HttpHeaders.authorizationHeader: 'Bearer $token',
        },
        body: jsonEncode(
            {'name': name, 'address_id': addressId, 'phone': phone}));
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    if (apiResult.statusCode == 200) {
      print('Patch member berhasil');
      print(apiResult.statusCode);
      print(apiResult.body);
      return 'Simpan Berhasil!';
    } else {
      print('Patch member ' + apiResult.statusCode.toString());
      print(apiResult.reasonPhrase);
      print(apiResult.request);
      print(apiResult.body);
      throw Exception('Gagal menyimpan..');
    }
  }

103
  Future gender(String gen) async {
104
105
    var apiURL =
        Uri.parse('https://canary.fundex.id/api/v1.1/member-attribute');
106
107
    var token = await secureStorage.readSecureData('accessToken');
    final apiResult = await client.post(apiURL,
108
109
110
111
112
        headers: <String, String>{
          'Content-type': 'application/json',
          HttpHeaders.authorizationHeader: 'Bearer $token',
        },
        body: jsonEncode({'attribute_name': 'gender', 'attribute_value': gen}));
113
114
115
116
117
118
119
    if (apiResult.statusCode == 200) {
      print('gender berhasil');
      print(apiResult.statusCode);
      print(apiResult.body);
      var jsonObject = json.decode(apiResult.body);
      var jsonData = (jsonObject as Map<String, dynamic>)['data'];
      var idMemberAttributeGender = jsonData['id'];
120
      await secureStorage.writeSecureData(
121
          'idMemberAttributeGender', idMemberAttributeGender.toString());
122
123
124
125
126
127
      print(await secureStorage.readSecureData('idMemberAttributeGender'));
    } else {
      print('gender ' + apiResult.statusCode.toString());
      print(apiResult.reasonPhrase);
      print(apiResult.request);
      print(apiResult.body);
128
      throw Exception('Gagal menyimpan gender');
129
130
131
132
    }
  }

  Future birthdate(DateTime date) async {
133
134
    var apiURL =
        Uri.parse('https://canary.fundex.id/api/v1.1/member-attribute');
135
136
    var token = await secureStorage.readSecureData('accessToken');
    final apiResult = await client.post(apiURL,
137
138
139
140
141
142
        headers: <String, String>{
          'Content-type': 'application/json',
          HttpHeaders.authorizationHeader: 'Bearer $token',
        },
        body: jsonEncode(
            {'attribute_name': 'birthdate', 'attribute_value': date}));
143
144
145
146
147
148
149
    if (apiResult.statusCode == 200) {
      print('birthdate berhasil');
      print(apiResult.statusCode);
      print(apiResult.body);
      var jsonObject = json.decode(apiResult.body);
      var jsonData = (jsonObject as Map<String, dynamic>)['data'];
      var idMemberAttributeBirthdate = jsonData['id'];
150
151
      await secureStorage.writeSecureData(
          'idMemberAttributeBirthdate', idMemberAttributeBirthdate);
152
153
154
155
156
157
158
159
160
161
162
163
      print(await secureStorage.readSecureData('idMemberAttributeBirthdate'));
      return 'Simpan Berhasil!';
    } else {
      print('birthdate ' + apiResult.statusCode.toString());
      print(apiResult.reasonPhrase);
      print(apiResult.request);
      print(apiResult.body);
      throw Exception('Gagal menyimpan..');
    }
  }

  Future birthplace(String nameCity) async {
164
165
    var apiURL =
        Uri.parse('https://canary.fundex.id/api/v1.1/member-attribute');
166
167
    var token = await secureStorage.readSecureData('accessToken');
    final apiResult = await client.post(apiURL,
168
169
170
171
172
173
        headers: <String, String>{
          'Content-type': 'application/json',
          HttpHeaders.authorizationHeader: 'Bearer $token',
        },
        body: jsonEncode(
            {'attribute_name': 'birthplace', 'attribute_value': nameCity}));
174
175
176
177
178
179
180
    if (apiResult.statusCode == 200) {
      print('birthplace berhasil');
      print(apiResult.statusCode);
      print(apiResult.body);
      var jsonObject = json.decode(apiResult.body);
      var jsonData = (jsonObject as Map<String, dynamic>)['data'];
      var idMemberAttributeBirthplace = jsonData['id'];
181
182
      await secureStorage.writeSecureData(
          'idMemberAttributeBirthplace', idMemberAttributeBirthplace);
183
184
185
186
187
188
189
      print(await secureStorage.readSecureData('idMemberAttributeBirthplace'));
      return 'Simpan Berhasil!';
    } else {
      print('birthplace ' + apiResult.statusCode.toString());
      print(apiResult.reasonPhrase);
      print(apiResult.request);
      print(apiResult.body);
190
      throw Exception('Gagal menyimpan tempat lahir');
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
    }
  }

  Future getMember() async {
    final apiURL = Uri.parse('https://canary.fundex.id/api/v1.1/member');
    var token = await secureStorage.readSecureData('accessToken');
    var headers = {
      'Content-type': 'application/json',
      HttpHeaders.authorizationHeader: 'Bearer $token',
    };
    var apiResult = await client.get(apiURL, headers: headers);
    print(await secureStorage.readSecureData('accessToken'));
    print(apiResult.reasonPhrase);
    print(apiResult.request);
    print("get member: " + (apiResult.statusCode).toString());
    if (apiResult.statusCode == 200) {
      var jsonObject = json.decode(apiResult.body);
      var jsonData = (jsonObject as Map<String, dynamic>)['data'];
      var jsonMember = (jsonData as Map<String, dynamic>)['member'];
      return jsonMember;
    } else {
      throw Exception('Get Failed');
    }
  }

  Future getRefCity() async {
217
218
    final apiURL = Uri.parse(
        'https://canary.fundex.id/api/v1.1/ref-cities?offset=&limit=');
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
    var token = await secureStorage.readSecureData('accessToken');
    var headers = {
      'Content-type': 'application/json',
      HttpHeaders.authorizationHeader: 'Bearer $token',
    };
    var apiResult = await client.get(apiURL, headers: headers);
    print(await secureStorage.readSecureData('accessToken'));
    print(apiResult.reasonPhrase);
    print(apiResult.request);
    print("get ref cities: " + (apiResult.statusCode).toString());
    if (apiResult.statusCode == 200) {
      var jsonObject = json.decode(apiResult.body);
      var jsonData = (jsonObject as Map<String, dynamic>)['data'];
      List<dynamic> refCities = jsonData['ref_cities'];
      final List<String> city = <String>[];
      for (var i = 0; i < refCities.length; i++) {
        city.add(refCities[i]['name']);
      }
      return city;
    } else {
      throw Exception('Get Failed');
    }
  }

  Future getRefState() async {
244
245
    final apiURL = Uri.parse(
        'https://canary.fundex.id/api/v1.1/ref-states?offset=&limit=');
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    var token = await secureStorage.readSecureData('accessToken');
    var headers = {
      'Content-type': 'application/json',
      HttpHeaders.authorizationHeader: 'Bearer $token',
    };
    var apiResult = await client.get(apiURL, headers: headers);
    print(await secureStorage.readSecureData('accessToken'));
    print(apiResult.reasonPhrase);
    print(apiResult.request);
    print("get ref state: " + (apiResult.statusCode).toString());
    if (apiResult.statusCode == 200) {
      var jsonObject = json.decode(apiResult.body);
      var jsonData = (jsonObject as Map<String, dynamic>)['data'];
      List<dynamic> refCities = jsonData['ref_states'];
      final List<String> state = <String>[];
      for (var i = 0; i < refCities.length; i++) {
        state.add(refCities[i]['name']);
      }
      return state;
    } else {
      throw Exception('Get Failed');
    }
  }

  Future getRefDistrict() async {
271
272
    final apiURL = Uri.parse(
        'https://canary.fundex.id/api/v1.1/ref-districts?offset=&limit=');
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
    var token = await secureStorage.readSecureData('accessToken');
    var headers = {
      'Content-type': 'application/json',
      HttpHeaders.authorizationHeader: 'Bearer $token',
    };
    var apiResult = await client.get(apiURL, headers: headers);
    print(await secureStorage.readSecureData('accessToken'));
    print(apiResult.reasonPhrase);
    print(apiResult.request);
    print("get ref state: " + (apiResult.statusCode).toString());
    if (apiResult.statusCode == 200) {
      var jsonObject = json.decode(apiResult.body);
      var jsonData = (jsonObject as Map<String, dynamic>)['data'];
      List<dynamic> refCities = jsonData['ref_districts'];
      final List<String> distr = <String>[];
      for (var i = 0; i < refCities.length; i++) {
        distr.add(refCities[i]['name']);
      }
      return distr;
    } else {
      throw Exception('Get Failed');
    }
  }

297
298
299
  Future postAddress(String address, String district, String city,
      String province, String zipCode) async {
    var apiURL = Uri.parse('https://canary.fundex.id/api/v1.1/address');
300
301
    var token = await secureStorage.readSecureData('accessToken');
    final apiResult = await client.post(apiURL,
302
303
304
305
306
307
308
309
310
311
312
        headers: <String, String>{
          HttpHeaders.authorizationHeader: 'Bearer $token',
        },
        body: jsonEncode(<String, String>{
          "address": address,
          "district": district,
          "city": city,
          "province": province,
          "country": "Indonesia",
          "zip_code": zipCode
        }));
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
    if (apiResult.statusCode == 200) {
      // var jsonObject = json.decode(apiResult.body);
      // var data = (jsonObject as Map<String, dynamic>)['data'];
      // var name = data['name'];
      // Map<String, dynamic> map = {'name': name};
      print('Patch member berhasil');
      print(apiResult.statusCode);
      print(apiResult.reasonPhrase);
      print(apiResult.request);
      print(apiResult.body);
      return 'Simpan Berhasil!';
    } else {
      print(apiResult.statusCode);
      print(apiResult.reasonPhrase);
      print(apiResult.request);
      print(apiResult.body);
      throw Exception('Gagal menyimpan..');
    }
  }
332
333
334
335
336
337
338
339
340
341

  Future forgetPassword(String email, int statusCode) async {
    final apiURL =
        Uri.parse('https://canary.fundex.id/api/v1.1/auth/forgot-password');
    var body = jsonEncode({'email': email});
    var headers = {'Content-type': 'application/json'};
    var apiResult = await client.post(apiURL, headers: headers, body: body);
    statusCode = apiResult.statusCode;
    return (statusCode);
  }
342

343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
  Future getMemberDocsId() async {
    final apiURL = Uri.parse(
        'https://canary.fundex.id/api/v1.1/member-attributes?attribute_name=agreement_doc_id&offset=&limit=&order_by=');
    var token = await secureStorage.readSecureData('accessToken');
    var headers = {
      'Content-type': 'application/json',
      'Authorization': 'Bearer $token',
    };
    var apiResult = await client.get(apiURL, headers: headers);
    if (apiResult.statusCode == 200) {
      var jsonObject = json.decode(apiResult.body);
      var jsonData = (jsonObject as Map<String, dynamic>)['data'];
      var memberAttributes =
          (jsonData as Map<String, dynamic>)['member_attributes'];
      var idMemberDocs = (memberAttributes as Map<String, dynamic>)['id'];
      await secureStorage.writeSecureData('idMemberDocs', idMemberDocs);
      print(await secureStorage.readSecureData('idMemberDocs'));
    } else {
      throw Exception('Failed to get MemberDocsID');
    }
  }

  Future getMemberDocs(String memberDocsId) async {
    final apiURL = Uri.parse(
        'https://canary.fundex.id/api/v1.1/member-documents/$memberDocsId/private');
    var token = await secureStorage.readSecureData('accessToken');
    var headers = {
      'Content-type': 'application/json',
      'Authorization': 'Bearer $token',
    };
    var apiResult = await client.get(apiURL, headers: headers);
    if (apiResult.statusCode == 200) {
      var jsonObject = json.decode(apiResult.body);
      var jsonData = (jsonObject as Map<String, dynamic>)['data'];
      var memberDocument =
          (jsonData as Map<String, dynamic>)['member_document'];
      var description = (memberDocument as Map<String, dynamic>)['description'];
      var download = (description as Map<String, dynamic>)['download'];
      var expiredAt = (download as Map<String, dynamic>)['expiredAt'];
      var name = (memberDocument as Map<String, dynamic>)['name'];
      var imgUrl = (memberDocument as Map<String, dynamic>)['url'];
      await secureStorage.writeSecureData('memberDocs-expiredAt', expiredAt);
      await secureStorage.writeSecureData('memberDocs-name', name);
      await secureStorage.writeSecureData('memberDocs-imgUrl', imgUrl);
      print(await secureStorage.readSecureData('memberDocs-expiredAt'));
      print(await secureStorage.readSecureData('memberDocs-name'));
      print(await secureStorage.readSecureData('memberDocs-imgUrl'));
      // ISI DESCRIPTION
      //   {
      // "docToken": "bb5e12c77438f60da1f2ab6e566c5aeb6d03561bec3e5d588f7e240e4c164120",
      // "recipients": [{
      //     "privyId": "AB1234",
      //     "type": "Reviewer",
      //     "signatoryStatus": "Completed"
      //   },
      //   {
      //     "privyId": "DE3456",
      //     "type": "Signer",
      //     "signatoryStatus": "Completed"
      //   }
      // ],
      // "documentStatus": "Completed",
      // "urlDocument": "https://sign-sandbox.privy.id/doc/bb5e12c77438f60da1f2ab6e566c5aeb6d03561bec3e5d588f7e240e4c164120",
      // "download": {
      //   "url": "http://api-sandbox.privy.id/document/6S7p0MVgdB-f9a360ab-6d47-41d8-881d-b4492bad49f8",
      //   "expiredAt": "2019-04-25T09:33:58+00:00"
      // }}

    } else {
      throw Exception('Failed to get MemberDocsID');
    }
  }
Habel Christiando Tobing's avatar
Habel Christiando Tobing committed
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464

  Future<EquityOfferings> getSortedEquities(
      String statusOffering, String businessField, String sortBy) async {
    final apiURL = Uri.parse(
        'https://canary.fundex.id/api/v1.1/public/equity-offerings/explore?status_offering=' +
            statusOffering +
            '&business_field=' +
            businessField +
            '&sort_by=' +
            sortBy);
    var token = await secureStorage.readSecureData('accessToken');
    var headers = {
      'Content-type': 'application/json',
      HttpHeaders.authorizationHeader: 'Bearer $token',
    };
    var apiResult = await client.get(apiURL, headers: headers);
    print(await secureStorage.readSecureData('accessToken'));
    print(apiResult.reasonPhrase);
    print(apiResult.request);
    print("get equities: " + (apiResult.statusCode).toString());
    if (apiResult.statusCode == 200) {
      var jsonObject = json.decode(apiResult.body);
      var jsonData = (jsonObject as Map<String, dynamic>)['data'];
      return EquityOfferings.fromJson(jsonData);
    } else {
      throw Exception('Get Failed');
    }
  }

  Future<EquityOfferings> getEquities() async {
    final apiURL = Uri.parse(
        'https://canary.fundex.id/api/v1.1/public/equity-offerings/explore?status_offering=&business_field=&sort_by=');
    var token = await secureStorage.readSecureData('accessToken');
    var headers = {
      'Content-type': 'application/json',
      HttpHeaders.authorizationHeader: 'Bearer $token',
    };
    var apiResult = await client.get(apiURL, headers: headers);
    print(await secureStorage.readSecureData('accessToken'));
    print(apiResult.reasonPhrase);
    print(apiResult.request);
    print("get equities: " + (apiResult.statusCode).toString());
    if (apiResult.statusCode == 200) {
      var jsonObject = json.decode(apiResult.body);
      var jsonData = (jsonObject as Map<String, dynamic>)['data'];
      return EquityOfferings.fromJson(jsonData);
    } else {
      throw Exception('Get Failed');
    }
  }
465
}