/// <summary> /// creates HttpWebRequest including IDataBase data, request params, query filter and granularity /// </summary> /// <param name="requestMethod">RequestMethod</param> /// <param name="model">Ataxo.SlimAPIClient.Entities.Constants.Model</param> /// <param name="requestParams">params od model usually used for updating</param> /// <param name="queryItems">filter params</param> /// <param name="granularity">Granularity</param> /// <returns></returns> protected HttpWebRequest CreateRequest(RequestMethod requestMethod, string model, string additional_concretization = null, IDictionary<string, object> requestParams = null, IEnumerable<QueryItem> queryItems = null, Granularity granularity = null) { string url = ConstrucUrl(PropertiesData, model, additional_concretization, queryItems, granularity); Uri uri = new Uri(url); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.ContentType = "application/json"; request.Headers["Api-Token"] = PropertiesData.AccessToken; request.Method = requestMethod.ToString().ToUpper(); return request; }
/// <summary> /// sends synchronous request to server /// </summary> /// <param name="requestMethod">request method</param> /// <param name="model">model to connect</param> /// <param name="additional_concretization">additional sub_url added behind the backslash</param> /// <param name="requestParams">params of request usually used to update database on server</param> /// <param name="queryItems">specifies server data filtration usually by get request</param> /// <param name="granularity">specifies pagination, limit and order of returning data</param> /// <returns></returns> public virtual ResponseData SendRequestSync(RequestMethod requestMethod, string model, string additional_concretization = null, IDictionary<string, object> requestParams = null, IEnumerable<QueryItem> queryItems = null, Granularity granularity = null) { HttpWebRequest request = CreateRequest(requestMethod, model, additional_concretization, requestParams, queryItems, granularity); request.SetStreamData(requestParams); string result; try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { result = GetResponseString(response); } } catch (WebException ex) { result = GetResponseString(ex.Response); } catch (Exception ex) { throw ex; } return new ResponseData(result); }
protected string ConstrucUrl(IDataBase data, string model, string additional_concretization = null, IEnumerable<QueryItem> queryItems = null, Granularity granularity = null) { return string.Format("{0}/{1}/{2}/{3}{4}?{5}", data.DomainUrl, data.ApiVersion, data.Taxonomy, model, additional_concretization == null ? "" : string.Format("/{0}", additional_concretization), QueryManager.GenerateQuery(queryItems, granularity)); }
/// <summary> /// gets statistics /// </summary> /// <param name="campaignId"></param> /// <param name="granularity"></param> /// <param name="queryItems"></param> /// <returns></returns> public ResponseData GetStatisticsSync(int? campaignId = null, IEnumerable<QueryItem> queryItems = null, Granularity granularity = null) { if (campaignId != null) { if (queryItems == null) { queryItems = new List<QueryItem>(); } ((List<QueryItem>)queryItems).Add(new QueryItem(RequestParamKeys.Statistics.CampaignId, QueryOperators.Equals, new List<object>() { campaignId })); } return SendRequestSync(RequestMethod.Get, Model.Statistics, null, null, queryItems, granularity); }
/// <summary> /// sends asynchronous request to server /// </summary> /// <param name="requestMethod">request method</param> /// <param name="model">model to connect</param> /// <param name="additional_concretization">additional sub_url added behind the backslash</param> /// <param name="requestParams">params of request usually used to update database on server</param> /// <param name="queryItems">specifies server data filtration usually by get request</param> /// <param name="granularity">specifies pagination, limit and order of returning data</param> /// <returns></returns> public virtual void SendRequestAsync(RequestMethod requestMethod, string model, string additional_concretization = null, IDictionary<string, object> requestParams = null, Action<WebResponse, ResponseData> listener = null, IEnumerable<QueryItem> queryItems = null, Granularity granularity = null) { HttpWebRequest request = CreateRequest(requestMethod, model, additional_concretization, requestParams, queryItems, granularity); request.SetStreamData(requestParams); try { IAsyncResult result = request.BeginGetResponse(new AsyncCallback(RespCallback), new Tuple<HttpWebRequest, Action<WebResponse, ResponseData>>(request, listener)); } catch (WebException ex) { string result = GetResponseString(ex.Response); listener(ex.Response, new ResponseData(result)); } catch (Exception ex) { throw ex; } }
/// <summary> /// gets statistics /// </summary> /// <param name="campaignId"></param> /// <param name="listener"></param> /// <param name="granularity"></param> /// <param name="queryItems"></param> public void GetStatisticsAsync(int? campaignId = null, Action<WebResponse, ResponseData> listener = null, IEnumerable<QueryItem> queryItems = null, Granularity granularity = null) { if (campaignId != null) { if (queryItems == null) { queryItems = new List<QueryItem>(); } ((List<QueryItem>)queryItems).Add(new QueryItem(RequestParamKeys.Statistics.CampaignId, QueryOperators.Equals, new List<object>() { campaignId })); } SendRequestAsync(RequestMethod.Get, Model.Statistics, null, null, listener, queryItems, granularity); }
/// <summary> /// gets contracts by client id /// </summary> /// <param name="clientId"></param> /// <param name="granularity"></param> /// <param name="queryItems"></param> /// <returns></returns> public ResponseData GetContractsByClinetIdSync(int clientId, IEnumerable<QueryItem> queryItems = null, Granularity granularity = null) { if (queryItems == null) { queryItems = new List<QueryItem>(); } ((List<QueryItem>)queryItems).Add(new QueryItem(RequestParamKeys.Contract.ClientId, QueryOperators.Equals, new List<object>() { clientId })); return SendRequestSync(RequestMethod.Get, Model.Contracts, null, null, queryItems, granularity); }
/// <summary> /// gets contracts by client id /// </summary> /// <param name="clientId"></param> /// <param name="listener"></param> /// <param name="granularity"></param> /// <param name="queryItems"></param> public void GetContractsByClinetIdASync(int clientId, Action<WebResponse, ResponseData> listener = null, IEnumerable<QueryItem> queryItems = null, Granularity granularity = null) { if (queryItems == null) { queryItems = new List<QueryItem>(); } ((List<QueryItem>)queryItems).Add(new QueryItem(RequestParamKeys.Contract.ClientId, QueryOperators.Equals, new List<object>() { clientId })); SendRequestAsync(RequestMethod.Get, Model.Clients, null, null, listener, queryItems, granularity); }
private static void TmpInitGranularity(out Granularity granularity) { granularity = new Granularity(); granularity.Limit = 3; //granularity.Offset = 1; granularity.PropertyOrders = new List<PropertyOrder>(); PropertyOrder propertyOrder = new PropertyOrder(); propertyOrder.PropertyName = RequestParamKeys.Campaign.Id; propertyOrder.Order = Ordering.Ascending; PropertyOrder propertyOrder1 = new PropertyOrder(); propertyOrder1.PropertyName = RequestParamKeys.Campaign.LimitClicks; propertyOrder1.Order = Ordering.Ascending; granularity.PropertyOrders.Add(propertyOrder); granularity.PropertyOrders.Add(propertyOrder1); }
/// <summary> /// generates query string from array of query items and granularity /// </summary> /// <param name="queryItems"></param> /// <param name="granularity"></param> /// <returns></returns> public static string GenerateQuery(IEnumerable<QueryItem> queryItems, Granularity granularity) { IEnumerable<QueryItem> allQueries = granularity == null ? queryItems : JoinArrays(queryItems, ((Granularity)granularity).ToQueryItems()); return allQueries == null ? null : string.Join("&", allQueries.ToList().Select(q => q.GenerateSubQuery())); }