public void UpdateDepartment()
        {
            var departmentRequest = new DepartmentRequest {
                Title = "Title", DisplayOrder = 2, Type = DepartmentType.Public, Id = 12
            };
            var departments = new DepartmentCollection {
                new Department {
                    Title = "Title", DisplayOrder = 2, Type = DepartmentType.Public
                }
            };

            var apiMethod = string.Format("{0}/{1}", ApiBaseMethods.Departments, departmentRequest.Id);

            var parameters = new RequestBodyBuilder();

            parameters.AppendRequestData("title", departmentRequest.Title);
            parameters.AppendRequestData("type", EnumUtility.ToApiString(departmentRequest.Type));
            parameters.AppendRequestData("displayorder", departmentRequest.DisplayOrder);
            parameters.AppendRequestData("uservisibilitycustom", 0);

            this.kayakoApiRequest.Setup(x => x.ExecutePut <DepartmentCollection>(apiMethod, parameters.ToString())).Returns(departments);

            var departmentUpdated = this.departmentController.UpdateDepartment(departmentRequest);

            this.kayakoApiRequest.Verify(x => x.ExecutePut <DepartmentCollection>(apiMethod, parameters.ToString()), Times.Once());
            Assert.That(departmentUpdated, Is.EqualTo(departments.First()));
        }
        public void CreateDepartment()
        {
            var departmentRequest = new DepartmentRequest {
                Title = "Title", DisplayOrder = 2, Type = DepartmentType.Public
            };
            var departments = new DepartmentCollection {
                new Department {
                    Title = "Title", DisplayOrder = 2, Type = DepartmentType.Public
                }
            };

            var parameters = new RequestBodyBuilder();

            parameters.AppendRequestData("title", departmentRequest.Title);
            parameters.AppendRequestData("type", EnumUtility.ToApiString(departmentRequest.Type));
            parameters.AppendRequestData("module", EnumUtility.ToApiString(departmentRequest.Module));
            parameters.AppendRequestData("displayorder", departmentRequest.DisplayOrder);
            parameters.AppendRequestData("uservisibilitycustom", 0);

            _kayakoApiRequest.Setup(x => x.ExecutePost <DepartmentCollection>(ApiBaseMethods.Departments, parameters.ToString())).Returns(departments);

            var departmentCreated = _departmentController.CreateDepartment(departmentRequest);

            _kayakoApiRequest.Verify(x => x.ExecutePost <DepartmentCollection>(ApiBaseMethods.Departments, parameters.ToString()), Times.Once());
            Assert.That(departmentCreated, Is.EqualTo(departments.First()));
        }
        public NewsItemComment CreateNewsItemComment(NewsItemCommentRequest newsItemCommentRequest)
        {
            newsItemCommentRequest.EnsureValidData(RequestTypes.Create);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            parameters.AppendRequestData("newsitemid", newsItemCommentRequest.NewsItemId);
            parameters.AppendRequestDataNonEmptyString("contents", newsItemCommentRequest.Contents);
            parameters.AppendRequestData("creatortype", EnumUtility.ToApiString(newsItemCommentRequest.CreatorType));

            if (newsItemCommentRequest.CreatorId != null)
            {
                parameters.AppendRequestData("creatorid", newsItemCommentRequest.CreatorId);
            }
            else
            {
                parameters.AppendRequestDataNonEmptyString("fullname", newsItemCommentRequest.FullName);
            }

            parameters.AppendRequestDataNonEmptyString("email", newsItemCommentRequest.Email);
            parameters.AppendRequestData("parentcommentid", newsItemCommentRequest.ParentCommentId);

            var newsItemComments = Connector.ExecutePost <NewsItemCommentCollection>(NewsItemCommentBaseUrl, parameters.ToString());

            if (newsItemComments != null && newsItemComments.Count > 0)
            {
                return(newsItemComments[0]);
            }

            return(null);
        }
        /// <summary>
        /// Add a new time tracking note to a ticket
        /// </summary>
        public TicketTimeTrack AddTicketTimeTrackingNote(TicketTimeTrackRequest request)
        {
            request.EnsureValidData(RequestTypes.Create);

            string apiMethod = "/Tickets/TicketTimeTrack";

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            parameters.AppendRequestData("ticketid", request.TicketId);
            parameters.AppendRequestData("contents", request.Contents);
            parameters.AppendRequestData("staffid", request.StaffId);
            parameters.AppendRequestData("worktimeline", request.WorkTimeline);
            parameters.AppendRequestData("billtimeline", request.BillTimeline);
            parameters.AppendRequestData("timespent", request.TimeSpent);
            parameters.AppendRequestData("timebillable", request.TimeBillable);

            if (request.WorkerStaffId != null)
            {
                parameters.AppendRequestData("workerstaffid", request.WorkerStaffId);
            }

            if (request.NoteColor != null)
            {
                parameters.AppendRequestData("notecolor", EnumUtility.ToApiString(request.NoteColor));
            }

            TicketTimeTrackCollection ticketTimeTracks = Connector.ExecutePost <TicketTimeTrackCollection>(apiMethod, parameters.ToString());

            if (ticketTimeTracks != null && ticketTimeTracks.Count > 0)
            {
                return(ticketTimeTracks[0]);
            }

            return(null);
        }
        private RequestBodyBuilder PopulateRequestParameters(TroubleshooterStepRequest troubleshooterStepRequest, RequestTypes requestType)
        {
            troubleshooterStepRequest.EnsureValidData(requestType);

            var parameters = new RequestBodyBuilder();

            if (requestType == RequestTypes.Create)
            {
                parameters.AppendRequestData("categoryid", troubleshooterStepRequest.CategoryId);
            }

            parameters.AppendRequestDataNonEmptyString("subject", troubleshooterStepRequest.Subject);
            parameters.AppendRequestDataNonEmptyString("contents", troubleshooterStepRequest.Contents);

            parameters.AppendRequestDataNonNegativeInt(requestType == RequestTypes.Create ? "staffid" : "editedstaffid",
                                                       troubleshooterStepRequest.StaffId);

            if (troubleshooterStepRequest.DisplayOrder.HasValue)
            {
                parameters.AppendRequestDataNonNegativeInt("displayorder", troubleshooterStepRequest.DisplayOrder.Value);
            }

            parameters.AppendRequestDataBool("allowcomments", troubleshooterStepRequest.AllowComments);
            parameters.AppendRequestDataBool("enableticketredirection", troubleshooterStepRequest.EnableTicketRedirection);

            if (troubleshooterStepRequest.RedirectDepartmentId.HasValue)
            {
                parameters.AppendRequestDataNonNegativeInt("redirectdepartmentid", troubleshooterStepRequest.RedirectDepartmentId.Value);
            }

            if (troubleshooterStepRequest.TicketTypeId.HasValue)
            {
                parameters.AppendRequestDataNonNegativeInt("tickettypeid", troubleshooterStepRequest.TicketTypeId.Value);
            }

            if (troubleshooterStepRequest.TicketPriorityId.HasValue)
            {
                parameters.AppendRequestDataNonNegativeInt("ticketpriorityid", troubleshooterStepRequest.TicketPriorityId.Value);
            }

            parameters.AppendRequestDataNonEmptyString("ticketsubject", troubleshooterStepRequest.TicketSubject);

            if (troubleshooterStepRequest.StepStatus.HasValue)
            {
                parameters.AppendRequestData("stepstatus", EnumUtility.ToApiString(troubleshooterStepRequest.StepStatus.Value));
            }

            parameters.AppendRequestDataArrayCommaSeparated("parentstepidlist", troubleshooterStepRequest.ParentStepIdList);

            return(parameters);
        }
        private static RequestBodyBuilder PopulateRequestParameters(StaffGroupRequest staffGroup, RequestTypes requestType)
        {
            staffGroup.EnsureValidData(requestType);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            if (!String.IsNullOrEmpty(staffGroup.Title))
            {
                parameters.AppendRequestData("title", staffGroup.Title);
            }

            parameters.AppendRequestData("isadmin", Convert.ToInt32(staffGroup.IsAdmin));

            return(parameters);
        }
        private static RequestBodyBuilder PopulateRequestParameters(NewsCategoryRequest newsCategory, RequestTypes requestType)
        {
            newsCategory.EnsureValidData(requestType);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            if (!String.IsNullOrEmpty(newsCategory.Title))
            {
                parameters.AppendRequestData("title", newsCategory.Title);
            }

            parameters.AppendRequestData("visibilitytype", EnumUtility.ToApiString(newsCategory.VisibilityType));

            return(parameters);
        }
        private RequestBodyBuilder PopulateRequestParameters(TroubleshooterCategoryRequest troubleshooterCategoryRequest, RequestTypes requestType)
        {
            troubleshooterCategoryRequest.EnsureValidData(requestType);

            var parameters = new RequestBodyBuilder();

            parameters.AppendRequestDataNonEmptyString("title", troubleshooterCategoryRequest.Title);
            parameters.AppendRequestData("categorytype", EnumUtility.ToApiString(troubleshooterCategoryRequest.CategoryType));

            if (requestType == RequestTypes.Create)
            {
                parameters.AppendRequestDataNonNegativeInt("staffid", troubleshooterCategoryRequest.StaffId);
            }

            if (troubleshooterCategoryRequest.DisplayOrder.HasValue)
            {
                parameters.AppendRequestDataNonNegativeInt("displayorder", troubleshooterCategoryRequest.DisplayOrder.Value);
            }

            parameters.AppendRequestDataNonEmptyString("description", troubleshooterCategoryRequest.Description);
            parameters.AppendRequestDataBool("uservisibilitycustom", troubleshooterCategoryRequest.UserVisibilityCustom);
            parameters.AppendRequestDataArrayCommaSeparated("usergroupidlist", troubleshooterCategoryRequest.UserGroupIdList);
            parameters.AppendRequestDataBool("staffvisibilitycustom", troubleshooterCategoryRequest.StaffVisibilityCustom);
            parameters.AppendRequestDataArrayCommaSeparated("staffgroupidlist", troubleshooterCategoryRequest.StaffGroupIdList);

            return(parameters);
        }
