public async Task <TaskModels <R> > WaitForTaskComplete <R>(string taskId) { string url = ApiEndpoint + @"/v2/tasks/" + taskId; TaskModels <R> taskModel = new TaskModels <R>(); bool completed = false; do { var response = await WebRequests.GetAsync(url, AccessToken); if (response.StatusCode == System.Net.HttpStatusCode.OK) { taskModel = await RetrieveTask <R>(response); } if (taskModel.data?.status == "error" || taskModel.data?.status == "finished") { completed = true; } else { await Task.Delay(TimeSpan.FromSeconds(1)); } } while (!completed); return(taskModel); }
private async Task UseControllerToEnterApp(string controllerName, string userName, string passWord) { try { // First we send a request to the server to the Register Controller with our // Desired username and password // We should move this in the register method of the authentication class string serializedUser = await WebRequests.GetAsync(controllerName, new System.Collections.Generic.Dictionary <string, string> { { "username", userName }, { "password", passWord } }); // Now we need to deserialize the userdata User currentUser = new User(serializedUser); // First we set the authenticated user because our DB connection relies on IT Settings.AuthenticatedUserID = currentUser.UserID; // Now we can move forward to the Main Page await DataAccessLayer.Database.Instance.AddAuthenticatedUser(currentUser); // navigate to the application page await App.NavigationMethod.PushAsync(new MainPage()); } catch (Exception ex) { // We may want to check that await DisplayAlert("Error", "There was an error, please try again!", "OK"); } }
/// <summary> /// Default Constructor /// </summary> /// <param name="endpoint"></param> /// <param name="interval"></param> /// <param name="stateChangedCallback"></param> /// <param name="validResponseParser"></param> /// <param name="logger"></param> public HttpEndPointChecker(string endpoint, int interval, Action <bool> stateChangedCallback, Func <HttpWebResponse, Exception, bool> validResponseParser = null, ILogger logger = null) { //Set the endpoint Endpoint = endpoint; //Store callback mStateChangedCallback = stateChangedCallback; logger?.LogTraceSource($"HttpEndpointChacker started for {endpoint}"); // Start task Task.Run(async() => { while (!mDisposing) { //Create defaults var webResponse = default(HttpWebResponse); var exception = default(Exception); // Start by calling the endpoint try { //Log it logger?.LogTraceSource($"HttpEndpointChecker fetching {endpoint}"); // // By default, presume any response that doesn't throw // (so the server replied, even if its 401 for example // meaning the server we hit up actually responded // with something even if it was a page not found or server // error. // // The user is free to override this default behavior // webResponse = await WebRequests.GetAsync(endpoint); } catch (Exception ex) { exception = ex; } // Figure out the new state // - If we have a custom parser, ask it for the state based on the response // - Otherwise, so long as we have a response of any kind, it's valid var responsive = validResponseParser?.Invoke(webResponse, exception) ?? webResponse != null; //Close web response webResponse?.Close(); //Log it logger.LogTraceSource($"HttpEndpointChecker {endpoint} { (responsive ? "is" : "is not")} responsive"); // If the state has changed... if (responsive != Responsive) { // Set new value Responsive = responsive; // Inform listener mStateChangedCallback?.Invoke(responsive); } // if not disposing, wait interval.. then poll again if (!mDisposing) { await Task.Delay(interval); } } }); }