Пример #1
0
        /// <summary>
        /// Makes an API call to the base URL defined in AppData.cs using the GET method.
        /// </summary>
        /// <param name="url">The API function to hit.</param>
        /// <param name="jsonString">Any necesary data required to make the call.</param>
        /// <returns>The string response returned from the API call.</returns>
        public static async Task <string> MakeApiCallGet(string url, bool showDialog)
        {
            if (!ConnectedToInternet())
            {
                if (showDialog)
                {
                    APIExceptionDialog.ShowNoInternetConnectionDialog();
                }
                return(NO_CONNECTION);
            }
            var httpClient         = new HttpClient();
            Uri uri                = new Uri(URL_BASE + url);
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri);

            httpRequestMessage.Headers.Add("hudl-authtoken", ApplicationData.Current.RoamingSettings.Values["hudl-authtoken"].ToString());
            httpRequestMessage.Headers.Add("User-Agent", "HudlWin8/1.0.0");
            var response = await httpClient.SendAsync(httpRequestMessage);

            if (showDialog)
            {
                if (!response.IsSuccessStatusCode)
                {
                    APIExceptionDialog.ShowStatusCodeExceptionDialog(response.StatusCode.ToString(), uri.ToString());
                    return(null);
                }
            }

            return(await response.Content.ReadAsStringAsync());
        }
Пример #2
0
        /// <summary>
        /// Makes an API call to the base URL defined in AppData.cs using the POST method.
        /// </summary>
        /// <param name="url">The API function to hit.</param>
        /// <param name="jsonString">Any necesary data required to make the call.</param>
        /// <returns>The string response returned from the API call.</returns>
        public static async Task <string> MakeApiCallPost(string url, string jsonString, bool showDialog)
        {
            var httpClient         = new HttpClient();
            Uri uri                = new Uri(URL_BASE_SECURE + url);
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, uri);

            httpRequestMessage.Headers.Add("User-Agent", "HudlWin8/1.0.0");
            httpRequestMessage.Content = new StringContent(jsonString);
            httpRequestMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            var response = await httpClient.SendAsync(httpRequestMessage);

            //response.StatusCode 404 500 401
            if (!response.IsSuccessStatusCode)
            {
                APIExceptionDialog.ShowStatusCodeExceptionDialog(response.StatusCode.ToString(), uri.ToString());
                return(null);
            }
            return(await response.Content.ReadAsStringAsync());
        }