예제 #1
0
        public WeatherConditions GetConditions(WeatherLocation location)
        {
            if (location == null)
            {
                throw new ArgumentNullException(nameof(location));
            }
            if (!location.IsValidQuery)
            {
                throw new ArgumentException("WeatherLocation must have a valid value.", nameof(location));
            }

            var queryResult = WebString.Download(MakeQuery(location));

            if (!queryResult.Success)
            {
                return(new WeatherConditions(queryResult));
            }

            var json    = JObject.Parse(queryResult.Document);
            var message = (string)json["message"];

            if (string.IsNullOrEmpty(message))
            {
                return(new WeatherConditions(queryResult.Location, json));
            }
            else
            {
                var ex = new JsonErrorException(message);
                return(new WeatherConditions(queryResult.Location, ex));
            }
        }
예제 #2
0
        string MakeQuery(WeatherLocation location)
        {
            var queryData = Uri.EscapeDataString(location.ToString());

            if (location.IsCityQuery)
            {
                return(cityQuery + queryData);
            }
            else
            {
                return(zipQuery + queryData);
            }
        }