This commit is contained in:
Hillel Coren 2019-10-05 21:38:03 +03:00
parent ddf550a963
commit 2ab1785968
13 changed files with 61 additions and 34 deletions

View File

@ -61,7 +61,8 @@ class ClientRepository {
if (action != null) { if (action != null) {
url += '&action=' + action.toString(); url += '&action=' + action.toString();
} }
response = await webClient.put(url, credentials.token, json.encode(data)); response =
await webClient.put(url, credentials.token, data: json.encode(data));
} }
final ClientItemResponse clientResponse = final ClientItemResponse clientResponse =

View File

@ -68,8 +68,8 @@ class DocumentRepository {
if (action != null) { if (action != null) {
url += '?action=' + action.toString(); url += '?action=' + action.toString();
} }
response = response = await webClient.put(url, credentials.token,
await webClient.put(url, credentials.token, json.encode(data)); data: json.encode(data));
} }
} }

View File

@ -56,7 +56,8 @@ class ExpenseRepository {
if (action != null) { if (action != null) {
url += '?action=' + action.toString(); url += '?action=' + action.toString();
} }
response = await webClient.put(url, credentials.token, json.encode(data)); response =
await webClient.put(url, credentials.token, data: json.encode(data));
} }
final ExpenseItemResponse expenseResponse = final ExpenseItemResponse expenseResponse =

View File

@ -58,7 +58,8 @@ class InvoiceRepository {
if (action != null) { if (action != null) {
url += '?action=' + action.toString(); url += '?action=' + action.toString();
} }
response = await webClient.put(url, credentials.token, json.encode(data)); response =
await webClient.put(url, credentials.token, data: json.encode(data));
} }
final InvoiceItemResponse invoiceResponse = final InvoiceItemResponse invoiceResponse =

View File

@ -51,7 +51,8 @@ class PaymentRepository {
if (action != null) { if (action != null) {
url += '&action=' + action.toString(); url += '&action=' + action.toString();
} }
response = await webClient.put(url, credentials.token, json.encode(data)); response =
await webClient.put(url, credentials.token, data: json.encode(data));
} }
final PaymentItemResponse paymentResponse = final PaymentItemResponse paymentResponse =

View File

@ -45,7 +45,8 @@ class ProductRepository {
if (action != null) { if (action != null) {
url += '?action=' + action.toString(); url += '?action=' + action.toString();
} }
response = await webClient.put(url, credentials.token, json.encode(data)); response =
await webClient.put(url, credentials.token, data: json.encode(data));
} }
final ProductItemResponse productResponse = final ProductItemResponse productResponse =

View File

@ -56,7 +56,8 @@ class ProjectRepository {
if (action != null) { if (action != null) {
url += '?action=' + action.toString(); url += '?action=' + action.toString();
} }
response = await webClient.put(url, credentials.token, json.encode(data)); response =
await webClient.put(url, credentials.token, data: json.encode(data));
} }
final ProjectItemResponse projectResponse = final ProjectItemResponse projectResponse =

View File

@ -58,7 +58,8 @@ class QuoteRepository {
if (action != null) { if (action != null) {
url += '?action=' + action.toString(); url += '?action=' + action.toString();
} }
response = await webClient.put(url, credentials.token, json.encode(data)); response =
await webClient.put(url, credentials.token, data: json.encode(data));
} }
final InvoiceItemResponse quoteResponse = final InvoiceItemResponse quoteResponse =

View File

@ -21,7 +21,8 @@ class SettingsRepository {
dynamic response; dynamic response;
final url = credentials.url + '/companies/${company.id}'; final url = credentials.url + '/companies/${company.id}';
response = await webClient.put(url, credentials.token, json.encode(data)); response =
await webClient.put(url, credentials.token, data: json.encode(data));
final LoginResponse clientResponse = final LoginResponse clientResponse =
serializers.deserializeWith(LoginResponse.serializer, response); serializers.deserializeWith(LoginResponse.serializer, response);
@ -34,7 +35,8 @@ class SettingsRepository {
dynamic response; dynamic response;
final url = credentials.url + '/users/${user.id}'; final url = credentials.url + '/users/${user.id}';
response = await webClient.put(url, credentials.token, json.encode(data)); response =
await webClient.put(url, credentials.token, data: json.encode(data));
final UserItemResponse userResponse = final UserItemResponse userResponse =
serializers.deserializeWith(UserItemResponse.serializer, response); serializers.deserializeWith(UserItemResponse.serializer, response);
@ -46,7 +48,9 @@ class SettingsRepository {
final url = '${credentials.url}/companies'; final url = '${credentials.url}/companies';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: {'name': 'logo'}, filePath: path, fileIndex: 'logo'); filePath: path,
fileIndex: 'logo',
data: {'_method': 'PUT', 'name': 'TEST'});
debugPrint('### UPLOAD LOGO RESPONSE: $response'); debugPrint('### UPLOAD LOGO RESPONSE: $response');

View File

@ -61,7 +61,8 @@ class TaskRepository {
if (action != null) { if (action != null) {
url += '?action=' + action.toString(); url += '?action=' + action.toString();
} }
response = await webClient.put(url, credentials.token, json.encode(data)); response =
await webClient.put(url, credentials.token, data: json.encode(data));
} }
final TaskItemResponse taskResponse = final TaskItemResponse taskResponse =

