static void print(AuthorizeResponse ar) { if (ar.authorized) { Console.WriteLine("Authorized!!"); } else { Console.WriteLine("NOT Authorized!!" + ar.reason); } Console.WriteLine("PLAN: " + ar.plan); Console.WriteLine("#usages: " + ar.usages.Count); int i = 0; foreach (UsageItem item in ar.usages) { Console.WriteLine("Usage: " + i); Console.WriteLine(" Metric: " + item.metric); Console.WriteLine(" Period: " + item.period); Console.WriteLine(" CurrValue: " + item.current_value); Console.WriteLine(" MaxValue: " + item.max_value); Console.WriteLine(" PeriodStart:" + item.period_start); Console.WriteLine(" PeriodEnd: " + item.period_start); i++; } }
//user_key=3scale-9f58660be36c0add4d52198c7c9ecb6e&provider_key=3scale-5fc9d398ac038e4e8f212cc1e8cf01d2 static void Main(string[] args) { Api _3ScaleAPI = new Api("http://server.3scale.net", "3scale-5fc9d398ac038e4e8f212cc1e8cf01d2"); Console.WriteLine("====>Test 1:Success<======"); string resp = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><status><plan>Demo</plan><usage period=\"day\" metric=\"hits\"><period_start>2010-08-01 22:00:00</period_start><period_end>2010-08-02 21:59:59</period_end><current_value>0</current_value><max_value>10000</max_value></usage><usage period=\"hour\" metric=\"demoapiupload\"><period_start>2010-08-02 09:00:00</period_start><period_end>2010-08-02 09:59:59</period_end><current_value>0</current_value><max_value>1000</max_value></usage><usage period=\"hour\" metric=\"demoapiretrieve\"><period_start>2010-08-02 09:00:00</period_start><period_end>2010-08-02 09:59:59</period_end><current_value>0</current_value><max_value>1000</max_value></usage><usage period=\"hour\" metric=\"demoapisearch\"><period_start>2010-08-02 09:00:00</period_start><period_end>2010-08-02 09:59:59</period_end><current_value>0</current_value><max_value>2000</max_value></usage><usage period=\"day\" metric=\"demoapisearch\"><period_start>2010-08-01 22:00:00</period_start><period_end>2010-08-02 21:59:59</period_end><current_value>0</current_value><max_value>5000</max_value></usage></status>"; AuthorizeResponse auth_response = new AuthorizeResponse(Encoding.UTF8.GetString(resp)); }
/// <summary> /// Executes a different Authorize calls on the 3scale backend depending on the endpoint /// </summary> /// <returns>An AuthorizeResponse object representing the authorize response</returns> /// <param name="endPoint">The endpoint to hit</param> /// <param name="parameters">A Hashtable of parameter name value pairs.</param> private AuthorizeResponse CallAuthorize(string endPoint, Hashtable parameters) { string URL = hostURI + endPoint; string content = "?provider_key=" + provider_key; if (!parameters.ContainsKey("usage")) { Hashtable usage = new Hashtable(); usage.Add("hits", "1"); parameters.Add("usage", usage); } AddParameters(ref content, parameters); URL += content; var request = WebRequest.Create(URL); request.Headers.Add("X-3scale-User-Agent", "plugin-dotnet-v" + version); try { using (var response = request.GetResponse()) { Stream s = response.GetResponseStream(); List<byte> st = new List<byte>(); int ct = 0; while ((ct = s.ReadByte()) != -1) { st.Add((byte)ct); } byte[] b = st.ToArray(); st.Clear(); switch (((HttpWebResponse)response).StatusCode) { case HttpStatusCode.OK: AuthorizeResponse auth_response = new AuthorizeResponse(Encoding.UTF8.GetString(b)); s.Close(); return auth_response; } s.Close(); } } catch (WebException w) { if (w.Response == null) throw w; Stream s = w.Response.GetResponseStream(); byte[] b = new byte[s.Length]; s.Read(b, 0, b.Length); s.Close(); ApiError err = null; try { err = new ApiError(Encoding.UTF8.GetString(b)); } catch (Exception) { err = null; } if (err != null) throw new ApiException(err.code + " : " + err.message); switch (((HttpWebResponse)w.Response).StatusCode) { case HttpStatusCode.Forbidden: throw new ApiException("Forbidden"); case HttpStatusCode.BadRequest: throw new ApiException("Bad request"); case HttpStatusCode.InternalServerError: throw new ApiException("Internal server error"); case HttpStatusCode.NotFound: throw new ApiException("Request route not found"); case HttpStatusCode.Conflict: AuthorizeResponse auth_response = new AuthorizeResponse(Encoding.UTF8.GetString(b)); return auth_response; default: throw new ApiException("Unknown Exception: " + Encoding.UTF8.GetString(b)); } } return null; }