CS_threescale.Api.report C# (CSharp) Метод

report() публичный Метод

public report ( Hashtable transactions ) : void
transactions System.Collections.Hashtable
Результат void
        public void report(Hashtable transactions)
        {
            if ((transactions == null) || (transactions.Count <= 0)) throw new ApiException("argument error: undefined transactions, must be at least one");

            string URL = hostURI + "/transactions.xml";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);

            request.ContentType = contentType;
            request.Method = "POST";

            StringBuilder contentBuilder = new StringBuilder("provider_key=");
            contentBuilder.Append(provider_key);

            AddTransactions(contentBuilder, transactions);

            Console.WriteLine("content: " + contentBuilder);

            byte[] contentBytes = Encoding.UTF8.GetBytes(contentBuilder.ToString());

            request.ContentLength = contentBytes.Length;

            try
            {
                using(Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(contentBytes, 0, contentBytes.Length);
                }

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    //Console.WriteLine(".--------------- " + response.StatusCode + " :::: " + HttpStatusCode.OK);
                }
            }
            catch (WebException webException)
            {
                if (webException.Response == null) throw webException;

                using(HttpWebResponse response = (HttpWebResponse)webException.Response)
                {
                    string responseBody;

                    using(Stream responseStream = response.GetResponseStream())
                    using(StreamReader responseStreamReader = new StreamReader(responseStream, Encoding.UTF8))
                    {
                        responseBody = responseStreamReader.ReadToEnd();
                    }

                    ApiError err;

                    try
                    {
                        err = SerializeHelper<ApiError>.Ressurect(responseBody);
                    }
                    catch(Exception)
                    {
                        err = null;
                    }

                    if(err != null) throw new ApiException(err.code + " : " + err.message);

                    switch(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");

                        default:
                            throw new ApiException("Unknown Exception: " + responseBody);
                    }
                }
            }
            return;
        }

Usage Example

Пример #1
0
        static void Main(string[] args)
        {
            try {
                string provider_key = "YOUR_PROVIDER_KEY";
                string app_id = "APP_ID_OF_THE_USER";

                Api _3ScaleAPI = new Api("http://su1.3scale.net", provider_key);

                AuthorizeResponse resp = _3ScaleAPI.authorize(app_id);

                print(resp);

                Console.WriteLine("Done authorize...");

                System.Collections.Hashtable transactions = new System.Collections.Hashtable();
                System.Collections.Hashtable transaction = null;
                System.Collections.Hashtable usage = null;

                transaction = new System.Collections.Hashtable();
                transaction.Add("app_id",app_id);
                transaction.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss K"));
                usage = new System.Collections.Hashtable();
                usage.Add("hits", 10);
                transaction.Add("usage",usage);
                transactions.Add("0", transaction);

                transaction = new System.Collections.Hashtable();
                transaction.Add("app_id", app_id);
                transaction.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss K"));
                usage = new System.Collections.Hashtable();
                usage.Add("hits", 1);
                transaction.Add("usage", usage);
                transactions.Add("1", transaction);
                _3ScaleAPI.report(transactions);

                Console.WriteLine("Done report...");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            string s = Console.ReadLine();
        }
All Usage Examples Of CS_threescale.Api::report