public static CityAPISession GenerateAPISession() { string cityDomain = "mychurch.onthecity.org"; // for example: mychurch.onthecity.org string secretKey = ""; // get this from your City admin API privileges page string userToken = ""; // get this from your City admin API Privileges page if (string.IsNullOrEmpty(secretKey)) { throw new Exception("You must fill in your Test Global variables for your City API account to run unit tests. See TestGlobals.cs"); } CityAPISession newSession = new CityAPISession(cityDomain, secretKey, userToken); return newSession; }
internal BarcodesConnector(CityAPISession session) { _parentSession = session; }
internal GroupsConnector(CityAPISession session) { _parentSession = session; }
private void ProcessAPILimitResponseHeaders(WebHeaderCollection headerCollection, CityAPISession session) { //X-City-RateLimit-Limit-By-Account //X-City-RateLimit-Remaining-By-Account //X-City-RateLimit-Limit-By-Ip //X-City-RateLimit-Remaining-By-Ip session.RateLimitByAccount = ConvertToNullableInt(GetHeaderValue(headerCollection, "X-City-RateLimit-Limit-By-Account")); session.RateLimitRemainingByAccount = ConvertToNullableInt(GetHeaderValue(headerCollection, "X-City-RateLimit-Remaining-By-Account")); session.RateLimitByIP = ConvertToNullableInt(GetHeaderValue(headerCollection, "X-City-RateLimit-Limit-By-Ip")); session.RateLimitRemainingByIP = ConvertToNullableInt(GetHeaderValue(headerCollection, "X-City-RateLimit-Remaining-By-Ip")); }
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 JsonObject ExecuteGetRequestAndParse(CityAPISession session, string relativeUrl) { var response = ExecuteGetRequest(session, relativeUrl); var jsonObj = this.ParseJsonResponse(response); return jsonObj; }
internal UsersConnector(CityAPISession session) { _parentSession = session; }
internal CheckinsConnector(CityAPISession session) { _parentSession = session; }