Exemple #9
0
        private RequestBodyBuilder PopulateRequestParameters(KnowledgebaseArticleRequest knowledgebaseArticleRequest, RequestTypes requestType)
        {
            knowledgebaseArticleRequest.EnsureValidData(requestType);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            parameters.AppendRequestDataNonEmptyString("subject", knowledgebaseArticleRequest.Subject);
            parameters.AppendRequestDataNonEmptyString("contents", knowledgebaseArticleRequest.Contents);

            if (requestType == RequestTypes.Create && knowledgebaseArticleRequest.CreatorId.HasValue)
            {
                parameters.AppendRequestDataNonNegativeInt("creatorid", knowledgebaseArticleRequest.CreatorId.Value);
            }

            if (knowledgebaseArticleRequest.ArticleStatus.HasValue)
            {
                parameters.AppendRequestData("articlestatus", EnumUtility.ToApiString(knowledgebaseArticleRequest.ArticleStatus.Value));
            }

            parameters.AppendRequestDataBool("isfeatured", knowledgebaseArticleRequest.IsFeatured);
            parameters.AppendRequestDataBool("allowcomments", knowledgebaseArticleRequest.AllowComments);
            parameters.AppendRequestDataArrayCommaSeparated("categoryid", knowledgebaseArticleRequest.CategoryIds);

            if (requestType == RequestTypes.Update && knowledgebaseArticleRequest.EditedStaffId.HasValue)
            {
                parameters.AppendRequestDataNonNegativeInt("editedstaffid", knowledgebaseArticleRequest.EditedStaffId.Value);
            }

            return(parameters);
        }
Exemple #10
0
        private RequestBodyBuilder PopulateRequestParameters(KnowledgebaseCommentRequest knowledgebaseCommentRequest, RequestTypes requestType)
        {
            knowledgebaseCommentRequest.EnsureValidData(requestType);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            parameters.AppendRequestDataNonNegativeInt("knowledgebasearticleid", knowledgebaseCommentRequest.KnowledgebaseArticleId);
            parameters.AppendRequestDataNonEmptyString("contents", knowledgebaseCommentRequest.Contents);
            parameters.AppendRequestData("creatortype", EnumUtility.ToApiString(knowledgebaseCommentRequest.CreatorType));

            if (knowledgebaseCommentRequest.CreatorId.HasValue)
            {
                parameters.AppendRequestDataNonNegativeInt("creatorid", knowledgebaseCommentRequest.CreatorId.Value);
            }
            else
            {
                parameters.AppendRequestDataNonEmptyString("fullname", knowledgebaseCommentRequest.FullName);
            }

            parameters.AppendRequestDataNonEmptyString("email", knowledgebaseCommentRequest.Email);

            if (knowledgebaseCommentRequest.ParentCommentId.HasValue)
            {
                parameters.AppendRequestDataNonNegativeInt("parentcommentid", knowledgebaseCommentRequest.ParentCommentId.Value);
            }

            return(parameters);
        }
        /// <summary>
        /// Add a new post to an existing ticket.. Only <paramref name="userid"/> or <paramref name="staffid"/> should be set.
        /// <remarks>
        /// See - http://wiki.kayako.com/display/DEV/REST+-+TicketPost#REST-TicketPost-POST%2FTickets%2FTicketPost
        /// </remarks>
        /// </summary>
        /// <param name="ticketid">The unique numeric identifier of the ticket</param>
        /// <param name="subject">The ticket post subject.</param>
        /// <param name="contents">The ticket post contents</param>
        /// <param name="userid">The User Id, if the ticket post is to be created as a user</param>
        /// <param name="staffid">The Staff Id, if the ticket post is to be created as a staff.</param>
        /// <returns></returns>
        public TicketPost AddTicketPost(TicketPostRequest request)
        {
            string apiMethod = "/Tickets/TicketPost";

            request.EnsureValidData(RequestTypes.Create);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            parameters.AppendRequestData("ticketid", request.TicketId);
            parameters.AppendRequestData("subject", request.Subject);
            parameters.AppendRequestData("contents", request.Contents);

            if (request.UserId == null && request.StaffId == null)
            {
                throw new ArgumentException("Neither UserId nor StaffId are set, one of these fields are required field and cannot be null.");
            }

            if (request.UserId != null && request.StaffId != null)
            {
                throw new ArgumentException("UserId are StaffId are both set, only one of these fields must be set.");
            }

            if (request.UserId != null)
            {
                parameters.AppendRequestData("userid", request.UserId.Value);
            }

            if (request.StaffId != null)
            {
                parameters.AppendRequestData("staffid", request.StaffId.Value);

                if (request.IsPrivate != null)
                {
                    parameters.AppendRequestDataBool("isprivate", request.IsPrivate.Value);
                }
            }

            TicketPostCollection posts = Connector.ExecutePost <TicketPostCollection>(apiMethod, parameters.ToString());

            if (posts.Count > 0)
            {
                return(posts[0]);
            }

            return(null);
        }
Exemple #12
0
        private static RequestBodyBuilder PopulateRequestParameters(UserGroupRequest userGroup, RequestTypes requestType)
        {
            userGroup.EnsureValidData(requestType);

            var parameters = new RequestBodyBuilder();

            if (!string.IsNullOrEmpty(userGroup.Title))
            {
                parameters.AppendRequestData("title", userGroup.Title);
            }

            parameters.AppendRequestData("grouptype", EnumUtility.ToApiString(userGroup.GroupType));

            //parameters.AppendRequestData("ismaster", Convert.ToInt32(userGroup.Ismaster));

            return(parameters);
        }
