Exemple #1
0
        public void parseJson(string json)
        {
            mNewsArticles.Clear();

            Util.JsonParser parser = new Util.JsonParser();
            parser.addListener(this);

            parser.parseString(json);

            if (mCurrNewsArticle != null && mCurrNewsArticle.Title != "")
            {
                mNewsArticles.Add(mCurrNewsArticle);
                mCurrNewsArticle = new NewsArticle();
            }

            foreach (NewsArticleManagerListener listener in mListeners)
            {
                listener.onParsed(this);
            }
        }
        public void parseJson(string json)
        {
            clear();

            Util.JsonParser parser = new Util.JsonParser();
            parser.addListener(this);

            parser.parseString(json);

            if (mCurrQuote != null && mCurrQuote.Code != "")
            {
                mQuotes[mCurrQuote.Code] = mCurrQuote;
                mCurrQuote = new Quote();
            }

            foreach (QuoteManagerListener listener in mListeners)
            {
                listener.onParsed(this);
            }
        }
        internal string ExecuteGetRequest(CityAPISession session, string relativeUrl)
        {
            int timeSecs = GetUnixEpochTimeSecs();

            if (!relativeUrl.StartsWith("/"))
            {
                relativeUrl = "/" + relativeUrl;
            }

            string cityApiHost = "api.onthecity.org";
            string absoluteUrl = string.Format("https://{0}{1}", cityApiHost, relativeUrl);

            // city API requests require query string params to be in alphabetical order
            Uri absoluteUri = new Uri(absoluteUrl);
            string queryString = absoluteUri.Query;

            if (!string.IsNullOrEmpty(queryString))
            {
                var queryParams = System.Web.HttpUtility.ParseQueryString(queryString);
                List<string> sortedSegments = new List<string>();

                queryString = string.Join("&", queryParams.AllKeys.OrderBy(k => k).ToList().Select(key => key + "=" + queryParams[key]).ToArray());
                relativeUrl = absoluteUri.AbsolutePath + "?" + queryString;
                absoluteUrl = string.Format("https://{0}{1}", cityApiHost, relativeUrl);
            }

            string httpVerb = "GET";

            string constructedSignatureUrl = ConstructHmacUrl(timeSecs, httpVerb, absoluteUrl);
            string hmacSignature = new HmacEncoder().EncodeCityHmacSignature(constructedSignatureUrl, session.SecretKey);

            System.Net.HttpWebRequest wc = (HttpWebRequest)WebRequest.Create(absoluteUrl);
            wc.KeepAlive = false;
            wc.Headers.Clear();
            wc.Headers.Add("X-City-Sig", hmacSignature);
            wc.Headers.Add("X-City-User-Token", session.UserToken);
            wc.Headers.Add("X-City-Time", timeSecs.ToString());
            wc.Accept = "application/vnd.thecity.admin.v1+json";
            wc.ContentType = "application/json";
            wc.ContentLength = 0;
            wc.UserAgent = "TheCity .NET API, Brandon Potter";

            // uncomment to use Fiddler
            //wc.Proxy = new WebProxy("127.0.0.1", 8888);

            try
            {
                HttpWebResponse resp = (HttpWebResponse)wc.GetResponse();
                string stringResponse = null;

                using (Stream s = resp.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(s))
                    {
                        var responseText = reader.ReadToEnd();
                        stringResponse = responseText;
                    }
                }

                ProcessAPILimitResponseHeaders(resp.Headers, session);

                return stringResponse;
            }
            catch (WebException ex)
            {
                HttpWebResponse resp = (HttpWebResponse)ex.Response;
                using (Stream s = resp.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(s))
                    {
                        var responseText = reader.ReadToEnd();
                        Util.JsonParser jp = new Util.JsonParser();
                        var result = jp.Parse(responseText);
                        string errorMsg = result.error_message;

                        //if (errorMsg.Contains("signature"))
                        //{
                        if (System.Diagnostics.Debugger.IsAttached)
                        {
                            throw new Exception(ex.Message + System.Environment.NewLine + errorMsg + System.Environment.NewLine + System.Environment.NewLine + BuildSignatureDebugInfo(constructedSignatureUrl, timeSecs.ToString(),
                                session.UserToken, session.SecretKey, hmacSignature));
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(errorMsg))
                            {
                                throw new Exception(ex.Message);
                            }
                            else
                            {
                                throw new Exception(errorMsg);
                            }
                        }
                        //}

                        // throw new Exception(errorMsg ?? responseText);
                    }
                }
            }
        }
 internal dynamic ParseJsonResponse(string input)
 {
     Util.JsonParser jp = new Util.JsonParser();
     return jp.Parse(input);
 }