Exemple #1
0
 /// <summary>
 /// Turn the value into a descriptive string.
 /// </summary>
 /// <returns>String value as defined in the API.</returns>
 public static string ToDescriptionString(this EventsScroll value)
 {
     DescriptionAttribute[] attributes = (DescriptionAttribute[])value
                                         .GetType()
                                         .GetField(value.ToString())
                                         .GetCustomAttributes(typeof(DescriptionAttribute), false);
     return(attributes.Length > 0 ? attributes[0].Description : string.Empty);
 }
Exemple #2
0
        public async Task <IEnumerable <MeetupEventModel> > GetEvents(
            string urlName, EventsScroll scroll = EventsScroll.Undefined)
        {
            IEnumerable <MeetupEventModel> Default() => new List <MeetupEventModel>(0);

            if (string.IsNullOrWhiteSpace(urlName))
            {
                Logger.LogError($"Null or whitespace urlname, aborting API call.");
                return(Default());
            }

            NameValueCollection BuildQueryString(NameValueCollection query)
            {
                var collection = HttpUtility.ParseQueryString(string.Empty);

                foreach (var key in query.Cast <string>().Where(key => !string.IsNullOrEmpty(query[key])))
                {
                    collection[key] = query[key];
                }
                return(collection);
            }

            var queryString = BuildQueryString(new NameValueCollection {
                { "photo-host", "secure" },
                { "scroll", scroll.ToDescriptionString() },
                { "desc", "true" } // Hack; this param results in more returned events and in correct order.
            });
            var response = await HttpClient.GetAsync($"/{HttpUtility.UrlEncode(urlName)}/events?{queryString}");

            if (!response.IsSuccessStatusCode)
            {
                Logger.LogError($"Unexpected meetup API response; status code {response.StatusCode}.");
                return(Default());
            }
            try
            {
                return(await response.Content.ReadAsAsync <List <MeetupEventModel> >());
            }
            catch (Exception e)
            {
                Logger.LogError("Unable to deserialize API response.", e);
                return(Default());
            }
        }