Ejemplo n.º 1
0
        /// <summary>Submits a POST request. Returns null on success, otherwise returns error information.</summary>
        /// <param name="baseUrl">Root URL for the API.</param>
        /// <param name="tableName">The table name to add to the end of the baseUrl.</param>
        /// <param name="data">The data payload to post.</param>
        /// <param name="cred">Authentication credentials if needed.</param>
        /// <param name="canceller">A object that can be sued to canel the request. </param>
        /// <returns>Null on success otherwise an error message.</returns>
        public static async Task <string> PostTable([NotNull] string baseUrl, [NotNull] string tableName,
                                                    [NotNull] string data, [CanBeNull] Creds cred = null, [CanBeNull] CancellationToken?canceller = null)
        {
            if (!IsInternetAvailable())
            {
                //Responses starting with "Error:" should be filtered as failures.
                return("Error: No Internet, post request aborted.");
            }

            // Must add the AllowUI=false setting otherwise it tries enumerating UI and doesn't report errors properly.
            var client = new HttpClient(new HttpBaseProtocolFilter {
                AllowUI = false
            });
            var tokenHeader = "X-ZUMO-AUTH";
            var headers     = client.DefaultRequestHeaders;

            if (cred != null)
            {
                headers.Add(tokenHeader, cred.Token);
            }

            var url = UrlCombiner.CombineAsSeparateElements(baseUrl, tableName);

            if (canceller == null)
            {
                var cs = new CancellationTokenSource(TimeSpan.FromSeconds(DefaultTimeout));
                canceller = cs.Token;
            }


            try
            {
                var          buffUtf8 = CryptographicBuffer.ConvertStringToBinary(data, BinaryStringEncoding.Utf8);
                IHttpContent content  = new HttpBufferContent(buffUtf8, 0, buffUtf8.Length);
                var          body     = await content.ReadAsStringAsync();

                content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/json");

                var response = await client.PostAsync(url, content).AsTask((CancellationToken)canceller);

                var it = response.Content;
                return(response.IsSuccessStatusCode
                    ? null
                    : $"Error: Request returned {response.StatusCode} ({response.ReasonPhrase}), Content:{it}");
            }
            catch (TaskCanceledException)
            {
                Debug.WriteLine($"Req '{tableName}' cancelled ot timedout.");
                return("Error: Cancelled or timed out.");
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Req '{tableName}' error: {ex}");
                return($"Error: NetFail: {ex.Message}, {ex.InnerException}, END");
            }
        }
Ejemplo n.º 2
0
        /// <summary>Fetches a table, catches errors either returns the resonse or a message starting with
        /// "Error". A valid message will be Json starting enad ending with either [] or {}.</summary>
        /// <param name="baseUrl">Base url of the request API.</param>
        /// <param name="tableName">The name of the table to put on the end of the baseUrl.</param>
        /// <param name="cred">A credentials object used to authenticate the request, optional.</param>
        /// <param name="canceller">A token that can be used to cancell the request.</param>
        /// <returns>The response or an error message starting with "Error:"</returns>
        public static async Task <string> GetTable([NotNull] string baseUrl, [NotNull] string tableName,
                                                   [CanBeNull] Creds cred = null, [CanBeNull] CancellationToken?canceller = null)
        {
            if (!IsInternetAvailable())
            {
                //Responses starting with "Error:" should be filtered as failures.
                return("Error: No Internet, get request aborted.");
            }

            // Must add the AllowUI=false setting otherwise it tries enumerating UI and doesn't report errors properly.
            var client = new HttpClient(new HttpBaseProtocolFilter {
                AllowUI = false
            });
            var tokenHeader = "X-ZUMO-AUTH";
            var headers     = client.DefaultRequestHeaders;

            if (cred != null)
            {
                headers.Add(tokenHeader, cred.Token);
            }

            var url = UrlCombiner.CombineAsSeparateElements(baseUrl, tableName);

            if (canceller == null)
            {
                var cs = new CancellationTokenSource(TimeSpan.FromSeconds(DefaultTimeout));
                canceller = cs.Token;
            }

            try
            {
                var response = await client.GetStringAsync(url).AsTask((CancellationToken)canceller);

                return(response);
            }
            catch (TaskCanceledException)
            {
                Debug.WriteLine($"Req '{tableName}' cancelled ot timedout.");
                return("Error: Cancelled or timed out.");
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Req '{tableName}' error: {ex}");
                return($"Error: NetFail: {ex.Message}, {ex.InnerException}, END");
            }
        }
Ejemplo n.º 3
0
        private async Task <bool> TryConnect()
        {
            if (SettingsService.Instance.IsLoggedIn == false)
            {
                return(false);
            }

            _webSocket = new MessageWebSocket();
            _webSocket.Control.MessageType = SocketMessageType.Utf8;
            _webSocket.Closed          += SocketClosed;
            _webSocket.MessageReceived += MessageRecieved;


            var tokenHeader = "X-ZUMO-AUTH";
            var creds       = Creds.FromUserIdAndToken(SettingsService.Instance.CredUserId, SettingsService.Instance.CredToken);

            _webSocket.SetRequestHeader(tokenHeader, creds.Token);


            try
            {
                var uri = new Uri("wss://greenhouseapi.azurewebsites.net/api/Websocket");
                await _webSocket.ConnectAsync(uri);

                _messageWriter       = new DataWriter(_webSocket.OutputStream);
                _reconnectionAttempt = 0;
                LoggingService.LogInfo("Websocket connected", Windows.Foundation.Diagnostics.LoggingLevel.Information);
                return(true);
            }
            catch (Exception ex)
            {
                var error = WebSocketError.GetStatus(ex.HResult);
                if (error == Windows.Web.WebErrorStatus.CannotConnect)
                {
                    LoggingService.LogInfo($"Websocket cannot connect", Windows.Foundation.Diagnostics.LoggingLevel.Information);
                }
                else
                {
                    LoggingService.LogInfo($"Websocket connection failed due to {error}, full exception: {ex.ToString()}", Windows.Foundation.Diagnostics.LoggingLevel.Warning);
                }
                _reconnectionAttempt++;
                CleanUp();
                return(false);
            }
        }
Ejemplo n.º 4
0
        private async Task <bool> TryConnect()
        {
            if (Settings.Instance.IsLoggedIn == false)
            {
                return(false);
            }

            _webSocket = new MessageWebSocket();
            _webSocket.Control.MessageType = SocketMessageType.Utf8;
            _webSocket.Closed          += SocketClosed;
            _webSocket.MessageReceived += MessageRecieved;


            var tokenHeader = "X-ZUMO-AUTH";
            var creds       = Creds.FromUserIdAndToken(Settings.Instance.CredUserId, Settings.Instance.CredToken);

            _webSocket.SetRequestHeader(tokenHeader, creds.Token);


            try
            {
                var uri = new Uri("wss://greenhouseapi.azurewebsites.net/api/Websocket");
                await _webSocket.ConnectAsync(uri);

                _messageWriter       = new DataWriter(_webSocket.OutputStream);
                _reconnectionAttempt = 0;
                Debug.WriteLine("Websocket connected");
                return(true);
            }
            catch
            {
                Debug.WriteLine("Websocket connection failed");
                _reconnectionAttempt++;
                CleanUp();
                return(false);
            }
        }