Exemplo n.º 1
0
        /// <summary>
        /// Parse response from the server
        /// </summary>
        /// <param name="jsonRawData"></param>
        /// <returns></returns>
        private dynamic ParseResponse(dynamic jsonRawData)
        {
            dynamic data      = null;
            var     converter = new ExpandoObjectConverter();

            try
            {
                data = JsonConvert.DeserializeObject <ExpandoObject>(jsonRawData, converter);
                try
                {
                    if (data.err.e != null)
                    {
                        var errorMessage        = ECRDictionary.GetTranslation(data.err.e);
                        HttpErrorEventArgs args = new HttpErrorEventArgs();
                        args.Message = errorMessage;
                        OnHttpError(args);
                    }
                }
                catch (Exception) { }
            }
            catch (Exception)
            {
                data = JsonConvert.DeserializeObject <List <ExpandoObject> >(jsonRawData, converter);
            }
            return(data);
        }
Exemplo n.º 2
0
        public async Task <dynamic> RequestPost(string endPoint, IEntity parameters, bool isPatchRequest)
        {
            dynamic data     = null;
            string  jsonData = parameters.ToJson();

            try
            {
                HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, endPoint);

                requestMessage.Content = new StringContent(jsonData, Encoding.UTF8, "application/json");

                if (isPatchRequest)
                {
                    requestMessage.Headers.Add("X-HTTP-Method-Override", "PATCH");
                }
                HttpResponseMessage response = await client.SendAsync(requestMessage).ConfigureAwait(continueOnCapturedContext: false);

                var jsonRawData = await response.Content.ReadAsStringAsync();

                var converter = new ExpandoObjectConverter();
                try
                {
                    data = JsonConvert.DeserializeObject <ExpandoObject>(jsonRawData, converter);
                    try
                    {
                        if (data.err.e != null)
                        {
                            var errorMessage        = ECRDictionary.GetTranslation(data.err.e);
                            HttpErrorEventArgs args = new HttpErrorEventArgs();
                            args.Message = errorMessage;
                            OnHttpError(args);
                        }
                    }
                    catch (Exception) {}
                }
                catch (Exception)
                {
                    data = JsonConvert.DeserializeObject <List <ExpandoObject> >(jsonRawData, converter);
                }
                return(data);
            }
            catch (Exception e)
            {
                HttpErrorEventArgs args = new HttpErrorEventArgs();
                args.Message = e.Message;
                OnHttpError(args);
                return(data);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Check if the response is success or not
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public ResponseFirmware CheckFirmwareFieldForError(dynamic field)
        {
            ResponseFirmware response = new ResponseFirmware();

            response.IsSuccess = true;
            response.Error     = "";
            try
            {
                int responseCode = int.Parse(field);
                if (responseCode != RESPONSE_SUCCESS)
                {
                    response.IsSuccess = false;
                    response.Error     = ECRDictionary.GetTranslation(field);
                    return(response);
                }
            }
            catch (Exception)
            {
                response.IsSuccess = false;
                response.Error     = ECRDictionary.GetTranslation(field);
                return(response);
            }
            return(response);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Provide GET request with parameters
        /// </summary>
        /// <param name="endPoint"></param>
        /// <param name="parameters"></param>
        /// <param name="isGzip"></param>
        /// <returns></returns>
        public async Task <dynamic> RequestGet(string endPoint, Dictionary <string, string> parameters, bool isGzip)
        {
            dynamic data = null;

            /**
             * Append the query string
             */
            endPoint += BuildURLParametersString(parameters);
            try
            {
                HttpResponseMessage response = await client.GetAsync(endPoint).ConfigureAwait(true);

                response.EnsureSuccessStatusCode();

                /**
                 * Read response as byte array
                 */
                var buffer = await response.Content.ReadAsByteArrayAsync();

                var byteArray = buffer.ToArray();
                if (isGzip)
                {
                    byteArray = Decompress(byteArray);
                }
                string jsonRawData;

                /**
                 * Check if the string contains the BOM character
                 * */
                if (byteArray.Length >= 3 && byteArray[0] == 0xEF && byteArray[1] == 0xBB && byteArray[2] == 0xBF)
                {
                    jsonRawData = Encoding.UTF8.GetString(byteArray, 3, byteArray.Length - 3);
                }
                else
                {
                    jsonRawData = Encoding.UTF8.GetString(byteArray);
                }

                var converter = new ExpandoObjectConverter();
                try
                {
                    data = JsonConvert.DeserializeObject <ExpandoObject>(jsonRawData, converter);
                    var isError    = false;
                    var errorKey   = "";
                    var properties = (IDictionary <string, object>)data;

                    /**
                     * Investigate the response for the error availability
                     */
                    if (properties.ContainsKey("err"))
                    {
                        if (properties["err"] is IList)
                        {
                        }
                        if (properties["err"] is string)
                        {
                            isError  = true;
                            errorKey = properties["err"].ToString();
                        }
                    }
                    if (isError)
                    {
                        var errorMessage        = ECRDictionary.GetTranslation(errorKey);
                        HttpErrorEventArgs args = new HttpErrorEventArgs();
                        args.Message = errorMessage;
                        OnHttpError(args);
                    }
                }
                catch (Exception)
                {
                    data = JsonConvert.DeserializeObject <List <ExpandoObject> >(jsonRawData, converter);
                }
                return(data);
            }
            catch (Exception e)
            {
                HttpErrorEventArgs args = new HttpErrorEventArgs();
                args.Message = e.Message;
                OnHttpError(args);
                return(data);
            }
        }