View File

@ -56,7 +56,8 @@ class VendorRepository {
if (action != null) { if (action != null) {
url += '?action=' + action.toString(); url += '?action=' + action.toString();
} }
response = await webClient.put(url, credentials.token, json.encode(data)); response =
await webClient.put(url, credentials.token, data: json.encode(data));
} }
final VendorItemResponse vendorResponse = final VendorItemResponse vendorResponse =

View File

@ -36,25 +36,15 @@ class WebClient {
} }
Future<dynamic> post(String url, String token, Future<dynamic> post(String url, String token,
{dynamic data, String filePath, String fileIndex = 'file'}) async { {dynamic data, String filePath, String fileIndex}) async {
url = _checkUrl(url); url = _checkUrl(url);
print('POST: $url'); print('POST: $url');
print('Data: $data'); print('Data: $data');
http.Response response; http.Response response;
if (filePath != null) { if (filePath != null) {
final file = File(filePath); response = await _uploadFile(url, token, filePath,
final stream = http.ByteStream(DelegatingStream.typed(file.openRead())); fileIndex: fileIndex, data: data);
final length = await file.length();
final request = http.MultipartRequest('POST', Uri.parse(url))
..fields.addAll(data ?? {})
..headers.addAll(_getHeaders(token))
..files.add(http.MultipartFile(fileIndex, stream, length,
filename: basename(file.path)));
response = await http.Response.fromStream(await request.send())
.timeout(const Duration(minutes: 10));
} else { } else {
response = await http.Client() response = await http.Client()
.post(url, body: data, headers: _getHeaders(token)) .post(url, body: data, headers: _getHeaders(token))
@ -66,16 +56,24 @@ class WebClient {
return json.decode(response.body); return json.decode(response.body);
} }
Future<dynamic> put(String url, String token, dynamic data) async { Future<dynamic> put(String url, String token,
{dynamic data, String filePath, String fileIndex = 'file'}) async {
url = _checkUrl(url); url = _checkUrl(url);
print('PUT: $url'); print('PUT: $url');
print('Data: $data'); print('Data: $data');
final http.Response response = await http.Client().put( http.Response response;
url,
body: data, if (filePath != null) {
headers: _getHeaders(token), response = await _uploadFile(url, token, filePath,
); fileIndex: fileIndex, data: data, method: 'PUT');
} else {
response = await http.Client().put(
url,
body: data,
headers: _getHeaders(token),
);
}
_checkResponse(response); _checkResponse(response);
@ -180,3 +178,19 @@ bool _isVersionSupported(String version) {
minor >= kMinMinorAppVersion && minor >= kMinMinorAppVersion &&
patch >= kMinPatchAppVersion; patch >= kMinPatchAppVersion;
} }
Future<http.Response> _uploadFile(String url, String token, String filePath,
{String method = 'POST', String fileIndex = 'file', dynamic data}) async {
final file = File(filePath);
final stream = http.ByteStream(DelegatingStream.typed(file.openRead()));
final length = await file.length();
final request = http.MultipartRequest(method, Uri.parse(url))
..fields.addAll(data ?? {})
..headers.addAll(_getHeaders(token))
..files.add(http.MultipartFile(fileIndex, stream, length,
filename: basename(file.path)));
return await http.Response.fromStream(await request.send())
.timeout(const Duration(minutes: 10));
}

View File

@ -59,7 +59,7 @@ class StubRepository {
if (action != null) { if (action != null) {
url += '?action=' + action.toString(); url += '?action=' + action.toString();
} }
response = await webClient.put(url, credentials.token, json.encode(data)); response = await webClient.put(url, credentials.token, data: json.encode(data));
} }
final StubItemResponse stubResponse = final StubItemResponse stubResponse =