Exemple #13
0
        private RequestBodyBuilder PopulateRequestParameters(KnowledgebaseCategoryRequest knowledgebaseCategoryRequest, RequestTypes requestType)
        {
            knowledgebaseCategoryRequest.EnsureValidData(requestType);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            parameters.AppendRequestDataNonEmptyString("title", knowledgebaseCategoryRequest.Title);

            if (knowledgebaseCategoryRequest.CategoryType.HasValue)
            {
                parameters.AppendRequestData("categorytype",
                                             EnumUtility.ToApiString(knowledgebaseCategoryRequest.CategoryType.Value));
            }

            if (knowledgebaseCategoryRequest.ParentCategoryId.HasValue)
            {
                parameters.AppendRequestData("parentcategoryid", knowledgebaseCategoryRequest.ParentCategoryId.Value);
            }

            if (knowledgebaseCategoryRequest.DisplayOrder.HasValue)
            {
                parameters.AppendRequestDataNonNegativeInt("displayorder", knowledgebaseCategoryRequest.DisplayOrder.Value);
            }

            if (knowledgebaseCategoryRequest.ArticleSortOrder.HasValue)
            {
                parameters.AppendRequestData("articlesortorder",
                                             EnumUtility.ToApiString(knowledgebaseCategoryRequest.ArticleSortOrder.Value));
            }

            parameters.AppendRequestDataBool("allowcomments", knowledgebaseCategoryRequest.AllowComments);
            parameters.AppendRequestDataBool("allowrating", knowledgebaseCategoryRequest.AllowRating);
            parameters.AppendRequestDataBool("ispublished", knowledgebaseCategoryRequest.IsPublished);
            parameters.AppendRequestDataBool("uservisibilitycustom", knowledgebaseCategoryRequest.UserVisibilityCustom);
            parameters.AppendRequestDataArrayCommaSeparated("usergroupidlist", knowledgebaseCategoryRequest.UserGroupIdList);
            parameters.AppendRequestDataBool("staffvisibilitycustom", knowledgebaseCategoryRequest.StaffVisibilityCustom);
            parameters.AppendRequestDataArrayCommaSeparated("staffgroupidlist", knowledgebaseCategoryRequest.StaffGroupIdList);

            if (requestType == RequestTypes.Create && knowledgebaseCategoryRequest.StaffId.HasValue)
            {
                parameters.AppendRequestDataNonNegativeInt("staffid", knowledgebaseCategoryRequest.StaffId.Value);
            }

            return(parameters);
        }
        public void CreateDepartment()
        {
            var departmentRequest = new DepartmentRequest { Title = "Title", DisplayOrder = 2, Type = DepartmentType.Public };
            var departments = new DepartmentCollection { new Department { Title = "Title", DisplayOrder = 2, Type = DepartmentType.Public} };

            var parameters = new RequestBodyBuilder();
            parameters.AppendRequestData("title", departmentRequest.Title);
            parameters.AppendRequestData("type", EnumUtility.ToApiString(departmentRequest.Type));
            parameters.AppendRequestData("module", EnumUtility.ToApiString(departmentRequest.Module));
            parameters.AppendRequestData("displayorder", departmentRequest.DisplayOrder);
            parameters.AppendRequestData("uservisibilitycustom", 0);

            _kayakoApiRequest.Setup(x => x.ExecutePost<DepartmentCollection>(ApiBaseMethods.Departments, parameters.ToString())).Returns(departments);

            var departmentCreated = _departmentController.CreateDepartment(departmentRequest);

            _kayakoApiRequest.Verify(x => x.ExecutePost<DepartmentCollection>(ApiBaseMethods.Departments, parameters.ToString()), Times.Once());
            Assert.That(departmentCreated, Is.EqualTo(departments.First()));
        }
        private static RequestBodyBuilder PopulateRequestParameters(NewsItemRequest newsItem, RequestTypes requestType)
        {
            newsItem.EnsureValidData(requestType);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            parameters.AppendRequestDataNonEmptyString("subject", newsItem.Subject);
            parameters.AppendRequestDataNonEmptyString("contents", newsItem.Contents);

            if (requestType == RequestTypes.Create)
            {
                parameters.AppendRequestDataNonNegativeInt("staffid", newsItem.StaffId);
            }
            else
            {
                parameters.AppendRequestDataNonNegativeInt("editedstaffid", newsItem.StaffId);
            }

            if (requestType == RequestTypes.Create && newsItem.NewsItemType.HasValue)
            {
                parameters.AppendRequestData("newstype", EnumUtility.ToApiString(newsItem.NewsItemType));
            }

            if (newsItem.NewsItemStatus.HasValue)
            {
                parameters.AppendRequestData("newsstatus", EnumUtility.ToApiString(newsItem.NewsItemStatus));
            }

            parameters.AppendRequestDataNonEmptyString("fromname", newsItem.FromName);
            parameters.AppendRequestDataNonEmptyString("email", newsItem.Email);
            parameters.AppendRequestDataNonEmptyString("customemailsubject", newsItem.CustomEmailSubject);
            parameters.AppendRequestDataBool("sendemail", newsItem.SendEmail);
            parameters.AppendRequestDataBool("allowcomments", newsItem.AllowComments);
            parameters.AppendRequestDataBool("uservisibilitycustom", newsItem.UserVisibilityCustom);
            parameters.AppendRequestDataArrayCommaSeparated("usergroupidlist", newsItem.UserGroupIdList);
            parameters.AppendRequestDataBool("staffvisibilitycustom", newsItem.StaffVisibilityCustom);
            parameters.AppendRequestDataArrayCommaSeparated("staffgroupidlist", newsItem.StaffGroupIdList);
            parameters.AppendRequestData("expiry", newsItem.Expiry.DateTime.ToString("M/d/yyyy"));
            parameters.AppendRequestDataArrayCommaSeparated("newscategoryidlist", newsItem.Categories);

            return(parameters);
        }
