mirror of
				https://github.com/inventree/inventree-app.git
				synced 2025-11-04 07:15:46 +00:00 
			
		
		
		
	Updated PATCH request
This commit is contained in:
		
							
								
								
									
										85
									
								
								lib/api.dart
									
									
									
									
									
								
							
							
						
						
									
										85
									
								
								lib/api.dart
									
									
									
									
									
								
							@@ -348,9 +348,8 @@ class InvenTreeAPI {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Perform a PATCH request
 | 
					  // Perform a PATCH request
 | 
				
			||||||
  Future<http.Response> patch(String url, {Map<String, String> body}) async {
 | 
					  Future<dynamic> patch(String url, {Map<String, String> body, int expectedStatusCode=200}) async {
 | 
				
			||||||
    var _url = makeApiUrl(url);
 | 
					    var _url = makeApiUrl(url);
 | 
				
			||||||
    var _headers = defaultHeaders();
 | 
					 | 
				
			||||||
    var _body = Map<String, String>();
 | 
					    var _body = Map<String, String>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Copy across provided data
 | 
					    // Copy across provided data
 | 
				
			||||||
@@ -358,10 +357,59 @@ class InvenTreeAPI {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    print("PATCH: " + _url);
 | 
					    print("PATCH: " + _url);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return http.patch(_url,
 | 
					    var client = createClient(true);
 | 
				
			||||||
      headers: _headers,
 | 
					
 | 
				
			||||||
      body: _body,
 | 
					    HttpClientRequest request = await client.patchUrl(Uri.parse(_url));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    var data = json.encode(body);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Set headers
 | 
				
			||||||
 | 
					    request.headers.set('Accept', 'application/json');
 | 
				
			||||||
 | 
					    request.headers.set('Content-type', 'application/json');
 | 
				
			||||||
 | 
					    request.headers.set('Content-Length', data.length.toString());
 | 
				
			||||||
 | 
					    request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request.add(utf8.encode(data));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    HttpClientResponse response = await request.close()
 | 
				
			||||||
 | 
					    .timeout(Duration(seconds: 30))
 | 
				
			||||||
 | 
					    .catchError((error) {
 | 
				
			||||||
 | 
					      print("PATCH request returned error");
 | 
				
			||||||
 | 
					      print("URL: ${_url}");
 | 
				
			||||||
 | 
					      print("Error: ${error.toString()}");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      var ctx = OneContext().context;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if (error is SocketException) {
 | 
				
			||||||
 | 
					        showServerError(
 | 
				
			||||||
 | 
					            I18N
 | 
				
			||||||
 | 
					                .of(ctx)
 | 
				
			||||||
 | 
					                .connectionRefused,
 | 
				
			||||||
 | 
					            error.toString()
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
 | 
					      } else if (error is TimeoutException) {
 | 
				
			||||||
 | 
					        showTimeoutError(ctx);
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        showServerError(
 | 
				
			||||||
 | 
					            I18N
 | 
				
			||||||
 | 
					                .of(ctx)
 | 
				
			||||||
 | 
					                .serverError,
 | 
				
			||||||
 | 
					            error.toString()
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      return null;
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (response == null) {
 | 
				
			||||||
 | 
					      print("null response from PATCH ${_url}");
 | 
				
			||||||
 | 
					      return null;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (response.statusCode != expectedStatusCode) {
 | 
				
			||||||
 | 
					      showStatusCodeError(response.statusCode);
 | 
				
			||||||
 | 
					      return null;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /*
 | 
					  /*
 | 
				
			||||||
@@ -394,7 +442,6 @@ class InvenTreeAPI {
 | 
				
			|||||||
   */
 | 
					   */
 | 
				
			||||||
  Future<dynamic> post(String url, {Map<String, dynamic> body, int expectedStatusCode=201}) async {
 | 
					  Future<dynamic> post(String url, {Map<String, dynamic> body, int expectedStatusCode=201}) async {
 | 
				
			||||||
    var _url = makeApiUrl(url);
 | 
					    var _url = makeApiUrl(url);
 | 
				
			||||||
    var _headers = jsonHeaders();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    print("POST: ${_url} -> ${body.toString()}");
 | 
					    print("POST: ${_url} -> ${body.toString()}");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -409,13 +456,7 @@ class InvenTreeAPI {
 | 
				
			|||||||
    request.headers.set('Accept', 'application/json');
 | 
					    request.headers.set('Accept', 'application/json');
 | 
				
			||||||
    request.headers.set('Content-type', 'application/json');
 | 
					    request.headers.set('Content-type', 'application/json');
 | 
				
			||||||
    request.headers.set('Content-Length', data.length.toString());
 | 
					    request.headers.set('Content-Length', data.length.toString());
 | 
				
			||||||
 | 
					    request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader());
 | 
				
			||||||
    if (profile != null) {
 | 
					 | 
				
			||||||
      request.headers.set(
 | 
					 | 
				
			||||||
        HttpHeaders.authorizationHeader,
 | 
					 | 
				
			||||||
        _authorizationHeader(profile.username, profile.password)
 | 
					 | 
				
			||||||
      );
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Add JSON data to the request
 | 
					    // Add JSON data to the request
 | 
				
			||||||
    request.add(utf8.encode(data));
 | 
					    request.add(utf8.encode(data));
 | 
				
			||||||
@@ -509,7 +550,6 @@ class InvenTreeAPI {
 | 
				
			|||||||
   */
 | 
					   */
 | 
				
			||||||
  Future<dynamic> get(String url, {Map<String, String> params, int expectedStatusCode=200}) async {
 | 
					  Future<dynamic> get(String url, {Map<String, String> params, int expectedStatusCode=200}) async {
 | 
				
			||||||
    var _url = makeApiUrl(url);
 | 
					    var _url = makeApiUrl(url);
 | 
				
			||||||
    var _headers = defaultHeaders();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    print("GET: ${_url}");
 | 
					    print("GET: ${_url}");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -535,12 +575,7 @@ class InvenTreeAPI {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // Set headers
 | 
					    // Set headers
 | 
				
			||||||
    request.headers.set(HttpHeaders.contentTypeHeader, 'application/json');
 | 
					    request.headers.set(HttpHeaders.contentTypeHeader, 'application/json');
 | 
				
			||||||
    if (profile != null) {
 | 
					    request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader());
 | 
				
			||||||
      request.headers.set(
 | 
					 | 
				
			||||||
          HttpHeaders.authorizationHeader,
 | 
					 | 
				
			||||||
          _authorizationHeader(profile.username, profile.password)
 | 
					 | 
				
			||||||
      );
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    HttpClientResponse response = await request.close()
 | 
					    HttpClientResponse response = await request.close()
 | 
				
			||||||
    .timeout(Duration(seconds: 30))
 | 
					    .timeout(Duration(seconds: 30))
 | 
				
			||||||
@@ -604,9 +639,7 @@ class InvenTreeAPI {
 | 
				
			|||||||
  Map<String, String> defaultHeaders() {
 | 
					  Map<String, String> defaultHeaders() {
 | 
				
			||||||
    var headers = Map<String, String>();
 | 
					    var headers = Map<String, String>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (profile != null) {
 | 
					    headers[HttpHeaders.authorizationHeader] = _authorizationHeader();
 | 
				
			||||||
      headers[HttpHeaders.authorizationHeader] = _authorizationHeader(profile.username, profile.password);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return headers;
 | 
					    return headers;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@@ -617,11 +650,13 @@ class InvenTreeAPI {
 | 
				
			|||||||
    return headers;
 | 
					    return headers;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  String _authorizationHeader(String username, String password) {
 | 
					  String _authorizationHeader() {
 | 
				
			||||||
    if (_token.isNotEmpty) {
 | 
					    if (_token.isNotEmpty) {
 | 
				
			||||||
      return "Token $_token";
 | 
					      return "Token $_token";
 | 
				
			||||||
 | 
					    } else if (profile != null) {
 | 
				
			||||||
 | 
					      return "Basic " + base64Encode(utf8.encode('${profile.username}:${profile.password}'));
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      return "Basic " + base64Encode(utf8.encode('${username}:${password}'));
 | 
					      return "";
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -186,34 +186,15 @@ class InvenTreeModel {
 | 
				
			|||||||
      addr += "/";
 | 
					      addr += "/";
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var response = await api.patch(addr, body: values)
 | 
					    var response = await api.patch(
 | 
				
			||||||
        .timeout(Duration(seconds: 10))
 | 
					      addr,
 | 
				
			||||||
        .catchError((e) {
 | 
					      body: values,
 | 
				
			||||||
 | 
					      expectedStatusCode: 200
 | 
				
			||||||
          if (e is SocketException) {
 | 
					 | 
				
			||||||
            showServerError(
 | 
					 | 
				
			||||||
              I18N.of(context).connectionRefused,
 | 
					 | 
				
			||||||
              e.toString()
 | 
					 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
          } else if (e is TimeoutException) {
 | 
					 | 
				
			||||||
            showTimeoutError(context);
 | 
					 | 
				
			||||||
          } else {
 | 
					 | 
				
			||||||
            // Re-throw the error
 | 
					 | 
				
			||||||
            throw e;
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
          return null;
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (response == null) return false;
 | 
					    if (response == null) return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (response.statusCode != 200) {
 | 
					 | 
				
			||||||
      showStatusCodeError(response.statusCode);
 | 
					 | 
				
			||||||
      return false;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return true;
 | 
					    return true;
 | 
				
			||||||
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Return the detail view for the associated pk
 | 
					  // Return the detail view for the associated pk
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								lib/l10n
									
									
									
									
									
								
							
							
								
								
								
								
								
							
						
						
									
										2
									
								
								lib/l10n
									
									
									
									
									
								
							 Submodule lib/l10n updated: 2fe34df1dd...385c15b1e3
									
								
							
		Reference in New Issue
	
	Block a user