void ApiRequest <T>(string endpoint, Action <RestRequest> arguments, Action <T> onSuccess, Action <string> onError) { var client = new RestClient(GhostVREndpoint); var request = new RestRequest(endpoint, Method.POST); if (Token.HasValue) { request.AddHeader("Authorization", "Bearer " + Token.Value); } arguments(request); logger.Info($"API request: {endpoint} {request} token={(Token.HasValue ? Token.Value.ToString() : "(none)")}"); client.ExecuteAsync(request, (response, request_) => { try { if (response.ErrorException != null) { logger.Error("Cannot connect to GhostVR API: " + response.ErrorMessage); onError("Cannot connect to GhostVR API: " + response.ErrorMessage); potentialAuthError = true; return; } ApiResponse apiResponse = JsonConvert.DeserializeObject <ApiResponse>(response.Content); if (apiResponse.status == ApiResponse.ApiStatus.success) { ApiResponseSuccess <T> apiResponseSuccess = JsonConvert.DeserializeObject <ApiResponseSuccess <T> >(response.Content); logger.Info("ApiRequest success: " + endpoint); onSuccess(apiResponseSuccess.data); } else { ApiResponseError apiResponseError = JsonConvert.DeserializeObject <ApiResponseError>(response.Content); logger.Error($"ApiRequest error: {endpoint}, {apiResponseError.status} {apiResponseError.message} ({apiResponseError.code})"); onError($"{apiResponseError.status} {apiResponseError.message} ({apiResponseError.code})"); potentialAuthError = true; } } catch (Exception e) { logger.Error("ApiRequest parse error:" + endpoint + e); onError("an exception occurred: " + e); potentialAuthError = true; } }); }
public ApiResponse(Exception exception) { Error = new ApiResponseError(exception); }