Exemple #16
0
        /// <summary>
        ///     Run a search on Users. You can search users from email, full name, phone, organization name and user group.
        /// </summary>
        public UserCollection UserSearch(string query)
        {
            const string apiMethod = "/Base/UserSearch";

            var parameters = new RequestBodyBuilder();

            parameters.AppendRequestData("query", query);

            var users = this.Connector.ExecutePost <UserCollection>(apiMethod, parameters.ToString());

            return(users);
        }
        /// <summary>
        /// Add an attachment to a ticket post.
        /// </summary>
        /// <param name="ticketId">The unique numeric identifier of the ticket</param>
        /// <param name="ticketPostId">The unique numeric identifier of the ticket post</param>
        /// <param name="fileName">The file name for the attachment </param>
        /// <param name="contents">The BASE64 encoded attachment contents</param>
        public TicketAttachment AddTicketAttachment(TicketAttachmentRequest request)
        {
            string apiMethod = "/Tickets/TicketAttachment";

            request.EnsureValidData(RequestTypes.Create);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            parameters.AppendRequestData("ticketid", request.TicketId);
            parameters.AppendRequestData("ticketpostid", request.TicketPostId);
            parameters.AppendRequestData("filename", request.FileName);
            parameters.AppendRequestData("contents", request.Contents);

            TicketAttachmentCollection attachments = Connector.ExecutePost <TicketAttachmentCollection>(apiMethod, parameters.ToString());

            if (attachments != null && attachments.Count > 0)
            {
                return(attachments[0]);
            }

            return(null);
        }
        /// <summary>
        /// Create a new User record
        /// </summary>
        public User CreateUser(UserRequest user, string password, bool sendWelcomeEmail)
        {
            string apiMethod = "/Base/User/";

            RequestBodyBuilder parameters = PopulateRequestParameters(user, RequestTypes.Create);

            if (!String.IsNullOrEmpty(password))
            {
                parameters.AppendRequestData("password", password);
            }

            parameters.AppendRequestData("sendwelcomeemail", Convert.ToInt32(sendWelcomeEmail));

            UserCollection users = Connector.ExecutePost <UserCollection>(apiMethod, parameters.ToString());

            if (users != null && users.Count > 0)
            {
                return(users[0]);
            }

            return(null);
        }
        /// <summary>
        /// Populates the post parameters to send to Kayako Api service
        /// </summary>
        /// <returns></returns>
        internal RequestBodyBuilder GetRequestBodyParameters()
        {
            RequestBodyBuilder parameters = new RequestBodyBuilder();
            parameters.AppendRequestData("query", Query);

            FieldInfo[] props = typeof(TicketSearchField).GetFields(BindingFlags.Public | BindingFlags.Static);

            foreach (FieldInfo p in props)
            {
                if (_searchFields.Contains((TicketSearchField)p.GetValue(typeof(TicketSearchField))))
                {
                    RequestParameterNameAttribute[] att = (RequestParameterNameAttribute[])p.GetCustomAttributes(typeof(RequestParameterNameAttribute), false);

                    if(att != null)
                    {
                        parameters.AppendRequestData(att[0].RequestName, 1);   
                    }
                }
            }


            return parameters;
        }
        /// <summary>
        /// Populates the post parameters to send to Kayako Api service
        /// </summary>
        /// <returns></returns>
        internal RequestBodyBuilder GetRequestBodyParameters()
        {
            RequestBodyBuilder parameters = new RequestBodyBuilder();

            parameters.AppendRequestData("query", Query);

            FieldInfo[] props = typeof(TicketSearchField).GetFields(BindingFlags.Public | BindingFlags.Static);

            foreach (FieldInfo p in props)
            {
                if (_searchFields.Contains((TicketSearchField)p.GetValue(typeof(TicketSearchField))))
                {
                    RequestParameterNameAttribute[] att = (RequestParameterNameAttribute[])p.GetCustomAttributes(typeof(RequestParameterNameAttribute), false);

                    if (att != null)
                    {
                        parameters.AppendRequestData(att[0].RequestName, 1);
                    }
                }
            }


            return(parameters);
        }
        private RequestBodyBuilder PopulateRequestParameters(TroubleshooterCommentRequest troubleshooterCommentRequest, RequestTypes requestType)
        {
            troubleshooterCommentRequest.EnsureValidData(requestType);

            var parameters = new RequestBodyBuilder();

            parameters.AppendRequestDataNonNegativeInt("troubleshooterstepid", troubleshooterCommentRequest.TroubleshooterStepId);
            parameters.AppendRequestDataNonEmptyString("contents", troubleshooterCommentRequest.Contents);
            parameters.AppendRequestData("creatortype", EnumUtility.ToApiString(troubleshooterCommentRequest.CreatorType));
            parameters.AppendRequestDataNonNegativeInt("creatorid", troubleshooterCommentRequest.CreatorId);
            parameters.AppendRequestDataNonEmptyString("fullname", troubleshooterCommentRequest.FullName);
            parameters.AppendRequestDataNonEmptyString("email", troubleshooterCommentRequest.Email);
            parameters.AppendRequestDataNonNegativeInt("parentcommentid", troubleshooterCommentRequest.ParentCommentId);

            return(parameters);
        }
        private static RequestBodyBuilder PopulateRequestParameters(DepartmentRequest dept, RequestTypes requestType)
        {
            dept.EnsureValidData(requestType);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            if (!String.IsNullOrEmpty(dept.Title))
            {
                parameters.AppendRequestData("title", dept.Title);
            }

            parameters.AppendRequestData("type", EnumUtility.ToApiString(dept.Type));

            if (requestType == RequestTypes.Create)
            {
                parameters.AppendRequestData("module", EnumUtility.ToApiString(dept.Module));
            }

            if (dept.DisplayOrder > 0)
            {
                parameters.AppendRequestData("displayorder", dept.DisplayOrder);
            }

            if (dept.ParentDepartmentId > 0)
            {
                parameters.AppendRequestData("parentdepartmentid", dept.ParentDepartmentId);
            }

            if (dept.UserVisibilityCustom)
            {
                parameters.AppendRequestData("uservisibilitycustom", 1);
            }
            else
            {
                parameters.AppendRequestData("uservisibilitycustom", 0);
            }

            if (dept.UserGroups != null && dept.UserGroups.Count > 0)
            {
                parameters.AppendRequestDataArray <int>("usergroupid[]", dept.UserGroups);
            }

            return(parameters);
        }
        ///// <summary>
        ///// Add a new note to a ticket.
        ///// </summary>
        ///// <remarks>
        ///// http://wiki.kayako.com/display/DEV/REST+-+TicketNote#REST-TicketNote-POST%2FTickets%2FTicketNote</remarks>
        ///// <returns>The added ticket note.</returns>
        public TicketNote AddTicketNote(TicketNoteRequest request)
        {
            string apiMethod = "/Tickets/TicketNote";

            request.EnsureValidData(RequestTypes.Create);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            parameters.AppendRequestData("ticketid", request.TicketId);
            parameters.AppendRequestData("contents", request.Content);
            parameters.AppendRequestData("notecolor", (int)request.NoteColor);

            if (request.FullName == null && request.StaffId == null)
            {
                throw new ArgumentException("Neither FullName nor StaffId are set, one of these fields are required field and cannot be null.");
            }

            if (request.FullName != null && request.StaffId != null)
            {
                throw new ArgumentException("FullName are StaffId are both set, only one of these fields must be set.");
            }

            if (request.FullName != null)
            {
                parameters.AppendRequestData("fullname", request.FullName);
            }

            if (request.StaffId != null)
            {
                parameters.AppendRequestData("staffid", request.StaffId.Value);
            }

            if (request.ForStaffId != null)
            {
                parameters.AppendRequestData("forstaffid", request.ForStaffId.Value);
            }

            TicketNoteCollection notes = Connector.ExecutePost <TicketNoteCollection>(apiMethod, parameters.ToString());

            if (notes.Count > 0)
            {
                return(notes[0]);
            }

            return(null);
        }
		private static RequestBodyBuilder PopulateRequestParameters(NewsCategoryRequest newsCategory, RequestTypes requestType)
		{
			newsCategory.EnsureValidData(requestType);

			RequestBodyBuilder parameters = new RequestBodyBuilder();

			if (!String.IsNullOrEmpty(newsCategory.Title))
			{
				parameters.AppendRequestData("title", newsCategory.Title);
			}

			parameters.AppendRequestData("visibilitytype", EnumUtility.ToApiString(newsCategory.VisibilityType));

			return parameters;
		}
		private static RequestBodyBuilder PopulateRequestParameters(NewsItemRequest newsItem, RequestTypes requestType)
		{
			newsItem.EnsureValidData(requestType);

			RequestBodyBuilder parameters = new RequestBodyBuilder();
			parameters.AppendRequestDataNonEmptyString("subject", newsItem.Subject);
			parameters.AppendRequestDataNonEmptyString("contents", newsItem.Contents);

			if (requestType == RequestTypes.Create)
			{
				parameters.AppendRequestDataNonNegativeInt("staffid", newsItem.StaffId);
			}
			else
			{
				parameters.AppendRequestDataNonNegativeInt("editedstaffid", newsItem.StaffId);
			}

			if (requestType == RequestTypes.Create && newsItem.NewsItemType.HasValue)
			{
				parameters.AppendRequestData("newstype", EnumUtility.ToApiString(newsItem.NewsItemType));
			}

			if (newsItem.NewsItemStatus.HasValue)
			{
				parameters.AppendRequestData("newsstatus", EnumUtility.ToApiString(newsItem.NewsItemStatus));
			}

			parameters.AppendRequestDataNonEmptyString("fromname", newsItem.FromName);
			parameters.AppendRequestDataNonEmptyString("email", newsItem.Email);
			parameters.AppendRequestDataNonEmptyString("customemailsubject", newsItem.CustomEmailSubject);
			parameters.AppendRequestDataBool("sendemail", newsItem.SendEmail);
			parameters.AppendRequestDataBool("allowcomments", newsItem.AllowComments);
			parameters.AppendRequestDataBool("uservisibilitycustom", newsItem.UserVisibilityCustom);
			parameters.AppendRequestDataArrayCommaSeparated("usergroupidlist", newsItem.UserGroupIdList);
			parameters.AppendRequestDataBool("staffvisibilitycustom", newsItem.StaffVisibilityCustom);
			parameters.AppendRequestDataArrayCommaSeparated("staffgroupidlist", newsItem.StaffGroupIdList);
			parameters.AppendRequestData("expiry", newsItem.Expiry.DateTime.ToString("M/d/yyyy"));
			parameters.AppendRequestDataArrayCommaSeparated("newscategoryidlist", newsItem.Categories);
			
			return parameters;
		}
        ///// <summary>
        ///// Add a new note to a ticket.
        ///// </summary>
        ///// <remarks>
        ///// http://wiki.kayako.com/display/DEV/REST+-+TicketNote#REST-TicketNote-POST%2FTickets%2FTicketNote</remarks>
        ///// <returns>The added ticket note.</returns>
        public TicketNote AddTicketNote(TicketNoteRequest request)
        {
            string apiMethod = "/Tickets/TicketNote";

            request.EnsureValidData(RequestTypes.Create);

			RequestBodyBuilder parameters = new RequestBodyBuilder();

            parameters.AppendRequestData("ticketid", request.TicketId);
            parameters.AppendRequestData("contents", request.Content);
            parameters.AppendRequestData("notecolor", (int)request.NoteColor);

            if (request.FullName == null && request.StaffId == null)
			{
				throw new ArgumentException("Neither FullName nor StaffId are set, one of these fields are required field and cannot be null.");
			}

            if (request.FullName != null && request.StaffId != null)
			{
				throw new ArgumentException("FullName are StaffId are both set, only one of these fields must be set.");
			}

            if (request.FullName != null)
			{
                parameters.AppendRequestData("fullname", request.FullName);
			}

            if (request.StaffId != null)
			{
                parameters.AppendRequestData("staffid", request.StaffId.Value);
			}

            if (request.ForStaffId != null)
			{
                parameters.AppendRequestData("forstaffid", request.ForStaffId.Value);
			}

			TicketNoteCollection notes = Connector.ExecutePost<TicketNoteCollection>(apiMethod, parameters.ToString());

			if (notes.Count > 0)
			{
				return notes[0];
			}

			return null;
		}
		private RequestBodyBuilder PopulateRequestParameters(TroubleshooterCommentRequest troubleshooterCommentRequest, RequestTypes requestType)
		{
			troubleshooterCommentRequest.EnsureValidData(requestType);

			RequestBodyBuilder parameters = new RequestBodyBuilder();
			parameters.AppendRequestDataNonNegativeInt("troubleshooterstepid", troubleshooterCommentRequest.TroubleshooterStepId);
			parameters.AppendRequestDataNonEmptyString("contents", troubleshooterCommentRequest.Contents);
			parameters.AppendRequestData("creatortype", EnumUtility.ToApiString(troubleshooterCommentRequest.CreatorType));
			parameters.AppendRequestDataNonNegativeInt("creatorid", troubleshooterCommentRequest.CreatorId);
			parameters.AppendRequestDataNonEmptyString("fullname", troubleshooterCommentRequest.FullName);
			parameters.AppendRequestDataNonEmptyString("email", troubleshooterCommentRequest.Email);
			parameters.AppendRequestDataNonNegativeInt("parentcommentid", troubleshooterCommentRequest.ParentCommentId);

			return parameters;
		}
        public Ticket UpdateTicket(TicketRequest request)
        {
            request.EnsureValidData(RequestTypes.Update);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            if (!String.IsNullOrEmpty(request.Subject))
            {
                parameters.AppendRequestData("subject", request.Subject);
            }

            if (!String.IsNullOrEmpty(request.FullName))
            {
                parameters.AppendRequestData("fullname", request.FullName);
            }

            if (!String.IsNullOrEmpty(request.Email))
            {
                parameters.AppendRequestData("email", request.Email);
            }

            if (request.DepartmentId != null)
            {
                parameters.AppendRequestData("departmentid", request.DepartmentId);
            }

            if (request.TicketStatusId != null)
            {
                parameters.AppendRequestData("ticketstatusid", request.TicketStatusId);
            }

            if (request.TicketPriorityId != null)
            {
                parameters.AppendRequestData("ticketpriorityid", request.TicketPriorityId);
            }

            if (request.TicketTypeId != null)
            {
                parameters.AppendRequestData("tickettypeid", request.TicketTypeId);
            }

            if (request.OwnerStaffId != null)
            {
                parameters.AppendRequestData("ownerstaffid", request.OwnerStaffId);
            }

            if (request.UserId != null)
            {
                parameters.AppendRequestData("userid", request.UserId);
            }

            if (request.TemplateGroupId != null)
            {
                parameters.AppendRequestData("templategroup", request.TemplateGroupId);
            }
            else if (!string.IsNullOrEmpty(request.TemplateGroupName))
            {
                parameters.AppendRequestData("templategroup", request.TemplateGroupName);
            }

            string apiMethod = String.Format("/Tickets/Ticket/{0}", request.Id);

            TicketCollection tickets = Connector.ExecutePut <TicketCollection>(apiMethod, parameters.ToString());

            if (tickets != null && tickets.Count > 0)
            {
                return(tickets[0]);
            }

            return(null);
        }
