Esempio n. 1
0
        private void LoadListStuff()
        {
            var result = string.Empty;

            result    = _client.Get(new Customer());
            customers = JsonDeserializer <Customer> .DeserializeObject(result);

            result   = _client.Get(new Product());
            products = JsonDeserializer <Product> .DeserializeObject(result);

            result = _client.Get(new Store());
            stores = JsonDeserializer <Store> .DeserializeObject(result);

            result       = _client.Get(new LoyaltyCard());
            loyaltyCards = JsonDeserializer <LoyaltyCard> .DeserializeObject(result);

            result = _client.Get(new Offer());
            offers = JsonDeserializer <Offer> .DeserializeObject(result);

            result     = _client.Get(new StockItem());
            stockItems = JsonDeserializer <StockItem> .DeserializeObject(result);

            result = string.Empty;

            AttachList();

            this.ItemDGV.ItemsSource     = products;
            this.StoreDGV.ItemsSource    = stores;
            this.CustomerDGV.ItemsSource = customers;
            this.OfferDGV.ItemsSource    = offers;

            this.cmbOffer.ItemsSource       = offers;
            this.cmbOffer.DisplayMemberPath = "Name";
            this.cmbOffer.SelectedIndex     = 0;
        }
Esempio n. 2
0
        private void btnAddItem_Click(object sender, RoutedEventArgs e)
        {
            var product = new Product()
            {
                Name        = txtItemName.Text,
                Price       = Convert.ToDecimal(txtPrice.Text),
                DeliveryFee = Convert.ToDecimal(txtDeliveryFee.Text),
                OfferId     = 0
            };
            var result = _client.Post(product);

            products = JsonDeserializer <Product> .DeserializeObject(result);

            LoadListStuff();
        }
        public async Task <ExpandResponseModel> Expand(
            string shortUrl,
            CancellationToken cancellationToken
            )
        {
            if (string.IsNullOrWhiteSpace(shortUrl))
            {
                throw new InvalidOperationException("shortUrl was null or whitespace.");
            }


            var values = new Dictionary <string, object>();


            values.Add("shorturl", shortUrl);
            values.Add("format", ApiResponseFormat);


            values.Add("action", ExpandActionName);
            var response = await CallApi(
                ExpandActionName,
                values,
                cancellationToken
                );

            if (!response.IsSuccessStatusCode)
            {
                throw new YourlsException();
            }

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

            var resultModel = JsonDeserializer.DeserializeObject <ExpandResponse>(responseText);

            if (resultModel is null || resultModel.Keyword is null)
            {
                throw new YourlsException($"The result of {nameof(JsonDeserializer.DeserializeObject)} was null or it didn't contain a Keyword value.");
            }

            return(new ExpandResponseModel
            {
                Keyword = resultModel.Keyword,
                Title = resultModel.Title,
                LongUrl = resultModel.LongUrl,
                ShortUrl = resultModel.ShortUrl
            });
        }
        public async Task <UrlStatsResponseModel> GetUrlStats(
            string shortUrl,
            CancellationToken cancellationToken
            )
        {
            if (string.IsNullOrWhiteSpace(shortUrl))
            {
                throw new InvalidOperationException("shortUrl was null or whitespace.");
            }


            var values = new Dictionary <string, object>();


            values.Add("shorturl", shortUrl);
            values.Add("format", ApiResponseFormat);


            values.Add("action", GetUrlStatsActionName);
            var response = await CallApi(
                GetUrlStatsActionName,
                values,
                cancellationToken
                );

            if (!response.IsSuccessStatusCode)
            {
                throw new YourlsException();
            }

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

            var resultModel = JsonDeserializer.DeserializeObject <GetUrlStatsResponse>(responseText);

            if (resultModel is null || resultModel.Message is null || resultModel.Link is null)
            {
                throw new YourlsException($"The result of {nameof(JsonDeserializer.DeserializeObject)} was null or it didn't contain Message or Link values.");
            }

            return(resultModel.Link);
        }
 public static object FromJson(string json, Type t)
 {
     try
     {
         JsonDeserializer jsonDeserializer = new JsonDeserializer();
         int i = 0;
         return(jsonDeserializer.DeserializeObject(json, ref i, t));
     }
     catch (Exception ex)
     {
         LoggingUtilities.LogFormat(
             "Json deserialization halted with error message: ({0})\nInner Exception: ({1})\nData: ({2})\nHelplink: ({3})\nHResult: ({4})\nSource: ({5})\nTargetSite: ({6})\nStack Trace: ({7})",
             ex.Message,
             ex.InnerException,
             ex.Data,
             ex.HelpLink,
             ex.HResult,
             ex.Source,
             ex.TargetSite,
             ex.StackTrace);
         throw ex;
     }
 }
        public async Task <ShortenUrlResponseModel> ShortenUrl(
            ShortenUrlRequestModel model,
            CancellationToken cancellationToken
            )
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }


            if (string.IsNullOrWhiteSpace(model.Url))
            {
                throw new InvalidOperationException("Url was null or whitespace.");
            }


            var values = new Dictionary <string, object>();

            if (!string.IsNullOrWhiteSpace(model.Keyword))
            {
                values.Add("keyword", model.Keyword);
            }

            if (!string.IsNullOrWhiteSpace(model.Title))
            {
                values.Add("title", model.Title);
            }


            values.Add("url", model.Url);
            values.Add("format", ApiResponseFormat);


            values.Add("action", ShortenUrlActionName);


            var response = await CallApi(
                ShortenUrlActionName,
                values,
                cancellationToken
                );

            if (!response.IsSuccessStatusCode)
            {
                throw new YourlsException();
            }

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

            var resultModel = JsonDeserializer.DeserializeObject <ShortenUrlResponse>(responseText);

            var status = resultModel.Status?.ToLower();
            var code   = resultModel.Code?.ToLower();

            if (code == "error:keyword")
            {
                throw new YourlsException("Keyword was already taken.");
            }

            if (status != "success" && code != "error:url")
            {
                throw new YourlsException($"Unknown error occured, original code was: {resultModel.Code}");
            }

            if (resultModel is null || resultModel.Url is null)
            {
                throw new YourlsException($"The result of {nameof(JsonDeserializer.DeserializeObject)} was null or it didn't contain a Url value.");
            }

            var result = resultModel.Url;

            if (resultModel.Status?.ToLower() != "success")
            {
                result.Existed = true;
            }

            return(new ShortenUrlResponseModel
            {
                Url = result,

                Code = resultModel.Code,
                Message = resultModel.Message,
                Status = resultModel.Status,
                Title = resultModel.Title,
                ShortUrl = resultModel.ShortUrl,
                StatusCode = resultModel.StatusCode
            });
        }