Example #1
0
        /*/// <summary>
         * /// Async method that allows you to send arbitrary strings to server
         * /// </summary>
         * /// <param name="value">The arbitrary string to be sent to the server</param>
         * /// <param name="connectedToInternet">Flag for whether an internet connection is present. A <exception cref="NotConnectedToInternetException">NotConnectedToInternetException exception is throw if value is false</exception></param>
         * /// <returns>ServerResponseObjectsBase on success or null on failure</returns>
         * [Obsolete("ServerResponseObjectsBase is an unneeded class and will be removed, as will this method that has a dependancy on it. Use MakeGetCallAsync instead")]
         * protected async Task<ServerResponseObjectsBase> GetStringAsync(string value, bool connectedToInternet)
         * {
         *  string url = string.Format(GetCallsBasePath, value);
         *  var client = DefaultClient;
         *  foreach (KeyValuePair<string, string> headers in extraHeaders)
         *  {
         *      client.DefaultRequestHeaders.Add(headers.Key, headers.Value);
         *  }
         *
         *  HttpResponseMessage responseMsg = null;
         *
         *  try
         *  {
         *      if (!connectedToInternet)
         *      {
         *          throw new NotConnectedToInternetException();
         *      }
         *
         *      responseMsg = await client.GetAsync(await this.AddLocationAndLanguageToApi(url));
         *
         *      string result = responseMsg.Content.ReadAsStringAsync().Result;
         *      if (responseMsg.IsSuccessStatusCode == false)
         *      {
         *          throw new Exception(result);
         *      }
         *
         *      return new ServerResponseObjectsBase
         *      {
         *          RawResponseText = result,
         *          StatusCode = responseMsg.StatusCode,
         *          IsSuccessStatus = responseMsg.IsSuccessStatusCode
         *      };
         *  }
         *  catch (Exception ex)
         *  {
         *      Debug.WriteLine("Call error was " + ex.Message);
         *      return new ServerResponseObjectsBase
         *      {
         *          IsSuccessStatus = false,
         *          RequestException = ex,
         *          StatusCode = responseMsg != null ? responseMsg.StatusCode : HttpStatusCode.ExpectationFailed,
         *          RawResponseText = "{}"
         *      };
         *  }
         * }*/

        /// <summary>
        /// Async method that allows you to send arbitrary JSON string to server
        /// </summary>
        /// <param name="jsonString">The arbitrary JSON string to be sent to the server</param>
        /// <param name="successCallback">The success callback</param>
        /// <param name="filterFlags">Error handling filter flags</param>
        /// <param name="timeOut">Time out in milliseconds</param>
        /// <returns>JSON string on success or null on failure</returns>
        protected async Task <ServerResponseObjectsBase> PostStringAsync(string jsonString, Action <object> successCallback = null, ErrorFilterFlags filterFlags = ErrorFilterFlags.EnableErrorHandling, ApiTimeoutEnum timeOut = ApiTimeoutEnum.Normal)
        {
            this.Timeout = timeOut;
            HttpResponseMessage responseMsg = null;
            var client = this.DefaultClient;

            foreach (KeyValuePair <string, string> headers in this._extraHeaders)
            {
                client.DefaultRequestHeaders.Add(headers.Key, headers.Value);
            }

            try
            {
                string url = await this.AddLocationAndLanguageToApi(this.ApiPath);

                responseMsg = await client.PostAsync(url, new StringContent(jsonString, Encoding.UTF8, @"application/json"));

                this.NotifyIfErrorStatusCode((uint)responseMsg.StatusCode);
                string rawResponse = await responseMsg.Content.ReadAsStringAsync();

                rawResponse.WriteLine();
                var resp = new ServerResponseObjectsBase
                {
                    RawResponseText = await responseMsg.Content.ReadAsStringAsync(),
                    StatusCode      = responseMsg.StatusCode,
                    IsSuccessStatus = responseMsg.IsSuccessStatusCode
                };

                if (!filterFlags.HasFlag(ErrorFilterFlags.AllowEmptyResponses))
                {
                    this.NotifyIfEmptyResponse(resp.RawResponseText, null);
                }

                if (successCallback != null)
                {
                    successCallback(resp);
                }

                return(resp);
            }
            catch (Exception ex)
            {
                ApiErrorHandler.ExceptionOccured(
                    this,
                    ex,
                    responseMsg != null ? (uint)responseMsg.StatusCode : 0,
                    filterFlags,
                    async x => await this.PostStringAsync(jsonString, successCallback),
                    successCallback);

                return(new ServerResponseObjectsBase
                {
                    IsSuccessStatus = false,
                    RequestException = ex,
                    StatusCode = responseMsg != null ? responseMsg.StatusCode : HttpStatusCode.ExpectationFailed,
                    RawResponseText = "{}"
                });
            }
        }
Example #2
0
        public virtual async Task <T> PostObjectAsync <T>(object obj, Action <object> successCallback = null, ErrorFilterFlags filterFlags = ErrorFilterFlags.EnableErrorHandling, ApiTimeoutEnum timeOut = ApiTimeoutEnum.Normal) where T : ServerResponseObjectsBase
        {
            ServerResponseObjectsBase response = await this.PostStringAsync(JsonConvert.SerializeObject(this.GetPostableData(obj), Formatting.Indented), successCallback, filterFlags, timeOut);

            if (response == null)
            {
                return(default(T));
            }

            return(response.GetResponseObject <T>());
        }