Exemple #29
0
        private static RequestBodyBuilder PopulateRequestParameters(UserOrganizationRequest org, RequestTypes requestType)
        {
            org.EnsureValidData(requestType);

            var parameters = new RequestBodyBuilder();

            parameters.AppendRequestData("name", org.Name);
            parameters.AppendRequestData("organizationtype", EnumUtility.ToApiString(org.OrganizationType));

            if (!string.IsNullOrEmpty(org.Address))
            {
                parameters.AppendRequestData("address", org.Address);
            }

            if (!string.IsNullOrEmpty(org.City))
            {
                parameters.AppendRequestData("city", org.City);
            }

            if (!string.IsNullOrEmpty(org.State))
            {
                parameters.AppendRequestData("state", org.State);
            }

            if (!string.IsNullOrEmpty(org.PostalCode))
            {
                parameters.AppendRequestData("postalcode", org.PostalCode);
            }

            if (!string.IsNullOrEmpty(org.Country))
            {
                parameters.AppendRequestData("country", org.Country);
            }

            if (!string.IsNullOrEmpty(org.Phone))
            {
                parameters.AppendRequestData("phone", org.Phone);
            }

            if (!string.IsNullOrEmpty(org.Fax))
            {
                parameters.AppendRequestData("fax", org.Fax);
            }

            if (!string.IsNullOrEmpty(org.Website))
            {
                parameters.AppendRequestData("website", org.Website);
            }

            if (org.SlaPlanId != null)
            {
                parameters.AppendRequestData("slaplanid", org.SlaPlanId);
            }

            if (org.SlaPlanExpiry != null)
            {
                parameters.AppendRequestData("slaplanexpiry", org.SlaPlanExpiry);
            }

            return(parameters);
        }
		/// <summary>
		/// Run a search on Users. You can search users from email, full name, phone, organization name and user group.
		/// </summary>
		public UserCollection UserSearch(string query)
		{
			string apiMethod = "/Base/UserSearch";

			RequestBodyBuilder parameters = new RequestBodyBuilder();
			parameters.AppendRequestData("query", query);

			UserCollection users = Connector.ExecutePost<UserCollection>(apiMethod, parameters.ToString());

			return users;
		}
        private static RequestBodyBuilder PopulateRequestParameters(StaffUserRequest staffUser, RequestTypes requestType)
        {
            staffUser.EnsureValidData(requestType);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            if (!String.IsNullOrEmpty(staffUser.FirstName))
            {
                parameters.AppendRequestData("firstname", staffUser.FirstName);
            }

            if (!String.IsNullOrEmpty(staffUser.LastName))
            {
                parameters.AppendRequestData("lastname", staffUser.LastName);
            }

            if (!String.IsNullOrEmpty(staffUser.UserName))
            {
                parameters.AppendRequestData("username", staffUser.UserName);
            }

            if (!String.IsNullOrEmpty(staffUser.Password))
            {
                parameters.AppendRequestData("password", staffUser.Password);
            }

            if (staffUser.GroupId > 0)
            {
                parameters.AppendRequestData("staffgroupid", staffUser.GroupId);
            }

            if (!String.IsNullOrEmpty(staffUser.Email))
            {
                parameters.AppendRequestData("email", staffUser.Email);
            }

            if (!String.IsNullOrEmpty(staffUser.Designation))
            {
                parameters.AppendRequestData("designation", staffUser.Designation);
            }

            if (!String.IsNullOrEmpty(staffUser.MobileNumber))
            {
                parameters.AppendRequestData("mobilenumber", staffUser.MobileNumber);
            }

            parameters.AppendRequestData("isenabled", Convert.ToInt32(staffUser.IsEnabled));

            if (!String.IsNullOrEmpty(staffUser.Greeting))
            {
                parameters.AppendRequestData("greeting", staffUser.Greeting);
            }

            if (!String.IsNullOrEmpty(staffUser.Signature))
            {
                parameters.AppendRequestData("staffsignature", staffUser.Signature);
            }

            if (!String.IsNullOrEmpty(staffUser.TimeZone))
            {
                parameters.AppendRequestData("timezone", staffUser.TimeZone);
            }

            parameters.AppendRequestData("enabledst", Convert.ToInt32(staffUser.EnableDst));

            return(parameters);
        }
        /// <summary>
		/// Add a new post to an existing ticket.. Only <paramref name="userid"/> or <paramref name="staffid"/> should be set.
		/// <remarks>
		/// See - http://wiki.kayako.com/display/DEV/REST+-+TicketPost#REST-TicketPost-POST%2FTickets%2FTicketPost
		/// </remarks>
		/// </summary>
		/// <param name="ticketid">The unique numeric identifier of the ticket</param>
		/// <param name="subject">The ticket post subject.</param>
		/// <param name="contents">The ticket post contents</param>
		/// <param name="userid">The User Id, if the ticket post is to be created as a user</param>
		/// <param name="staffid">The Staff Id, if the ticket post is to be created as a staff.</param>
		/// <returns></returns>
		public TicketPost AddTicketPost(TicketPostRequest request)
		{
            string apiMethod = "/Tickets/TicketPost";

            request.EnsureValidData(RequestTypes.Create);

			RequestBodyBuilder parameters = new RequestBodyBuilder();

            parameters.AppendRequestData("ticketid", request.TicketId);
            parameters.AppendRequestData("subject", request.Subject);
            parameters.AppendRequestData("contents", request.Contents);

            if (request.UserId == null && request.StaffId == null)
			{
				throw new ArgumentException("Neither UserId nor StaffId are set, one of these fields are required field and cannot be null.");
			}

            if (request.UserId != null && request.StaffId != null)
			{
				throw new ArgumentException("UserId are StaffId are both set, only one of these fields must be set.");
			}

            if (request.UserId != null)
			{
                parameters.AppendRequestData("userid", request.UserId.Value);
			}

            if (request.StaffId != null)
			{
                parameters.AppendRequestData("staffid", request.StaffId.Value);

				if (request.IsPrivate != null)
				{
					parameters.AppendRequestDataBool("isprivate", request.IsPrivate.Value);
				}
			}

			TicketPostCollection posts = Connector.ExecutePost<TicketPostCollection>(apiMethod, parameters.ToString());

			if (posts.Count > 0)
			{
				return posts[0];
			}

			return null;
		}
		/// <summary>
		/// Add an attachment to a ticket post.
		/// </summary>
		/// <param name="ticketId">The unique numeric identifier of the ticket</param>
		/// <param name="ticketPostId">The unique numeric identifier of the ticket post</param>
		/// <param name="fileName">The file name for the attachment </param>
		/// <param name="contents">The BASE64 encoded attachment contents</param>
		public TicketAttachment AddTicketAttachment(TicketAttachmentRequest request)
		{
            string apiMethod = "/Tickets/TicketAttachment";

            request.EnsureValidData(RequestTypes.Create);

			RequestBodyBuilder parameters = new RequestBodyBuilder();
			parameters.AppendRequestData("ticketid", request.TicketId);
            parameters.AppendRequestData("ticketpostid", request.TicketPostId);
            parameters.AppendRequestData("filename", request.FileName);
            parameters.AppendRequestData("contents", request.Contents);

			TicketAttachmentCollection attachments = Connector.ExecutePost<TicketAttachmentCollection>(apiMethod, parameters.ToString());

			if (attachments != null && attachments.Count > 0)
			{
				return attachments[0];
			}

			return null;
		}
