Sunday, August 27, 2017

C#, Json, Visual Studio - How To import a Json File into C# Class automatically with Visual studio


1. Create A Class.
2. Ctrl+C on the json data.
3. Select your new class and the curley braces.
4. Go to the menu edit then paste special Select Paste Json as Classes.
5. Now all the properties should be parsed.



Wednesday, August 23, 2017

C# - BitlyLink for Twitter with Http Client Get


   public BitlyLink.BitlyLookup Lookup()
        {
            var url = string.Format(ConfigurationManager.AppSettings["BitlyLookupUrl"] + "?url={0}&access_token={1}", _longUrl, AccessToken);
            
            try
            {
                using (var httpClient = new HttpClient())
                {
                    var dataResult = httpClient.GetAsync(url).Result;
                    var responseJson = dataResult.Content.ReadAsStringAsync().Result;
                    var result = JsonConvert.DeserializeObject<BitlyLink.BitlyLookup>(responseJson);

                    return result;
                }
            }
            catch (Exception e)
            {
                var logger = LogManager.GetLogger();
                logger.Error(string.Format("Bitly returned the following error: {0}", e.Message));
                logger.Warning(string.Format("Couldn't bitly this url: {0}", _longUrl));

                var bitlyLinkLookup = new List<BitlyLink.BitlyLinkLookup>
                {
                    new BitlyLink.BitlyLinkLookup
                    {
                        AggregateLink = HttpUtility.UrlDecode(_longUrl),
                        Url = HttpUtility.UrlDecode(_longUrl)
                    }
                };
                var bitlyData = new BitlyLink.BitlyDataLookup
                {
                    LinkLookup = bitlyLinkLookup
                };
                return new BitlyLink.BitlyLookup
                {
                    Data = bitlyData
                };
            }
        }