internal static T Get <T>(AmoCrmConfig crmConfig, params Parameter[] parameters) where T : class, IAmoCrmResponse, new() { var client = new RestClient(crmConfig.GetUrl <T>()) { CookieContainer = GetCookies(crmConfig) }; var request = new RestRequest(Method.GET); request.AddParameter("limit_rows", crmConfig.LimitRows ?? 500); request.AddParameter("limit_offset", crmConfig.LimitOffset ?? 0); foreach (var parameter in parameters) { request.AddParameter(parameter); } if (crmConfig.ModifiedSince.HasValue) { request.AddHeader("If-Modified-Since", crmConfig.ModifiedSince.Value.ToString("u")); } var response = client.Execute(request); if (response.ErrorException != null) { throw new AmoCrmException(response.ErrorMessage, response.ErrorException); } return(response.Content.DeserializeTo <T>()); }
internal static T Post <T>(string url, string data, AmoCrmConfig crmConfig) where T : class, IAmoCrmResponse, new() { var request = (HttpWebRequest)WebRequest.Create(new Uri(url)); request.Method = WebRequestMethods.Http.Post; request.KeepAlive = false; request.CookieContainer = GetCookies(crmConfig); using (var writer = new StreamWriter(request.GetRequestStream())) { if (data != null) { writer.Write(data); } } using (var webResponse = (HttpWebResponse)request.GetResponse()) { using (var responseStream = webResponse.GetResponseStream()) { if (responseStream == null) { throw new NullReferenceException("responseStream"); } using (var responseReader = new StreamReader(responseStream, Encoding.UTF8)) { var responseResult = responseReader.ReadToEnd(); return(responseResult.DeserializeTo <T>()); } } } }
private static CookieContainer GetCookies(AmoCrmConfig crmConfig) { var sessionLimitIsExceeded = DateTime.Now - new TimeSpan(0, 0, AuthSessionLimit, 0) > _lastSessionStarted; if (_cookies == null || sessionLimitIsExceeded) { _cookies = GetNewCookies(crmConfig); } return(_cookies); }
private static CookieContainer GetNewCookies(AmoCrmConfig crmConfig) { var request = new RestRequest(Method.POST); request.AddParameter("USER_LOGIN", crmConfig.UserLogin); request.AddParameter("USER_HASH", crmConfig.UserHash); var response = new RestClient(crmConfig.AuthUrl).Execute(request); var newCookieContainer = new CookieContainer(); foreach (var cookie in response.Cookies) { newCookieContainer.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain)); } _lastSessionStarted = DateTime.Now; return(newCookieContainer); }
internal static T Post <T>(IAmoCrmRequest data, AmoCrmConfig crmConfig) where T : class, IAmoCrmResponse, new() { var client = new RestClient(crmConfig.GetUrl <T>()) { CookieContainer = GetCookies(crmConfig) }; var request = new RestRequest(Method.POST); request.AddParameter(new Parameter { Name = "text/json", Value = JsonConvert.SerializeObject(data), Type = ParameterType.RequestBody }); var response = client.Execute(request); var result = response.Content.DeserializeTo <T>(); //if (result.Error!=null) throw new AmoCrmException(result.Error); return(result); }
public AmoCrmService(AmoCrmConfig crmConfig) { _crmConfig = crmConfig; }
/// <summary> /// Asynchronous method of transmitting data to the server (POST) /// </summary> /// <typeparam name="T">class to serialize</typeparam> /// <param name="url">URL request</param> /// <param name="data">String in json format</param> /// <param name="config">Configuration Class Instance</param> /// <returns>Id of added item</returns> /// <exception cref="ArgumentNullException">Argument is null</exception> /// <exception cref="AmoCrmException">Return error from server</exception> /// <returns></returns> public static Task <T> PostAsync <T>(string url, string data, AmoCrmConfig config) where T : class { return(PostAsync <T>(url, data, GetCookies(new NetworkCredential(config.UserLogin, config.UserHash, config.AuthUrl)))); }
/// <summary> /// Method for obtaining cookies for authorization /// </summary> /// <param name="config">Instance of the library configuration class</param> /// <exception cref="AmoCrmException">Return error from server</exception> /// <returns></returns> public static CookieContainer GetCookies(AmoCrmConfig config) { return(GetCookies(new NetworkCredential(config.UserLogin, config.UserHash, config.AuthUrl))); }
/// <summary> /// Asynchronous method of transmitting data to the server (GET) /// </summary> /// <typeparam name="T">class to serialize</typeparam> /// <param name="url">URL request</param> /// <param name="parameters">Query Parameters: Name, Value</param> /// <param name="config">Class instance AmoCrmConfig</param> /// <exception cref="ArgumentNullException">Argument is null</exception> /// <exception cref="AmoCrmException">Return error from server</exception> /// <returns></returns> public static Task <T> GetAsync <T>(string url, Parameter[] parameters, AmoCrmConfig config) where T : class { return(GetAsync <T>(url, parameters, GetCookies(config), config.LimitRows, config.LimitOffset, config.ModifiedSince)); }