2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-28 13:36:50 +00:00

Adds function to get OPTIONS endpoint data

This commit is contained in:
Oliver 2021-07-14 17:00:47 +10:00
parent fad7840c5c
commit aea0036881

View File

@ -2,6 +2,8 @@ import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:intl/intl.dart';
import 'package:InvenTree/inventree/sentry.dart'; import 'package:InvenTree/inventree/sentry.dart';
import 'package:InvenTree/user_profile.dart'; import 'package:InvenTree/user_profile.dart';
import 'package:InvenTree/widget/snacks.dart'; import 'package:InvenTree/widget/snacks.dart';
@ -472,6 +474,7 @@ class InvenTreeAPI {
// Set headers // Set headers
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('Accept-Language', Intl.getCurrentLocale());
request.headers.set('Content-Length', data.length.toString()); request.headers.set('Content-Length', data.length.toString());
request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader()); request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader());
@ -570,6 +573,54 @@ class InvenTreeAPI {
return response; return response;
} }
/**
* Perform a HTTP OPTIONS request,
* to get the available fields at a given endpoint.
* We send this with the currently selected "locale",
* so that (hopefully) the field messages are correctly translated
*/
Future<dynamic> options(String url) async {
var _url = makeApiUrl(url);
var client = createClient(true);
final uri = Uri.parse(_url);
if (uri.host.isEmpty) {
showServerError(L10().invalidHost, L10().invalidHostDetails);
return null;
}
HttpClientRequest? request;
HttpClientResponse? response;
try {
request = await client.openUrl("OPTIONS", uri).timeout(Duration(seconds: 10));
request.headers.set('Accept', 'application/json');
request.headers.set('Accept-Language', Intl.getCurrentLocale());
request.headers.set('Content-type', 'application/json');
request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader());
response = await request.close().timeout(Duration(seconds: 10));
} on SocketException catch (error) {
showServerError(L10().connectionRefused, error.toString());
return null;
} on TimeoutException catch (error) {
showTimeoutError();
return null;
} catch (error, stackTrace) {
showServerError(L10().serverError, error.toString());
sentryReportError(error, stackTrace);
return null;
}
var responseData = await responseToJson(response);
return responseData;
}
/** /**
* Perform a HTTP POST request * Perform a HTTP POST request
* Returns a json object (or null if unsuccessful) * Returns a json object (or null if unsuccessful)