-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_consumer.dart
56 lines (37 loc) · 1.08 KB
/
api_consumer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import 'dart:typed_data';
import 'package:http/http.dart' as http;
import 'package:shivangi/global.dart';
class ApiConsumer
{
Future<Uint8List> removeImageBackgroundApi(String imagePath) async
{
//request
var requestApi = http.MultipartRequest(
"POST",
Uri.parse("https://api.remove.bg/v1.0/removebg")
);
//which img file to send - define
requestApi.files.add(
await http.MultipartFile.fromPath(
"image_file",
imagePath
)
);
//API header communicate with help of API key
requestApi.headers.addAll(
{
"X-API-Key": apiKeyRemoveImageBackground
});
//send request and receive response
final responseFromApi = await requestApi.send();
if(responseFromApi.statusCode == 200)
{
http.Response getTransparentImageFromResponse = await http.Response.fromStream(responseFromApi);
return getTransparentImageFromResponse.bodyBytes;
}
else
{
throw Exception("Error Occured:: " + responseFromApi.statusCode.toString());
}
}
}