public static EntityResult Get(string url, Params parameters, Dictionary<string, string> headers, ApiConfig env)
        {
            HttpStatusCode code;
            string json = GetJson(url, parameters, env, headers, out code);

            EntityResult result = new EntityResult(code, json);
            return result;
        }
        public static string BuildUrl(ApiConfig apiConfig, params string[] paths)
        {
            StringBuilder sb = new StringBuilder(apiConfig.ApiBaseUrl);

            foreach (var path in paths)
            {
                sb.Append('/').Append(HttpUtility.UrlPathEncode(path));
            }

            return sb.ToString();
        }
        public static EntityResult Post(string url, Params parameters, Dictionary<string, string> headers, ApiConfig env)
        {
            HttpWebRequest request = GetRequest(url, HttpMethod.POST, headers, env);
            byte[] paramsBytes =
                Encoding.GetEncoding(env.Charset).GetBytes(parameters.GetQuery());

            request.ContentLength = paramsBytes.Length;
            request.ContentType =
                String.Format("application/x-www-form-urlencoded;charset={0}",env.Charset);
            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(paramsBytes, 0, paramsBytes.Length);

                HttpStatusCode code;
                string json = SendRequest(request, out code);

                EntityResult result = new EntityResult(code, json);
                return result;
            }
        }
 private static string GetJson(string url, Params parameters, ApiConfig env, Dictionary<string, string> headers, out HttpStatusCode code)
 {
     url = String.Format("{0}?{1}", url, parameters.GetQuery());
     HttpWebRequest request = GetRequest(url, HttpMethod.GET, headers, env);
     return SendRequest(request, out code);
 }
        private static HttpWebRequest GetRequest(string url, HttpMethod method, Dictionary<string, string> headers, ApiConfig env)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = Enum.GetName(typeof(HttpMethod), method);
            request.UserAgent = String.Format("ChargeBee-DotNet-Client v{0} on {1} / {2}",
                ApiConfig.Version,
                Environment.Version,
                Environment.OSVersion);

             request.Accept = "application/json";

            AddHeaders (request, env);
            AddCustomHeaders (request, headers);

            request.Timeout = env.ConnectTimeout;
            request.ReadWriteTimeout = env.ReadTimeout;

            return request;
        }
        public static void Configure(string siteName, string apiKey)
        {
            if (String.IsNullOrEmpty(siteName))
                throw new ArgumentException("Site name can't be empty!");

            if (String.IsNullOrEmpty(apiKey))
                throw new ArgumentException("Api key can't be empty!");

            m_instance = new ApiConfig(siteName, apiKey);
        }
Exemple #7
0
        private static async Task <EntityResult> GetEntityResultAsync(String url, Params parameters, Dictionary <string, string> headers, ApiConfig env, HttpMethod meth)
        {
            HttpRequestMessage request = GetRequestMessage(url, meth, parameters, headers, env);
            var response = await httpClient.SendAsync(request).ConfigureAwait(false);

            var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                EntityResult result = new EntityResult(response.StatusCode, json);
                return(result);
            }
            else
            {
                HandleException(response);
                return(null);
            }
        }
Exemple #8
0
        private static HttpRequestMessage GetRequestMessage(string url, HttpMethod method, Params parameters, Dictionary <string, string> headers, ApiConfig env)
        {
            HttpRequestMessage request = BuildRequest(url, method, parameters, env);

            AddHeaders(request, env);
            AddCustomHeaders(request, headers);
            return(request);
        }
Exemple #9
0
 public static void Configure(string siteName, string apiKey)
 {
     m_instance = new ApiConfig(siteName, apiKey);
 }
Exemple #10
0
        public static async Task <ListResult> GetListAsync(string url, Params parameters, Dictionary <string, string> headers, ApiConfig env)
        {
            url = String.Format("{0}?{1}", url, parameters.GetQuery(true));
            HttpRequestMessage request = GetRequestMessage(url, HttpMethod.GET, parameters, headers, env);
            var response = await httpClient.SendAsync(request).ConfigureAwait(false);

            var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                ListResult result = new ListResult(response.StatusCode, json);
                return(result);
            }
            else
            {
                HandleException(response);
                return(null);
            }
        }
Exemple #11
0
        private static HttpRequestMessage BuildRequest(string url, HttpMethod method, Params parameters, ApiConfig env)
        {
            HttpRequestMessage request;

            System.Net.Http.HttpMethod meth = new System.Net.Http.HttpMethod(method.ToString());
            if (method.Equals(HttpMethod.POST))
            {
                byte[] paramBytes = Encoding.GetEncoding(env.Charset).GetBytes(parameters.GetQuery(false));
                string postData   = Encoding.GetEncoding(env.Charset).GetString(paramBytes, 0, paramBytes.Length);
                request = new HttpRequestMessage(meth, new Uri(url))
                {
                    Content = new StringContent(postData, Encoding.UTF8, "application/x-www-form-urlencoded")
                };
            }
            else
            {
                request = new HttpRequestMessage(meth, new Uri(url));
            }
            return(request);
        }
Exemple #12
0
 public static ListResult GetList(string url, Params parameters, Dictionary <string, string> headers, ApiConfig env)
 {
     return(GetListAsync(url, parameters, headers, env).GetAwaiter().GetResult());
 }
Exemple #13
0
 public static Task <EntityResult> GetAsync(string url, Params parameters, Dictionary <string, string> headers, ApiConfig env)
 {
     url = String.Format("{0}?{1}", url, parameters.GetQuery(false));
     return(GetEntityResultAsync(url, parameters, headers, env, HttpMethod.GET));
 }
Exemple #14
0
 public static Task <EntityResult> PostAsync(string url, Params parameters, Dictionary <string, string> headers, ApiConfig env)
 {
     return(GetEntityResultAsync(url, parameters, headers, env, HttpMethod.POST));
 }
Exemple #15
0
 public static EventListRequest List(ApiConfig apiConfig)
 {
     string url = ApiUtil.BuildUrl(apiConfig, "events");
     return new EventListRequest(url);
 }
Exemple #16
0
 public ListResult Request(ApiConfig env)
 {
     return(ApiUtil.GetList(m_url, m_params, headers, ApiConfig.Instance));
 }
Exemple #17
0
 public static EntityRequest<Type> Retrieve(ApiConfig apiConfig, string id)
 {
     string url = ApiUtil.BuildUrl(apiConfig, "events", CheckNull(id));
     return new EntityRequest<Type>(url, HttpMethod.GET);
 }
 private static void AddHeaders(HttpWebRequest request, ApiConfig env)
 {
     request.Headers.Add(HttpRequestHeader.AcceptCharset, env.Charset);
     request.Headers.Add(HttpRequestHeader.Authorization, env.AuthValue);
 }
 public ListResult Request(ApiConfig env)
 {
     return ApiUtil.GetList(m_url, m_params, headers, env);
 }
Exemple #20
0
 private static EntityResult GetEntityResult(String url, Params parameters, Dictionary <string, string> headers, ApiConfig env, HttpMethod meth)
 {
     return(GetEntityResultAsync(url, parameters, headers, env, meth).ConfigureAwait(false).GetAwaiter().GetResult());
 }