コード例 #1
0
        /// <summary>
        /// Report the specified transactions to the 3scale backend.
        /// </summary>
        /// <param name="transactions">A Hashtable containing the transactions to be reported</param>
        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.Headers.Add("X-3scale-User-Agent", "plugin-dotnet-v" + version);

            request.ContentType = contentType;
            request.Method = "POST";
            string content = "provider_key=" + provider_key;

            AddTransactions(ref content, transactions);

            Console.WriteLine("content: " + content);
            
            byte[] data = Encoding.UTF8.GetBytes(content);
            request.ContentLength = data.Length;

            try
            {
                request.ContentLength = data.Length;
                Stream str = request.GetRequestStream();
                str.Write(data, 0, data.Length);

                using (HttpWebResponse response = (HttpWebResponse)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();

                    //Console.WriteLine(".--------------- " + response.StatusCode + " :::: " + HttpStatusCode.OK);
                    
                    switch (response.StatusCode)
                    {
                        case HttpStatusCode.OK:
                            s.Close();
                            return;
                        case HttpStatusCode.Accepted:
                            s.Close();
                            return;

                    }
                    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);
                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");

                    default:
                        throw new ApiException("Unknown Exception: " + Encoding.UTF8.GetString(b));
                }
            }

            return;
        }
コード例 #2
0
        /// <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;
        }
コード例 #3
0
        /// <summary>
        /// Report the specified transactions to the 3scale backend.
        /// </summary>
        /// <param name="transactions">A Hashtable containing the transactions to be reported</param>
        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.Headers.Add("X-3scale-User-Agent", "plugin-dotnet-v" + version);

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

            string content;

            //Check we have a service_token or provider_key

            if (String.IsNullOrEmpty(provider_key) && !transactions.ContainsKey("service_token"))
            {
                throw new ApiException("You need to set either a provider_key or service_token");
            }
            else
            {
                content = String.IsNullOrEmpty(provider_key) ? "" : "provider_key=" + provider_key;
            }

            AddTransactions(ref content, transactions);

            byte [] data = Encoding.UTF8.GetBytes(content);

            try{
                request.ContentLength = data.Length;
                Stream str = request.GetRequestStream();
                str.Write(data, 0, data.Length);

                using (var response = request.GetResponse()) {
                    switch (((HttpWebResponse)response).StatusCode)
                    {
                    case HttpStatusCode.OK:
                        return;

                    case HttpStatusCode.Accepted:
                        return;
                    }
                }
            }
            catch (WebException w) {
                using (WebResponse response = w.Response) {
                    using (var streamReader = new StreamReader(response.GetResponseStream())) {
                        ApiError err     = null;
                        string   err_msg = streamReader.ReadToEnd();
                        try
                        {
                            err = new ApiError(err_msg);
                        } 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");

                        default:
                            throw new ApiException("Unknown Exception: " + err_msg);
                        }
                    }
                }
            }
            return;
        }
コード例 #4
0
        /// <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())
                {
                    using (var streamReader = new StreamReader(response.GetResponseStream())) {
                        switch (((HttpWebResponse)response).StatusCode)
                        {
                        case HttpStatusCode.OK:
                            string            msg           = streamReader.ReadToEnd();
                            AuthorizeResponse auth_response = new AuthorizeResponse(msg);
                            return(auth_response);
                        }
                    }
                }
            }
            catch (WebException w)
            {
                using (WebResponse response = w.Response) {
                    using (var streamReader = new StreamReader(response.GetResponseStream())) {
                        ApiError err     = null;
                        string   err_msg = streamReader.ReadToEnd();
                        try {
                            err = new ApiError(err_msg);
                        } 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(err_msg);
                            return(auth_response);

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

            return(null);
        }
コード例 #5
0
        /// <summary>
        /// Report the specified transactions to the 3scale backend.
        /// </summary>
        /// <param name="transactions">A Hashtable containing the transactions to be reported</param>
        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.Headers.Add("X-3scale-User-Agent", "plugin-dotnet-v" + version);

            request.ContentType = contentType;
            request.Method      = "POST";
            string content = "provider_key=" + provider_key;

            AddTransactions(ref content, transactions);

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

            byte[] data = Encoding.UTF8.GetBytes(content);
            request.ContentLength = data.Length;

            try
            {
                request.ContentLength = data.Length;
                Stream str = request.GetRequestStream();
                str.Write(data, 0, data.Length);

                using (HttpWebResponse response = (HttpWebResponse)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();

                    //Console.WriteLine(".--------------- " + response.StatusCode + " :::: " + HttpStatusCode.OK);

                    switch (response.StatusCode)
                    {
                    case HttpStatusCode.OK:
                        s.Close();
                        return;

                    case HttpStatusCode.Accepted:
                        s.Close();
                        return;
                    }
                    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);
                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");

                default:
                    throw new ApiException("Unknown Exception: " + Encoding.UTF8.GetString(b));
                }
            }

            return;
        }
コード例 #6
0
        /// <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);
        }