示例#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);
        }
示例#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);
            }
        }
示例#3
0
 /// <summary>
 /// Load all error translations from device
 /// </summary>
 public void LoadTranslations()
 {
     try
     {
         var descriptions    = httpClient.RequestGet("/desc", new Dictionary <string, string>(), true).Result;
         var defaultLanguage = descriptions.def;
         var byName          = (IDictionary <string, object>)descriptions;
         var data            = byName[defaultLanguage];
         var errors          = data.err;
         foreach (KeyValuePair <string, object> kvp in errors)
         {
             ECRDictionary.AddTranslations(kvp.Key.ToString(), kvp.Value.ToString());
         }
     }
     catch (Exception) { }
 }
示例#4
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);
        }
示例#5
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);
            }
        }