internal static async Task <T> ConvertToDataModel <T>(this Http.IHttpResponse response) where T : ODDataModel
        {
            using (Stream stream = await response.GetResponseStreamAsync())
            {
                var    reader = new StreamReader(stream);
                string data   = await reader.ReadToEndAsync();

                T result = data.ConvertToDataModel <T>();
                return(result);
            }
        }
예제 #2
0
        private static object Predict(Http.IHttpRequest request, Http.IHttpResponse response)
        {
            var idUsr   = float.Parse(request.QueryString.Where(x => x.Key == "idUsr").Select(x => x.Value).FirstOrDefault());
            var idSound = float.Parse(request.QueryString.Where(x => x.Key == "idSound").Select(x => x.Value).FirstOrDefault());

            ModelInput sampleData = new ModelInput()
            {
                Userid  = idUsr,
                Musicid = idSound,
            };

            var predictionResult = ConsumeModel.Predict(sampleData);

            return(predictionResult.Score.ToString("#.#######", new System.Globalization.CultureInfo("en")));
        }
예제 #3
0
        public static async Task <ODServerException> ToException(this Http.IHttpResponse response)
        {
            var responseErrorType = response.StatusCode.ToHttpResponseType();

            if (responseErrorType != HttpResponseType.ServerError && responseErrorType != HttpResponseType.ClientError)
            {
                throw new InvalidOperationException("Attempting to create exception for a non-error response");
            }

            var contentType = response.ContentType.CleanContentType();

            if (null != contentType && contentType.Equals(ApiConstants.ContentTypeJson))
            {
                // Parse the error response json
                ODError errorObj = await response.ConvertToDataModel <ODError>();

                if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    return(new ODAuthenticationException(null != errorObj ? errorObj.Message : response.StatusDescription)
                    {
                        HttpStatusCode = (int)response.StatusCode,
                        HttpStatusMessage = response.StatusDescription,
                        ServiceError = errorObj
                    });
                }
                else
                {
                    return(new ODServerException(null != errorObj ? errorObj.Message : response.StatusDescription)
                    {
                        HttpStatusCode = (int)response.StatusCode,
                        HttpStatusMessage = response.StatusDescription,
                        ServiceError = errorObj
                    });
                }
            }
            else
            {
                return(new ODServerException("HTTP response did not include a JSON error object", null));
            }
        }