Esempio n. 1
0
        public IEnumerable <GroceryQueryResult> EndQuery(IAsyncResult result)
        {
            if (!result.IsCompleted)
            {
                result.AsyncWaitHandle.WaitOne();
            }

            GroceryResult groceryResult = result as GroceryResult;

            if (groceryResult == null)
            {
                throw new ArgumentException(InvalidAsyncResult, "result");
            }

            WebRequest req = groceryResult.InnerResult.AsyncState as WebRequest;

            if (req == null)
            {
                throw new ArgumentException(InvalidAsyncResult, "result");
            }

            JObject jObject = null;

            using (WebResponse resp = req.EndGetResponse(groceryResult.InnerResult))
                using (Stream stream = resp.GetResponseStream())
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        string json = reader.ReadToEnd();
                        jObject = JObject.Parse(json);
                    }

            if ((int)jObject["StatusCode"] >= 400)
            {
                throw new ApplicationException(
                          String.Format(
                              "Grocery API returned status code {0}",
                              jObject["StatusCode"]));
            }

            var data = jObject["Groceries"] as JArray;

            if (data != null)
            {
                foreach (var queryResult in data)
                {
                    var jobjResult = queryResult as JObject;
                    yield return(new GroceryQueryResult(jobjResult));
                }
            }
            else
            {
                var groceries = jObject["Groceries"] as JObject;
                yield return(new GroceryQueryResult(groceries));
            }
        }
Esempio n. 2
0
        public IAsyncResult BeginQuery(string query, string entity, AsyncCallback callback, object state)
        {
            string uri = ConstructQueryUri(query, entity);
            WebRequest req = WebRequest.Create(uri);

            GroceryResult smResult = new GroceryResult(state);
            smResult.InnerResult = req.BeginGetResponse(
                (result) =>
                {
                    if (callback != null)
                        callback(smResult);
                },
                req);

            return smResult;
        }
Esempio n. 3
0
        public IAsyncResult BeginQuery(string query, string entity, AsyncCallback callback, object state)
        {
            string     uri = ConstructQueryUri(query, entity);
            WebRequest req = WebRequest.Create(uri);

            GroceryResult smResult = new GroceryResult(state);

            smResult.InnerResult = req.BeginGetResponse(
                (result) =>
            {
                if (callback != null)
                {
                    callback(smResult);
                }
            },
                req);

            return(smResult);
        }