Exemple #34
0
        private static RequestBodyBuilder PopulateRequestParameters(UserRequest user, RequestTypes requestType)
        {
            user.EnsureValidData(requestType);

            var parameters = new RequestBodyBuilder();

            if (!string.IsNullOrEmpty(user.FullName))
            {
                parameters.AppendRequestData("fullname", user.FullName);
            }

            if (user.GroupId > 0)
            {
                parameters.AppendRequestData("usergroupid", user.GroupId);
            }

            if (user.EmailAddresses != null && user.EmailAddresses.Length > 0)
            {
                parameters.AppendRequestDataArray("email[]", user.EmailAddresses);
            }

            if (user.OrganizationId != null && user.OrganizationId.HasValue && user.OrganizationId.Value > 0)
            {
                parameters.AppendRequestData("userorganizationid", user.OrganizationId.Value);
            }

            parameters.AppendRequestData("salutation", EnumUtility.ToApiString(user.Salutation));

            if (!string.IsNullOrEmpty(user.Designation))
            {
                parameters.AppendRequestData("designation", user.Designation);
            }

            if (!string.IsNullOrEmpty(user.Phone))
            {
                parameters.AppendRequestData("phone", user.Phone);
            }

            parameters.AppendRequestData("isenabled", Convert.ToInt32(user.IsEnabled));

            parameters.AppendRequestData("userrole", EnumUtility.ToApiString(user.Role));

            if (!string.IsNullOrEmpty(user.TimeZone))
            {
                parameters.AppendRequestData("timezone", user.TimeZone);
            }

            parameters.AppendRequestData("enabledst", Convert.ToInt32(user.EnableDst));

            if (user.SlaPlanId != null)
            {
                parameters.AppendRequestData("slaplanid", user.SlaPlanId);
            }

            if (user.SlaPlanExpiry != null)
            {
                parameters.AppendRequestData("slaplanexpiry", user.SlaPlanExpiry);
            }

            if (user.Expiry != null)
            {
                parameters.AppendRequestData("userexpiry", user.Expiry);
            }

            return(parameters);
        }
		private static RequestBodyBuilder PopulateRequestParameters(StaffGroupRequest staffGroup, RequestTypes requestType)
		{
            staffGroup.EnsureValidData(requestType);

			RequestBodyBuilder parameters = new RequestBodyBuilder();

			if (!String.IsNullOrEmpty(staffGroup.Title))
			{
				parameters.AppendRequestData("title", staffGroup.Title);
			}

			parameters.AppendRequestData("isadmin", Convert.ToInt32(staffGroup.IsAdmin));

			return parameters;
		}
		private static RequestBodyBuilder PopulateRequestParameters(UserGroupRequest userGroup, RequestTypes requestType)
		{
            userGroup.EnsureValidData(requestType);

			RequestBodyBuilder parameters = new RequestBodyBuilder();

			if (!String.IsNullOrEmpty(userGroup.Title))
			{
				parameters.AppendRequestData("title", userGroup.Title);
			}

			parameters.AppendRequestData("grouptype", EnumUtility.ToApiString(userGroup.GroupType));

			//parameters.AppendRequestData("ismaster", Convert.ToInt32(userGroup.Ismaster));

			return parameters;
		}
		private RequestBodyBuilder PopulateRequestParameters(TroubleshooterCategoryRequest troubleshooterCategoryRequest, RequestTypes requestType)
		{
			troubleshooterCategoryRequest.EnsureValidData(requestType);

			RequestBodyBuilder parameters = new RequestBodyBuilder();
			parameters.AppendRequestDataNonEmptyString("title", troubleshooterCategoryRequest.Title);
			parameters.AppendRequestData("categorytype", EnumUtility.ToApiString(troubleshooterCategoryRequest.CategoryType));

			if (requestType == RequestTypes.Create)
			{
				parameters.AppendRequestDataNonNegativeInt("staffid", troubleshooterCategoryRequest.StaffId);
			}

			if (troubleshooterCategoryRequest.DisplayOrder.HasValue)
			{
				parameters.AppendRequestDataNonNegativeInt("displayorder", troubleshooterCategoryRequest.DisplayOrder.Value);
			}

			parameters.AppendRequestDataNonEmptyString("description", troubleshooterCategoryRequest.Description);
			parameters.AppendRequestDataBool("uservisibilitycustom", troubleshooterCategoryRequest.UserVisibilityCustom);
			parameters.AppendRequestDataArrayCommaSeparated("usergroupidlist", troubleshooterCategoryRequest.UserGroupIdList);
			parameters.AppendRequestDataBool("staffvisibilitycustom", troubleshooterCategoryRequest.StaffVisibilityCustom);
			parameters.AppendRequestDataArrayCommaSeparated("staffgroupidlist", troubleshooterCategoryRequest.StaffGroupIdList);

			return parameters;
		}
        public Ticket CreateTicket(TicketRequest ticketRequest)
        {
            string apiMethod = "/Tickets/Ticket";

            ticketRequest.EnsureValidData(RequestTypes.Create);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            parameters.AppendRequestData("subject", ticketRequest.Subject);
            parameters.AppendRequestData("fullname", ticketRequest.FullName);
            parameters.AppendRequestData("email", ticketRequest.Email);
            parameters.AppendRequestData("contents", ticketRequest.Contents);
            parameters.AppendRequestData("departmentid", ticketRequest.DepartmentId);
            parameters.AppendRequestData("ticketstatusid", ticketRequest.TicketStatusId);
            parameters.AppendRequestData("ticketpriorityid", ticketRequest.TicketPriorityId);
            parameters.AppendRequestData("tickettypeid", ticketRequest.TicketTypeId);

            if (ticketRequest.AutoUserId != null)
            {
                parameters.AppendRequestData("autouserid", Convert.ToInt32(ticketRequest.AutoUserId));
            }
            else if (ticketRequest.UserId != null)
            {
                parameters.AppendRequestData("userid", ticketRequest.UserId);
            }
            else if (ticketRequest.StaffId != null)
            {
                parameters.AppendRequestData("staffid", ticketRequest.StaffId);
            }

            if (ticketRequest.OwnerStaffId != null)
            {
                parameters.AppendRequestData("ownerstaffid", ticketRequest.OwnerStaffId);
            }

            if (ticketRequest.TemplateGroupId != null)
            {
                parameters.AppendRequestData("templategroup", ticketRequest.TemplateGroupId);
            }
            else if (!string.IsNullOrEmpty(ticketRequest.TemplateGroupName))
            {
                parameters.AppendRequestData("templategroup", ticketRequest.TemplateGroupName);
            }

            if (ticketRequest.IgnoreAutoResponder != null)
            {
                parameters.AppendRequestData("ignoreautoresponder", Convert.ToInt32(ticketRequest.IgnoreAutoResponder));
            }

            if (ticketRequest.CreationType != null)
            {
                parameters.AppendRequestData("type", EnumUtility.ToApiString(ticketRequest.CreationType));
            }

            TicketCollection tickets = Connector.ExecutePost <TicketCollection>(apiMethod, parameters.ToString());

            if (tickets.Count > 0)
            {
                return(tickets[0]);
            }

            return(null);
        }
		private static RequestBodyBuilder PopulateRequestParameters(StaffUserRequest staffUser, RequestTypes requestType)
        {
			staffUser.EnsureValidData(requestType);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            if (!String.IsNullOrEmpty(staffUser.FirstName))
            {
                parameters.AppendRequestData("firstname", staffUser.FirstName);
            }

            if (!String.IsNullOrEmpty(staffUser.LastName))
            {
                parameters.AppendRequestData("lastname", staffUser.LastName);
            }

            if (!String.IsNullOrEmpty(staffUser.UserName))
            {
                parameters.AppendRequestData("username", staffUser.UserName);
            }

			if (!String.IsNullOrEmpty(staffUser.Password))
            {
				parameters.AppendRequestData("password", staffUser.Password);
            }

            if (staffUser.GroupId > 0)
            {
                parameters.AppendRequestData("staffgroupid", staffUser.GroupId);
            }

            if (!String.IsNullOrEmpty(staffUser.Email))
            {
                parameters.AppendRequestData("email", staffUser.Email);
            }

            if (!String.IsNullOrEmpty(staffUser.Designation))
            {
                parameters.AppendRequestData("designation", staffUser.Designation);
            }

            if (!String.IsNullOrEmpty(staffUser.MobileNumber))
            {
                parameters.AppendRequestData("mobilenumber", staffUser.MobileNumber);
            }

            parameters.AppendRequestData("isenabled", Convert.ToInt32(staffUser.IsEnabled));

            if (!String.IsNullOrEmpty(staffUser.Greeting))
            {
                parameters.AppendRequestData("greeting", staffUser.Greeting);
            }

            if (!String.IsNullOrEmpty(staffUser.Signature))
            {
				parameters.AppendRequestData("staffsignature", staffUser.Signature);
            }

            if (!String.IsNullOrEmpty(staffUser.TimeZone))
            {
                parameters.AppendRequestData("timezone", staffUser.TimeZone);
            }

            parameters.AppendRequestData("enabledst", Convert.ToInt32(staffUser.EnableDst));

            return parameters;
        }
		/// <summary>
		/// Add a new time tracking note to a ticket
		/// </summary>
		public TicketTimeTrack AddTicketTimeTrackingNote(TicketTimeTrackRequest request)
		{
            request.EnsureValidData(RequestTypes.Create);

			string apiMethod = "/Tickets/TicketTimeTrack";

			RequestBodyBuilder parameters = new RequestBodyBuilder();
            parameters.AppendRequestData("ticketid", request.TicketId);
            parameters.AppendRequestData("contents", request.Contents);
            parameters.AppendRequestData("staffid", request.StaffId);
            parameters.AppendRequestData("worktimeline", request.WorkTimeline);
            parameters.AppendRequestData("billtimeline", request.BillTimeline);
            parameters.AppendRequestData("timespent", request.TimeSpent);
            parameters.AppendRequestData("timebillable", request.TimeBillable);

            if (request.WorkerStaffId != null)
			{
                parameters.AppendRequestData("workerstaffid", request.WorkerStaffId);
			}

            if (request.NoteColor != null)
			{
                parameters.AppendRequestData("notecolor", EnumUtility.ToApiString(request.NoteColor));
			}

			TicketTimeTrackCollection ticketTimeTracks = Connector.ExecutePost<TicketTimeTrackCollection>(apiMethod, parameters.ToString());

			if (ticketTimeTracks != null && ticketTimeTracks.Count > 0)
			{
				return ticketTimeTracks[0];
			}

			return null;
		}
		private static RequestBodyBuilder PopulateRequestParameters(UserRequest user, RequestTypes requestType)
        {
			user.EnsureValidData(requestType);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            if (!String.IsNullOrEmpty(user.FullName))
            {
                parameters.AppendRequestData("fullname", user.FullName);
            }

            if (user.GroupId > 0)
            {
                parameters.AppendRequestData("usergroupid", user.GroupId);
            }

            if (user.EmailAddresses != null && user.EmailAddresses.Length > 0)
            {
                parameters.AppendRequestDataArray("email[]", user.EmailAddresses);
            }

			if (user.OrganizationId != null && user.OrganizationId.HasValue && user.OrganizationId.Value > 0)
            {
                parameters.AppendRequestData("userorganizationid", user.OrganizationId.Value);
            }

            parameters.AppendRequestData("salutation", EnumUtility.ToApiString(user.Salutation));

            if(!String.IsNullOrEmpty(user.Designation))
            {
                parameters.AppendRequestData("designation", user.Designation);
            }

            if(!String.IsNullOrEmpty(user.Phone))
            {
                parameters.AppendRequestData("phone", user.Phone);
            }

            parameters.AppendRequestData("isenabled", Convert.ToInt32(user.IsEnabled));

			parameters.AppendRequestData("userrole", EnumUtility.ToApiString(user.Role));

            if(!String.IsNullOrEmpty(user.TimeZone))
            {
                parameters.AppendRequestData("timezone", user.TimeZone);
            }
            
            parameters.AppendRequestData("enabledst", Convert.ToInt32(user.EnableDst));

			if (user.SlaPlanId != null)
			{
				parameters.AppendRequestData("slaplanid", user.SlaPlanId);
			}

			if (user.SlaPlanExpiry != null)
			{
				parameters.AppendRequestData("slaplanexpiry", user.SlaPlanExpiry);
			}

			if (user.Expiry != null)
			{
				parameters.AppendRequestData("userexpiry", user.Expiry);
			}

            return parameters;
		}
		public Ticket UpdateTicket(TicketRequest request)
        {
			request.EnsureValidData(RequestTypes.Update);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

			if (!String.IsNullOrEmpty(request.Subject))
            {
				parameters.AppendRequestData("subject", request.Subject);
            }

			if (!String.IsNullOrEmpty(request.FullName))
            {
				parameters.AppendRequestData("fullname", request.FullName);
            }

			if (!String.IsNullOrEmpty(request.Email))
            {
				parameters.AppendRequestData("email", request.Email);
            }

			if (request.DepartmentId != null)
            {
				parameters.AppendRequestData("departmentid", request.DepartmentId);
            }

			if (request.TicketStatusId != null)
            {
				parameters.AppendRequestData("ticketstatusid", request.TicketStatusId);
            }

			if (request.TicketPriorityId != null)
            {
				parameters.AppendRequestData("ticketpriorityid", request.TicketPriorityId);
            }

			if (request.TicketTypeId != null)
            {
				parameters.AppendRequestData("tickettypeid", request.TicketTypeId);
            }

			if (request.OwnerStaffId != null)
            {
				parameters.AppendRequestData("ownerstaffid", request.OwnerStaffId);
            }

			if (request.UserId != null)
            {
				parameters.AppendRequestData("userid", request.UserId);
            }

			if (request.TemplateGroupId != null)
			{
				parameters.AppendRequestData("templategroup", request.TemplateGroupId);
			}
			else if(!string.IsNullOrEmpty(request.TemplateGroupName))
			{
				parameters.AppendRequestData("templategroup", request.TemplateGroupName);
			}

			string apiMethod = String.Format("/Tickets/Ticket/{0}", request.Id);

            TicketCollection tickets = Connector.ExecutePut<TicketCollection>(apiMethod, parameters.ToString());

            if (tickets != null && tickets.Count > 0)
            {
                return tickets[0];
            }

            return null;
        }
		private static RequestBodyBuilder PopulateRequestParameters(UserOrganizationRequest org, RequestTypes requestType)
		{
			org.EnsureValidData(requestType);

			RequestBodyBuilder parameters = new RequestBodyBuilder();

			parameters.AppendRequestData("name", org.Name);
			parameters.AppendRequestData("organizationtype", EnumUtility.ToApiString(org.OrganizationType));

			if (!String.IsNullOrEmpty(org.Address))
			{
				parameters.AppendRequestData("address", org.Address);
			}

			if (!String.IsNullOrEmpty(org.City))
			{
			parameters.AppendRequestData("city", org.City);
			}

			if (!String.IsNullOrEmpty(org.State))
			{
			parameters.AppendRequestData("state", org.State);
			}

			if (!String.IsNullOrEmpty(org.PostalCode))
			{
			parameters.AppendRequestData("postalcode", org.PostalCode);
			}

			if (!String.IsNullOrEmpty(org.Country))
			{
			parameters.AppendRequestData("country", org.Country);
			}

			if (!String.IsNullOrEmpty(org.Phone))
			{
			parameters.AppendRequestData("phone", org.Phone);
			}

			if (!String.IsNullOrEmpty(org.Fax))
			{
			parameters.AppendRequestData("fax", org.Fax);
			}

			if (!String.IsNullOrEmpty(org.Website))
			{
			parameters.AppendRequestData("website", org.Website);
			}

			if (org.SlaPlanId != null)
			{
			parameters.AppendRequestData("slaplanid", org.SlaPlanId);
			}

			if (org.SlaPlanExpiry != null)
			{
			parameters.AppendRequestData("slaplanexpiry", org.SlaPlanExpiry);
			}

			return parameters;
		}
        public Ticket CreateTicket(TicketRequest ticketRequest)
        {
			string apiMethod = "/Tickets/Ticket";

            ticketRequest.EnsureValidData(RequestTypes.Create);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            parameters.AppendRequestData("subject", ticketRequest.Subject);
            parameters.AppendRequestData("fullname", ticketRequest.FullName);
            parameters.AppendRequestData("email", ticketRequest.Email);
            parameters.AppendRequestData("contents", ticketRequest.Contents);
            parameters.AppendRequestData("departmentid", ticketRequest.DepartmentId);
            parameters.AppendRequestData("ticketstatusid", ticketRequest.TicketStatusId);
            parameters.AppendRequestData("ticketpriorityid", ticketRequest.TicketPriorityId);
            parameters.AppendRequestData("tickettypeid", ticketRequest.TicketTypeId);

            if (ticketRequest.AutoUserId != null)
            {
                parameters.AppendRequestData("autouserid", Convert.ToInt32(ticketRequest.AutoUserId));
            }
            else if (ticketRequest.UserId != null)
            {
                parameters.AppendRequestData("userid", ticketRequest.UserId);
            }
            else if (ticketRequest.StaffId != null)
            {
                parameters.AppendRequestData("staffid", ticketRequest.StaffId);
            }

            if (ticketRequest.OwnerStaffId != null)
            {
                parameters.AppendRequestData("ownerstaffid", ticketRequest.OwnerStaffId);
            }

			if (ticketRequest.TemplateGroupId != null)
			{
				parameters.AppendRequestData("templategroup", ticketRequest.TemplateGroupId);
			}
			else if (!string.IsNullOrEmpty(ticketRequest.TemplateGroupName))
			{
				parameters.AppendRequestData("templategroup", ticketRequest.TemplateGroupName);
			}

			if (ticketRequest.IgnoreAutoResponder != null)
			{
				parameters.AppendRequestData("ignoreautoresponder", Convert.ToInt32(ticketRequest.IgnoreAutoResponder));
			}

			if (ticketRequest.CreationType != null)
			{
				parameters.AppendRequestData("type", EnumUtility.ToApiString(ticketRequest.CreationType));
			}

            TicketCollection tickets = Connector.ExecutePost<TicketCollection>(apiMethod, parameters.ToString());

            if (tickets.Count > 0)
            {
                return tickets[0];
            }

            return null;
        }
		private RequestBodyBuilder PopulateRequestParameters(TroubleshooterStepRequest troubleshooterStepRequest, RequestTypes requestType)
		{
			troubleshooterStepRequest.EnsureValidData(requestType);

			RequestBodyBuilder parameters = new RequestBodyBuilder();

			if (requestType == RequestTypes.Create)
			{
				parameters.AppendRequestData("categoryid", troubleshooterStepRequest.CategoryId);
			}

			parameters.AppendRequestDataNonEmptyString("subject", troubleshooterStepRequest.Subject);
			parameters.AppendRequestDataNonEmptyString("contents", troubleshooterStepRequest.Contents);

			parameters.AppendRequestDataNonNegativeInt(requestType == RequestTypes.Create ? "staffid" : "editedstaffid",
			                                           troubleshooterStepRequest.StaffId);

			if (troubleshooterStepRequest.DisplayOrder.HasValue)
			{
				parameters.AppendRequestDataNonNegativeInt("displayorder", troubleshooterStepRequest.DisplayOrder.Value);
			}

			parameters.AppendRequestDataBool("allowcomments", troubleshooterStepRequest.AllowComments);
			parameters.AppendRequestDataBool("enableticketredirection", troubleshooterStepRequest.EnableTicketRedirection);

			if (troubleshooterStepRequest.RedirectDepartmentId.HasValue)
			{
				parameters.AppendRequestDataNonNegativeInt("redirectdepartmentid", troubleshooterStepRequest.RedirectDepartmentId.Value);
			}

			if (troubleshooterStepRequest.TicketTypeId.HasValue)
			{
				parameters.AppendRequestDataNonNegativeInt("tickettypeid", troubleshooterStepRequest.TicketTypeId.Value);
			}

			if(troubleshooterStepRequest.TicketPriorityId.HasValue)
			{
				parameters.AppendRequestDataNonNegativeInt("ticketpriorityid", troubleshooterStepRequest.TicketPriorityId.Value);
			}

			parameters.AppendRequestDataNonEmptyString("ticketsubject", troubleshooterStepRequest.TicketSubject);
			
			if (troubleshooterStepRequest.StepStatus.HasValue)
			{
				parameters.AppendRequestData("stepstatus", EnumUtility.ToApiString(troubleshooterStepRequest.StepStatus.Value));
			}

			parameters.AppendRequestDataArrayCommaSeparated("parentstepidlist", troubleshooterStepRequest.ParentStepIdList);

			return parameters;
		}
		private RequestBodyBuilder PopulateRequestParameters(KnowledgebaseCategoryRequest knowledgebaseCategoryRequest, RequestTypes requestType)
		{
			knowledgebaseCategoryRequest.EnsureValidData(requestType);

			RequestBodyBuilder parameters = new RequestBodyBuilder();
			parameters.AppendRequestDataNonEmptyString("title", knowledgebaseCategoryRequest.Title);

			if (knowledgebaseCategoryRequest.CategoryType.HasValue)
			{
				parameters.AppendRequestData("categorytype",
				                             EnumUtility.ToApiString(knowledgebaseCategoryRequest.CategoryType.Value));
			}

			if (knowledgebaseCategoryRequest.ParentCategoryId.HasValue)
			{
				parameters.AppendRequestData("parentcategoryid", knowledgebaseCategoryRequest.ParentCategoryId.Value);
			}

			if (knowledgebaseCategoryRequest.DisplayOrder.HasValue)
			{
				parameters.AppendRequestDataNonNegativeInt("displayorder", knowledgebaseCategoryRequest.DisplayOrder.Value);
			}

			if (knowledgebaseCategoryRequest.ArticleSortOrder.HasValue)
			{
				parameters.AppendRequestData("articlesortorder",
				                             EnumUtility.ToApiString(knowledgebaseCategoryRequest.ArticleSortOrder.Value));
			}

			parameters.AppendRequestDataBool("allowcomments", knowledgebaseCategoryRequest.AllowComments);
			parameters.AppendRequestDataBool("allowrating", knowledgebaseCategoryRequest.AllowRating);
			parameters.AppendRequestDataBool("ispublished", knowledgebaseCategoryRequest.IsPublished);
			parameters.AppendRequestDataBool("uservisibilitycustom", knowledgebaseCategoryRequest.UserVisibilityCustom);
			parameters.AppendRequestDataArrayCommaSeparated("usergroupidlist", knowledgebaseCategoryRequest.UserGroupIdList);
			parameters.AppendRequestDataBool("staffvisibilitycustom", knowledgebaseCategoryRequest.StaffVisibilityCustom);
			parameters.AppendRequestDataArrayCommaSeparated("staffgroupidlist", knowledgebaseCategoryRequest.StaffGroupIdList);

			if (requestType == RequestTypes.Create && knowledgebaseCategoryRequest.StaffId.HasValue)
			{
				parameters.AppendRequestDataNonNegativeInt("staffid", knowledgebaseCategoryRequest.StaffId.Value);
			}

			return parameters;
		}
		private static RequestBodyBuilder PopulateRequestParameters(DepartmentRequest dept, RequestTypes requestType)
        {
			dept.EnsureValidData(requestType);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            if (!String.IsNullOrEmpty(dept.Title))
            {
                parameters.AppendRequestData("title", dept.Title);
            }

			parameters.AppendRequestData("type", EnumUtility.ToApiString(dept.Type));

			if (requestType == RequestTypes.Create)
			{
				parameters.AppendRequestData("module", EnumUtility.ToApiString(dept.Module));
			}

            if (dept.DisplayOrder > 0)
            {
                parameters.AppendRequestData("displayorder", dept.DisplayOrder);
            }

            if (dept.ParentDepartmentId > 0)
            {
                parameters.AppendRequestData("parentdepartmentid", dept.ParentDepartmentId);
            }

            if(dept.UserVisibilityCustom)
            {
                parameters.AppendRequestData("uservisibilitycustom", 1);
            }
            else
            {
                parameters.AppendRequestData("uservisibilitycustom", 0);
            }

            if (dept.UserGroups != null && dept.UserGroups.Count > 0)
            {
                parameters.AppendRequestDataArray<int>("usergroupid[]", dept.UserGroups);
            }

            return parameters;
        }
		private RequestBodyBuilder PopulateRequestParameters(KnowledgebaseArticleRequest knowledgebaseArticleRequest, RequestTypes requestType)
		{
			knowledgebaseArticleRequest.EnsureValidData(requestType);

			RequestBodyBuilder parameters = new RequestBodyBuilder();
			parameters.AppendRequestDataNonEmptyString("subject", knowledgebaseArticleRequest.Subject);
			parameters.AppendRequestDataNonEmptyString("contents", knowledgebaseArticleRequest.Contents);

			if (requestType == RequestTypes.Create && knowledgebaseArticleRequest.CreatorId.HasValue)
			{
				parameters.AppendRequestDataNonNegativeInt("creatorid", knowledgebaseArticleRequest.CreatorId.Value);
			}

			if (knowledgebaseArticleRequest.ArticleStatus.HasValue)
			{
				parameters.AppendRequestData("articlestatus", EnumUtility.ToApiString(knowledgebaseArticleRequest.ArticleStatus.Value));
			}

			parameters.AppendRequestDataBool("isfeatured", knowledgebaseArticleRequest.IsFeatured);
			parameters.AppendRequestDataBool("allowcomments", knowledgebaseArticleRequest.AllowComments);
			parameters.AppendRequestDataArrayCommaSeparated("categoryid", knowledgebaseArticleRequest.CategoryIds);

			if (requestType == RequestTypes.Update && knowledgebaseArticleRequest.EditedStaffId.HasValue)
			{
				parameters.AppendRequestDataNonNegativeInt("editedstaffid", knowledgebaseArticleRequest.EditedStaffId.Value);
			}

			return parameters;
		}
		private RequestBodyBuilder PopulateRequestParameters(KnowledgebaseCommentRequest knowledgebaseCommentRequest, RequestTypes requestType)
		{
			knowledgebaseCommentRequest.EnsureValidData(requestType);

			RequestBodyBuilder parameters = new RequestBodyBuilder();
			parameters.AppendRequestDataNonNegativeInt("knowledgebasearticleid", knowledgebaseCommentRequest.KnowledgebaseArticleId);
			parameters.AppendRequestDataNonEmptyString("contents", knowledgebaseCommentRequest.Contents);
			parameters.AppendRequestData("creatortype", EnumUtility.ToApiString(knowledgebaseCommentRequest.CreatorType));
			
			if (knowledgebaseCommentRequest.CreatorId.HasValue)
			{
				parameters.AppendRequestDataNonNegativeInt("creatorid", knowledgebaseCommentRequest.CreatorId.Value);
			}
			else
			{
				parameters.AppendRequestDataNonEmptyString("fullname", knowledgebaseCommentRequest.FullName);
			}

			parameters.AppendRequestDataNonEmptyString("email", knowledgebaseCommentRequest.Email);

			if (knowledgebaseCommentRequest.ParentCommentId.HasValue)
			{
				parameters.AppendRequestDataNonNegativeInt("parentcommentid", knowledgebaseCommentRequest.ParentCommentId.Value);
			}

			return parameters;
		}
		public NewsItemComment CreateNewsItemComment(NewsItemCommentRequest newsItemCommentRequest)
		{
			newsItemCommentRequest.EnsureValidData(RequestTypes.Create);

			RequestBodyBuilder parameters = new RequestBodyBuilder();
			parameters.AppendRequestData("newsitemid", newsItemCommentRequest.NewsItemId);
			parameters.AppendRequestDataNonEmptyString("contents", newsItemCommentRequest.Contents);
			parameters.AppendRequestData("creatortype", EnumUtility.ToApiString(newsItemCommentRequest.CreatorType));

			if (newsItemCommentRequest.CreatorId != null)
			{
				parameters.AppendRequestData("creatorid", newsItemCommentRequest.CreatorId);
			}
			else
			{
				parameters.AppendRequestDataNonEmptyString("fullname", newsItemCommentRequest.FullName);
			}

			parameters.AppendRequestDataNonEmptyString("email", newsItemCommentRequest.Email);
			parameters.AppendRequestData("parentcommentid", newsItemCommentRequest.ParentCommentId);

			var newsItemComments = Connector.ExecutePost<NewsItemCommentCollection>(NewsItemCommentBaseUrl, parameters.ToString());

			if (newsItemComments != null && newsItemComments.Count > 0)
			{
				return newsItemComments[0];
			}

			return null;
		}