/// <summary> /// Removes contacts from one ore more contact lists. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="emailAddresses">List of email addresses.</param> /// <param name="lists">List of id's.</param> /// <returns>Returns an Activity object.</returns> public Activity RemoveContactsFromLists(string accessToken, string apiKey, IList <string> emailAddresses, IList <string> lists) { Activity activity = null; string url = String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.RemoveFromListsActivity); RemoveContact removeContact = new RemoveContact() { ImportData = new List <ImportEmailAddress>() { new ImportEmailAddress() { EmailAddresses = emailAddresses } }, Lists = lists }; string json = removeContact.ToJSON(); CUrlResponse response = RestClient.Post(url, accessToken, apiKey, json); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { activity = response.Get <Activity>(); } return(activity); }
/// <summary> /// Get an activity. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="activityId">The activity identification.</param> /// <param name="status">The activity status.</param> /// <param name="type">The activity type.</param> /// <returns>Returns the activity identified by its id.</returns> public IList <Activity> GetDetailedReport(string accessToken, string apiKey, string activityId, ActivityStatus?status, ActivityType?type) { IList <Activity> activities = new List <Activity>(); string url = String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.Activities); if (status != null) { url = String.Concat(url, "?status=", status.ToString()); } if (type != null) { url = String.Concat(url, (status != null) ? "&type=" : "?type=", type.ToString()); } CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { activities = response.Get <IList <Activity> >(); } return(activities); }
/// <summary> /// Update a specific email campaign. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="campaign">Campaign to be updated.</param> /// <returns>Returns a campaign.</returns> public EmailCampaign UpdateCampaign(string accessToken, string apiKey, EmailCampaign campaign) { EmailCampaign emailCampaign = null; string url = String.Concat(Config.Endpoints.BaseUrl, String.Format(Config.Endpoints.Campaign, campaign.Id)); // Invalidate data that are not supported by the update API procedure campaign.CreatedDate = null; campaign.TrackingSummary = null; campaign.ModifiedDate = null; campaign.IsVisibleInUI = null; // Convert to JSON string string json = campaign.ToJSON(); CUrlResponse response = RestClient.Put(url, accessToken, apiKey, json); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { emailCampaign = response.Get <EmailCampaign>(); } return(emailCampaign); }
/// <summary> /// Delete an existing attributes for an item /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token</param> /// <param name="apiKey">The API key for the application</param> /// <param name="eventId">Event id</param> /// <param name="itemId">EventItem id</param> /// <param name="attributeId">Attribute id</param> /// <returns>True if successfuly deleted</returns> public bool DeleteEventItemAttribute(string accessToken, string apiKey, string eventId, string itemId, string attributeId) { string url = String.Format(String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.ItemAttribute), eventId, itemId, attributeId); CUrlResponse response = RestClient.Delete(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } return(response.StatusCode == System.Net.HttpStatusCode.NoContent); }
/// <summary> /// Delete a Contact List. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="listId">List id.</param> /// <returns>return true if list was deleted successfully, false otherwise</returns> public bool DeleteList(string accessToken, string apiKey, string listId) { string url = String.Concat(Config.Endpoints.BaseUrl, String.Format(Config.Endpoints.List, listId)); CUrlResponse response = RestClient.Delete(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } return(!response.IsError && response.StatusCode == System.Net.HttpStatusCode.NoContent); }
/// <summary> /// Retrieve an event specified by the event_id /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token</param> /// <param name="apiKey">The API key for the application</param> /// <param name="eventId">Event id</param> /// <returns>The event</returns> public IndividualEvent GetEventSpot(string accessToken, string apiKey, string eventId) { string url = String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.EventSpots, "/", eventId); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.HasData) { return(response.Get <IndividualEvent>()); } else if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } return(new IndividualEvent()); }
/// <summary> /// Retrieve all existing attributes for an item /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token</param> /// <param name="apiKey">The API key for the application</param> /// <param name="eventId">Event id</param> /// <param name="itemId">EventItem id</param> /// <returns>A list of Attributes</returns> public List <CTCT.Components.EventSpot.Attribute> GetAllEventItemAttributes(string accessToken, string apiKey, string eventId, string itemId) { string url = String.Format(String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.ItemAttribute), eventId, itemId, null); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.HasData) { return(response.Get <List <CTCT.Components.EventSpot.Attribute> >()); } else if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } return(new List <CTCT.Components.EventSpot.Attribute>()); }
/// <summary> /// View all existing events /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token</param> /// <param name="apiKey">The API key for the application</param> /// <param name="limit">Specifies the number of results per page in the output, from 1 - 500, default = 50</param> /// <param name="pag">Pagination object</param> /// <returns>ResultSet containing a results array of IndividualEvents</returns> public ResultSet <IndividualEvent> GetAllEventSpots(string accessToken, string apiKey, int?limit, Pagination pag) { string url = (pag == null) ? String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.EventSpots, GetQueryParameters(new object[] { "limit", limit })) : pag.GetNextUrl(); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.HasData) { return(response.Get <ResultSet <IndividualEvent> >()); } else if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } return(new ResultSet <IndividualEvent>()); }
/// <summary> /// Retrieve specific event item /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token</param> /// <param name="apiKey">The API key for the application</param> /// <param name="eventId">Event id</param> /// <param name="itemId">Eventitem id</param> /// <returns>EventItem object</returns> public EventItem GetEventItem(string accessToken, string apiKey, string eventId, string itemId) { string url = String.Format(String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.EventItem), eventId, itemId); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.HasData) { return(response.Get <EventItem>()); } else if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } return(new EventItem()); }
/// <summary> /// Retrieve a list of registrants for the specified event /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token</param> /// <param name="apiKey">The API key for the application</param> /// <param name="eventId">Event id</param> /// <returns>ResultSet containing a results array of Registrant</returns> public ResultSet <Registrant> GetAllRegistrants(string accessToken, string apiKey, string eventId) { string url = String.Format(String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.EventRegistrant), eventId, null); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.HasData) { return(response.Get <ResultSet <Registrant> >()); } else if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } return(new ResultSet <Registrant>()); }
/// <summary> /// Retrieve an existing promo codes for an event /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token</param> /// <param name="apiKey">The API key for the application</param> /// <param name="eventId">Event id</param> /// <param name="promocodeId">Promocode id</param> /// <returns>The Promocode object</returns> public Promocode GetPromocode(string accessToken, string apiKey, string eventId, string promocodeId) { string url = String.Format(String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.EventPromocode), eventId, promocodeId); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.HasData) { return(response.Get <Promocode>()); } else if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } return(new Promocode()); }
/// <summary> /// Publish an event /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token</param> /// <param name="apiKey">The API key for the application</param> /// <param name="eventSpot">The event to publish</param> /// <returns>The published event</returns> public IndividualEvent PostEventSpot(string accessToken, string apiKey, IndividualEvent eventSpot) { string url = String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.EventSpots); string json = eventSpot.ToJSON(); CUrlResponse response = RestClient.Post(url, accessToken, apiKey, json); if (response.HasData) { return(response.Get <IndividualEvent>()); } else if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } return(new IndividualEvent()); }
/// <summary> /// Create a specific event item /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token</param> /// <param name="apiKey">The API key for the application</param> /// <param name="eventId">Event id</param> /// <param name="eventItem">EventItem id</param> /// <returns>The newly created EventItem</returns> public EventItem PostEventItem(string accessToken, string apiKey, string eventId, EventItem eventItem) { string url = String.Format(String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.EventItem), eventId, null); string json = eventItem.ToJSON(); CUrlResponse response = RestClient.Post(url, accessToken, apiKey, json); if (response.HasData) { return(response.Get <EventItem>()); } else if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } return(new EventItem()); }
/// <summary> /// Updates an existing attributes for an item /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token</param> /// <param name="apiKey">The API key for the application</param> /// <param name="eventId">Event id</param> /// <param name="itemId">EventItem id</param> /// <param name="attributeId">Attribute id</param> /// <param name="attribute">Attribute new values</param> /// <returns>The newly updated attribute</returns> public CTCT.Components.EventSpot.Attribute PutEventItemAttribute(string accessToken, string apiKey, string eventId, string itemId, string attributeId, CTCT.Components.EventSpot.Attribute attribute) { string url = String.Format(String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.ItemAttribute), eventId, itemId, attributeId); string json = attribute.ToJSON(); CUrlResponse response = RestClient.Put(url, accessToken, apiKey, json); if (response.HasData) { return(response.Get <CTCT.Components.EventSpot.Attribute>()); } else if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } return(new CTCT.Components.EventSpot.Attribute()); }
/// <summary> /// Returns a list with the last 50 bulk activities. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token.</param> /// <param name="apiKey">The API key for the application</param> /// <returns>Returns the list of activities.</returns> public IList <Activity> GetLast50BulkActivities(string accessToken, string apiKey) { IList <Activity> activities = new List <Activity>(); string url = String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.Activities); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { activities = response.Get <IList <Activity> >(); } return(activities); }
/// <summary> /// Get all existing MyLibrary folders /// </summary> /// <param name="accessToken">Access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="sortBy">Specifies how the list of folders is sorted</param> /// <param name="limit">Specifies the number of results per page in the output, from 1 - 50, default = 50.</param> /// <param name="pag">Pagination object.</param> /// <returns>Returns a collection of MyLibraryFolder objects.</returns> public ResultSet <MyLibraryFolder> GetLibraryFolders(string accessToken, string apiKey, FoldersSortBy?sortBy, int?limit, Pagination pag) { ResultSet <MyLibraryFolder> results = null; string url = (pag == null) ? String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.MyLibraryFolders, GetQueryParameters(new object[] { "sort_by", sortBy, "limit", limit })) : pag.GetNextUrl(); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { results = response.Get <ResultSet <MyLibraryFolder> >(); } return(results); }
/// <summary> /// Get status for an upload file /// </summary> /// <param name="accessToken">Access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="fileId">The id of the file</param> /// <returns>Returns a list of FileUploadStatus objects</returns> public IList <FileUploadStatus> GetLibraryFileUploadStatus(string accessToken, string apiKey, string fileId) { IList <FileUploadStatus> lists = null; string url = String.Concat(Config.Endpoints.BaseUrl, string.Format(Config.Endpoints.MyLibraryFileUploadStatus, fileId)); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { lists = response.Get <IList <FileUploadStatus> >(); } return(lists); }
/// <summary> /// Get file after id /// </summary> /// <param name="accessToken">Access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="fileId">The id of the file</param> /// <returns>Returns a MyLibraryFile object.</returns> public MyLibraryFile GetLibraryFile(string accessToken, string apiKey, string fileId) { MyLibraryFile file = null; string url = String.Concat(Config.Endpoints.BaseUrl, String.Format(Config.Endpoints.MyLibraryFile, fileId)); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { file = Component.FromJSON <MyLibraryFile>(response.Body); } return(file); }
/// <summary> /// Get MyLibrary usage information /// </summary> /// <param name="accessToken">Access token.</param> /// <param name="apiKey">The API key for the application</param> /// <returns>Returns a MyLibraryInfo object</returns> public MyLibraryInfo GetLibraryInfo(string accessToken, string apiKey) { MyLibraryInfo result = null; string url = String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.MyLibraryInfo); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { result = response.Get <MyLibraryInfo>(); } return(result); }
/// <summary> /// Get a list of schedules for a campaign. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="campaignId">Campaign id to be scheduled.</param> /// <returns>Returns the list of schedules for the specified campaign.</returns> public IList <Schedule> GetSchedules(string accessToken, string apiKey, string campaignId) { IList <Schedule> list = new List <Schedule>(); string url = String.Concat(Config.Endpoints.BaseUrl, String.Format(Config.Endpoints.CampaignSchedules, campaignId)); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { list = Component.FromJSON <IList <Schedule> >(response.Body); } return(list); }
/// <summary> /// Create a new schedule for a campaign. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="campaignId">Campaign id to be scheduled.</param> /// <param name="schedule">Schedule to be created.</param> /// <returns>Returns the schedule added.</returns> public Schedule AddSchedule(string accessToken, string apiKey, string campaignId, Schedule schedule) { Schedule sch = null; string url = String.Concat(Config.Endpoints.BaseUrl, String.Format(Config.Endpoints.CampaignSchedules, campaignId)); string json = schedule.ToJSON(); CUrlResponse response = RestClient.Post(url, accessToken, apiKey, json); if (response.HasData) { sch = Component.FromJSON <Schedule>(response.Body); } else if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } return(sch); }
/// <summary> /// Get bounces for a given contact. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="contactId">Contact id.</param> /// <param name="limit">Specifies the number of results per page in the output, from 1 - 500, default = 500.</param> /// <param name="pag">Pagination object.</param> /// <returns>ResultSet containing a results array of @link BounceActivity</returns> private ResultSet <BounceActivity> GetBounces(string accessToken, string apiKey, string contactId, int?limit, Pagination pag) { ResultSet <BounceActivity> results = null; string url = (pag == null) ? Config.ConstructUrl(Config.Endpoints.ContactTrackingBounces, new object[] { contactId }, new object[] { "limit", limit }) : pag.GetNextUrl(); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { results = Component.FromJSON <ResultSet <BounceActivity> >(response.Body); } return(results); }
/// <summary> /// Returns the status of the specified activity. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token.</param> /// <param name="apiKey">The API key for the application.</param> /// <param name="activityId">The activity id.</param> /// <returns></returns> public Activity GetActivity(string accessToken, string apiKey, string activityId) { Activity activity = null; string url = String.Concat(Config.Endpoints.BaseUrl, String.Format(Config.Endpoints.Activity, activityId)); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { activity = response.Get <Activity>(); } return(activity); }
/// <summary> /// Get a specific schedule for a campaign. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="campaignId">Campaign id to be get a schedule for.</param> /// <param name="scheduleId">Schedule id to retrieve.</param> /// <returns>Returns the requested schedule object.</returns> public Schedule GetSchedule(string accessToken, string apiKey, string campaignId, string scheduleId) { Schedule schedule = null; string url = String.Concat(Config.Endpoints.BaseUrl, String.Format(Config.Endpoints.CampaignSchedule, campaignId, scheduleId)); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { schedule = Component.FromJSON <Schedule>(response.Body); } return(schedule); }
/// <summary> /// Get an individual contact list. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token</param> /// <param name="apiKey">The API key for the application</param> /// <param name="listId">List id.</param> /// <returns>Returns a contact list.</returns> public ContactList GetList(string accessToken, string apiKey, string listId) { ContactList list = null; string url = String.Concat(Config.Endpoints.BaseUrl, String.Format(Config.Endpoints.List, listId)); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { list = Component.FromJSON <ContactList>(response.Body); } return(list); }
/// <summary> /// Get all contacts from an individual list. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="listId">List id to retrieve contacts for.</param> /// <param name="limit">Specifies the number of results per page in the output, from 1 - 500, default = 500.</param> /// <param name="modifiedSince">limit contacts retrieved to contacts modified since the supplied date</param> /// <param name="pag">Pagination object.</param> /// <returns>Returns a list of contacts.</returns> private ResultSet <Contact> GetContactsFromList(string accessToken, string apiKey, string listId, int?limit, DateTime?modifiedSince, Pagination pag) { var contacts = new ResultSet <Contact>(); string url = (pag == null) ? Config.ConstructUrl(Config.Endpoints.ListContacts, new object[] { listId }, new object[] { "limit", limit, "modified_since", Extensions.ToISO8601String(modifiedSince) }) : pag.GetNextUrl(); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { contacts = response.Get <ResultSet <Contact> >(); } return(contacts); }
/// <summary> /// Get lists within an account. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="modifiedSince">limit contacts retrieved to contacts modified since the supplied date</param> /// <returns>Returns a list of contact lists.</returns> public IList <ContactList> GetLists(string accessToken, string apiKey, DateTime?modifiedSince) { IList <ContactList> lists = new List <ContactList>(); string url = String.Concat(Config.Endpoints.BaseUrl, Config.Endpoints.Lists); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { lists = Component.FromJSON <IList <ContactList> >(response.Body); } return(lists); }
/// <summary> /// Get a result set of bounces for a given campaign. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="campaignId">Campaign id.</param> /// <param name="limit">Specifies the number of results per page in the output, from 1 - 500, default = 500.</param> /// <param name="createdSince">Filter for bounces created since the supplied date in the collection</param> /// <param name="pag">Pagination object.</param> /// <returns>ResultSet containing a results array of @link BounceActivity.</returns> private ResultSet <BounceActivity> GetBounces(string accessToken, string apiKey, string campaignId, int?limit, DateTime?createdSince, Pagination pag) { ResultSet <BounceActivity> results = null; string url = (pag == null) ? Config.ConstructUrl(Config.Endpoints.CampaignTrackingBounces, new object[] { campaignId }, new object[] { "limit", limit, "created_since", Extensions.ToISO8601String(createdSince) }) : pag.GetNextUrl(); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { results = Component.FromJSON <ResultSet <BounceActivity> >(response.Body); } return(results); }
/// <summary> /// Get a summary of reporting data for a given campaign. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="campaignId">Campaign id.</param> /// <returns>Tracking summary.</returns> public TrackingSummary GetSummary(string accessToken, string apiKey, string campaignId) { TrackingSummary summary = null; string url = Config.ConstructUrl(Config.Endpoints.CampaignTrackingSummary, new object[] { campaignId }, null); CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } if (response.HasData) { summary = Component.FromJSON <TrackingSummary>(response.Body); } return(summary); }
/// <summary> /// Get ClickActivity at the given url. /// </summary> /// <param name="accessToken">Constant Contact OAuth2 access token.</param> /// <param name="apiKey">The API key for the application</param> /// <param name="url">The url from which to retrieve an array of @link ClickActivity.</param> /// <returns>ResultSet containing a results array of @link ClickActivity.</returns> private ResultSet <ClickActivity> GetClicks(string accessToken, string apiKey, string url) { CUrlResponse response = RestClient.Get(url, accessToken, apiKey); if (response.IsError) { throw new CtctException(response.GetErrorMessage()); } ResultSet <ClickActivity> results = null; if (response.HasData) { results = Component.FromJSON <ResultSet <ClickActivity> >(response.Body); } return(results); }
private CUrlResponse HttpRequest(string url, string method, Configuration configuration, byte[] data, bool? isMultipart) { // Initialize the response HttpWebResponse response = null; string responseText = null; CUrlResponse urlResponse = new CUrlResponse(); ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true); var address = ""; // url; address = string.Concat(configuration.ServiceUrl, url); //Moved apiKey to header value //if (!string.IsNullOrEmpty(apiKey)) //{ // address = string.Format("{0}{1}api_key={2}", url, url.Contains("?") ? "&" : "?", apiKey); //} HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; request.Method = method; request.Accept = "application/json"; if(isMultipart.HasValue && isMultipart.Value) { request.ContentType = "multipart/form-data; boundary=" + MultipartBuilder.MULTIPART_BOUNDARY; } else { request.ContentType = "application/json"; } // Add token as HTTP header //request.Headers.Add("Authorization", "Bearer " + accessToken); request.Headers.Add("Authorization-Token", configuration.ApiKey); if (data != null) { using (var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } // Now try to send the request try { response = request.GetResponse() as HttpWebResponse; //try //{ // response = request.GetResponse() as HttpWebResponse; //} //catch (WebException ex) //{ // if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound) // { // response = ex.Response as HttpWebResponse; // } //} // Expect the unexpected if (request.HaveResponse == true && response == null) { throw new WebException("Response was not returned or is null"); } foreach(string header in response.Headers.AllKeys) { urlResponse.Headers.Add(header, response.GetResponseHeader(header)); } urlResponse.StatusCode = response.StatusCode; if (response.StatusCode != HttpStatusCode.OK) { throw new WebException("Response with status: " + response.StatusCode + " " + response.StatusDescription); } } catch (WebException e) { //if (e.Response != null) //{ // response = (HttpWebResponse)e.Response; // urlResponse.IsError = true; //} response = e.Response as HttpWebResponse; urlResponse.IsError = true; } finally { if (response != null) { // Get the response content using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseText = reader.ReadToEnd(); } response.Close(); if (urlResponse.IsError && responseText.Contains("error_message")) { urlResponse.Info = CUrlRequestError.FromJSON<IList<CUrlRequestError>>(responseText); } else { urlResponse.Body = responseText; } } } return urlResponse; }
private CUrlResponse HttpRequest(string url, string method, string accessToken, string apiKey, byte[] data, bool? isMultipart) { // Initialize the response HttpWebResponse response = null; string responseText = null; CUrlResponse urlResponse = new CUrlResponse(); var address = url; if (!string.IsNullOrEmpty(apiKey)) { address = string.Format("{0}{1}api_key={2}", url, url.Contains("?") ? "&" : "?", apiKey); } HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; request.Method = method; request.Accept = "application/json"; request.Headers["x-ctct-request-source"] = "sdk.NET." + GetWrapperAssemblyVersion().ToString(); if(isMultipart.HasValue && isMultipart.Value) { request.ContentType = "multipart/form-data; boundary=" + MultipartBuilder.MULTIPART_BOUNDARY; } else { request.ContentType = "application/json"; } // Add token as HTTP header request.Headers.Add("Authorization", "Bearer " + accessToken); if (data != null) { using (var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } // Now try to send the request try { response = request.GetResponse() as HttpWebResponse; // Expect the unexpected if (request.HaveResponse == true && response == null) { throw new WebException("Response was not returned or is null"); } foreach(string header in response.Headers.AllKeys) { urlResponse.Headers.Add(header, response.GetResponseHeader(header)); } urlResponse.StatusCode = response.StatusCode; if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Created && response.StatusCode != HttpStatusCode.Accepted && response.StatusCode != HttpStatusCode.NoContent) { throw new WebException("Response with status: " + response.StatusCode + " " + response.StatusDescription); } } catch (WebException e) { response = e.Response as HttpWebResponse; urlResponse.IsError = true; } finally { if (response != null) { // Get the response content using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseText = reader.ReadToEnd(); } response.Close(); if (urlResponse.IsError && responseText.Contains("error_message")) { urlResponse.Info = CUrlRequestError.FromJSON<IList<CUrlRequestError>>(responseText); } else { urlResponse.Body = responseText; } } } return urlResponse; }
private CUrlResponse HttpRequest(string url, string method, string accessToken, string apiKey, string data) { // Initialize the response HttpWebResponse response = null; string responseText = null; CUrlResponse urlResponse = new CUrlResponse(); var address = url; if (!string.IsNullOrEmpty(apiKey)) { address = string.Format("{0}{1}api_key={2}", url, url.Contains("?") ? "&" : "?", apiKey); } HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; request.Method = method; request.ContentType = "application/json"; request.Accept = "application/json"; // Add token as HTTP header request.Headers.Add("Authorization", "Bearer " + accessToken); if (data != null) { // Convert the request contents to a byte array and include it byte[] requestBodyBytes = System.Text.Encoding.UTF8.GetBytes(data); request.GetRequestStream().Write(requestBodyBytes, 0, requestBodyBytes.Length); } // Now try to send the request try { response = request.GetResponse() as HttpWebResponse; // Expect the unexpected if (request.HaveResponse == true && response == null) { throw new WebException("Response was not returned or is null"); } urlResponse.StatusCode = response.StatusCode; if (response.StatusCode != HttpStatusCode.OK) { throw new WebException("Response with status: " + response.StatusCode + " " + response.StatusDescription); } } catch (WebException e) { if (e.Response != null) { response = (HttpWebResponse)e.Response; urlResponse.IsError = true; } } finally { if (response != null) { // Get the response content using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseText = reader.ReadToEnd(); } response.Close(); if (urlResponse.IsError && responseText.Contains("error_message")) { urlResponse.Info = CUrlRequestError.FromJSON<IList<CUrlRequestError>>(responseText); } else { urlResponse.Body = responseText; } } } return urlResponse; }