public string GetAPIVersion(string consumerSecretToken, string dataAccessToken, string version) { // Make request using Dovico.CommonLibrary CRestApiHelper.MakeAPIRequest method string result = CRestApiHelper.MakeAPIRequest(CRestApiHelper.BuildURI("APIinfo", null, version), "GET", CRestApiHelper.MIME_TYPE_APPLICATION_JSON, String.Empty, consumerSecretToken, dataAccessToken); // Fetch DataAccessToken from CommonLibrary result JavaScriptSerializer objJavaScriptSerializer = new JavaScriptSerializer(); objJavaScriptSerializer.MaxJsonLength = int.MaxValue; dynamic objJsonResultRootObject = objJavaScriptSerializer.DeserializeObject(result); if (objJsonResultRootObject != null) { Dictionary <string, object> dictionary = objJsonResultRootObject[0]; bool isDovicoBasic = Convert.ToString(dictionary["IsDovicoBasic"]) == "T" ? true : false; if (isDovicoBasic || (!isDovicoBasic && Convert.ToInt32(dictionary["MaxVersion"]) == Convert.ToInt32(Constants.Version_5))) { version = Constants.Version_5; } else if (!isDovicoBasic && Convert.ToInt32(dictionary["MaxVersion"]) >= Convert.ToInt32(Constants.Version_7)) { version = Constants.Version_7; } } return(version); }
/// <summary> /// Method to authenticate user /// </summary> /// <param name="consumerSecretToken"></param> /// <param name="version"></param> /// <param name="company"></param> /// <param name="username"></param> /// <param name="password"></param> /// <returns>string</returns> public string Authenticate(string consumerSecretToken, string version, string company, string username, string password) { string dataAccessToken = string.Empty; // Create Post Data CStringBuilder postData = new CStringBuilder(); postData.Append("{"); postData.Append(string.Concat("\"CompanyName\":\"", company, "\",")); postData.Append(string.Concat("\"UserName\":\"", username, "\",")); postData.Append(string.Concat("\"Password\":\"", password, "\"")); postData.Append("}"); // Make request using Dovico.CommonLibrary CRestApiHelper.MakeAPIRequest method string result = CRestApiHelper.MakeAPIRequest(CRestApiHelper.BuildURI("Authenticate", null, version), "Post", CRestApiHelper.MIME_TYPE_APPLICATION_JSON, postData, consumerSecretToken, null); // Check for errors in return value UtilityService.CheckErrorsInDovicoAPI(result); // Fetch DataAccessToken from CommonLibrary result JavaScriptSerializer objJavaScriptSerializer = new JavaScriptSerializer(); objJavaScriptSerializer.MaxJsonLength = int.MaxValue; dynamic objJsonResultRootObject = objJavaScriptSerializer.DeserializeObject(result); Dictionary <string, object> dictionary = objJsonResultRootObject; dataAccessToken = dictionary["DataAccessToken"].ToString(); return(dataAccessToken); }
/// <summary> /// Delete a specific Time Entry /// </summary> /// <param name="consumerSecretToken"></param> /// <param name="dataAccessToken"></param> /// <param name="version"></param> /// <param name="timeEntryId"></param> public void DeleteTimeEntry(string consumerSecretToken, string dataAccessToken, string version, string timeEntryId) { // Create uri to hit and make request using Dovico.CommonLibrary CRestApiHelper.MakeAPIRequest method string uri = CRestApiHelper.BuildURI(string.Concat("TimeEntries/", timeEntryId), null, version); string result = CRestApiHelper.MakeAPIRequest(uri, "DELETE", CRestApiHelper.MIME_TYPE_APPLICATION_JSON, string.Empty, consumerSecretToken, dataAccessToken); // Check for errors in return value UtilityService.CheckErrorsInDovicoAPI(result); }
// Handles the work of making a request and pulling Employee(s) from the result of a request (if there was an error the return value will // be null) protected static List <CEmployee> ProcessRequest(APIRequestResult aRequestResult) { // Make sure the Prev/Next Page URI, if there was an error, etc are reset in the event the user is re-using an object that has already // been used for a different call (don't want a previous call's results giving the caller false information) aRequestResult.ResetResultData(); // Pass the request on to the REST API. If there was an error then exit now CRestApiHelper.MakeAPIRequest(aRequestResult); if (aRequestResult.HadRequestError) { return(null); } // Load in the XML returned from the API call XmlDocument xdDoc = new XmlDocument(); xdDoc.LoadXml(aRequestResult.RequestResult); // Will hold the list of employees that will be returned to the calling function List <CEmployee> lstEmployees = new List <CEmployee>(); // Grab the root element and get the Previous/Next Page URIs from it (when requesting a specific employee there will be no paging // information returned since a single record is all that is ever returned. If that's the case we want our Previous/Next Page URIs to // hold 'N/A' rather than "" which is why we pass in the URI_NOT_AVAILABLE constant) XmlElement xeDocElement = xdDoc.DocumentElement; aRequestResult.ResultPrevPageURI = CXMLHelper.GetChildNodeValue(xeDocElement, Constants.PREV_PAGE_URI, Constants.URI_NOT_AVAILABLE); aRequestResult.ResultNextPageURI = CXMLHelper.GetChildNodeValue(xeDocElement, Constants.NEXT_PAGE_URI, Constants.URI_NOT_AVAILABLE); XmlElement xeEmployee = null; // Grab the list of Employee nodes and loop through the elements... XmlNodeList xnlEmployees = xeDocElement.GetElementsByTagName("Employee"); int iCount = xnlEmployees.Count; for (int iIndex = 0; iIndex < iCount; iIndex++) { // Grab the current element xeEmployee = (XmlElement)xnlEmployees[iIndex]; // Add the current item to our list lstEmployees.Add(new CEmployee( CDovicoID.Parse(CXMLHelper.GetChildNodeValue(xeEmployee, "ID")), CXMLHelper.GetChildNodeValue(xeEmployee, "LastName"), CXMLHelper.GetChildNodeValue(xeEmployee, "FirstName") // NOTE: If this is an Employee/Me/ request, the rest of the fields may not be available )); } // End of the for(int iIndex = 0; iIndex < iCount; iIndex++) loop. // Return the list of Employees to the caller return(lstEmployees); }
// Returns the assignments for the URI requested (if there are multiple pages of data, pass in the NextPageURI. If you are trying to get // the child assignment items, pass in the GetAssignmentsURI value of the item you wish to drill down on) public static string GetAssignments(string sAssignmentsURI, ref APIRequestResult aRequestResult) { // Set the URI if one was specified if (sAssignmentsURI != "") { aRequestResult.SetRequestURI(sAssignmentsURI); } // Request the list of child assignments CRestApiHelper.MakeAPIRequest(aRequestResult); return(aRequestResult.HadRequestError ? aRequestResult.GetRequestErrorMessage() : aRequestResult.RequestResult); }
/// <summary> /// Get Time Entry details for specific time entry /// </summary> /// <param name="consumerSecretToken"></param> /// <param name="dataAccessToken"></param> /// <param name="version"></param> /// <param name="timeEntryId"></param> /// <returns>IList<TimeEntryBO></returns> public IList <TimeEntryBO> GetSingleTimeEntry(string consumerSecretToken, string dataAccessToken, string version, string timeEntryId) { // Create uri to hit and make request using Dovico.CommonLibrary CRestApiHelper.MakeAPIRequest method string uri = CRestApiHelper.BuildURI(string.Concat("TimeEntries/", timeEntryId), null, version); string result = CRestApiHelper.MakeAPIRequest(uri, "GET", CRestApiHelper.MIME_TYPE_APPLICATION_JSON, string.Empty, consumerSecretToken, dataAccessToken); // Check for errors in return value UtilityService.CheckErrorsInDovicoAPI(result); // Convert data to TimeEntryBO IList <TimeEntryBO> timeEntries = DeserializeObject(result).TimeEntries; return(timeEntries); }
public static string GetInfoMeOptions(ref APIRequestResult aRequestResult) { // Set the URI for the Employee/Me/ request. Process the request and if the returned list is not null (no errors) then return the first item in the list (there // should only ever be the one item) aRequestResult.SetRequestURI("Employees/Me/Options/", ""); aRequestResult.ContentType = "text/xml"; CRestApiHelper.MakeAPIRequest(aRequestResult); XmlDocument xdDoc = new XmlDocument(); xdDoc.LoadXml(aRequestResult.RequestResult); string ShowBillable = CXMLHelper.GetChildNodeValue(xdDoc.DocumentElement, "ShowBillable"); return(ShowBillable); }
///// <summary> ///// Get Custom Templates data for specific type ///// </summary> ///// <param name="consumerSecretToken"></param> ///// <param name="dataAccessToken"></param> ///// <param name="version"></param> ///// <param name="type"></param> ///// <returns>IList<CustomTemplateBO></returns> //public IList<CustomTemplateBO> GetCustomTemplates(string consumerSecretToken, string dataAccessToken, string version, string type) //{ // // Create uri to hit and make request using Dovico.CommonLibrary CRestApiHelper.MakeAPIRequest method // string queryString = string.Empty; // string uri = CRestApiHelper.BuildURI(string.Concat("CustomFieldTemplates/Type/", type, "/"), queryString, version); // string result = CRestApiHelper.MakeAPIRequest(uri, "GET", CRestApiHelper.MIME_TYPE_APPLICATION_JSON, // String.Empty, consumerSecretToken, dataAccessToken); // // Check for errors in return value // UtilityService.CheckErrorsInDovicoAPI(result); // // Convert data to CustomTemplateBO // IList<CustomTemplateBO> customTemplates = DeserializeObject(result).CustomTemplates; // return customTemplates; //} /// <summary> /// Get Custom Templates data for the task /// </summary> /// <param name="consumerSecretToken"></param> /// <param name="dataAccessToken"></param> /// <param name="version"></param> /// <param name="taskId"></param> /// <returns>IList<CustomTemplateBO></returns> public IList <CustomTemplateBO> GetCustomTemplates(string consumerSecretToken, string dataAccessToken, string version, string taskId) { // Create uri to hit and make request using Dovico.CommonLibrary CRestApiHelper.MakeAPIRequest method string queryString = string.Concat("linkid=", taskId); string uri = CRestApiHelper.BuildURI(string.Concat("CustomFieldTemplates/Type/M/"), queryString, version); string result = CRestApiHelper.MakeAPIRequest(uri, "GET", CRestApiHelper.MIME_TYPE_APPLICATION_JSON, String.Empty, consumerSecretToken, dataAccessToken); // Check for errors in return value UtilityService.CheckErrorsInDovicoAPI(result); // Convert data to CustomTemplateBO IList <CustomTemplateBO> customTemplates = DeserializeObject(result).CustomTemplates; return(customTemplates); }
/// <summary> /// Gets CustomTerminology /// </summary> /// <param name="consumerSecretToken"></param> /// <param name="dataAccessToken"></param> /// <param name="version"></param> /// <returns>CustomTerminologyBO</returns> public CustomTerminologyBO GetCustomTerminology(string consumerSecretToken, string dataAccessToken, string version) { // Create uri to hit and make request using Dovico.CommonLibrary CRestApiHelper.MakeAPIRequest method string uri = CRestApiHelper.BuildURI("CustomTerminology", null, version); string result = CRestApiHelper.MakeAPIRequest(uri, "GET", CRestApiHelper.MIME_TYPE_APPLICATION_JSON, string.Empty, consumerSecretToken, dataAccessToken); // Check for errors in return value UtilityService.CheckErrorsInDovicoAPI(result); JavaScriptSerializer objJavaScriptSerializer = new JavaScriptSerializer(); objJavaScriptSerializer.MaxJsonLength = int.MaxValue; // Convert data to CustomTerminologyBO CustomTerminologyBO objJsonResultRootObject = (CustomTerminologyBO)objJavaScriptSerializer.Deserialize(result, typeof(CustomTerminologyBO)); return(objJsonResultRootObject); }
/// <summary> /// Submit the time entries /// </summary> /// <param name="consumerSecretToken"></param> /// <param name="dataAccessToken"></param> /// <param name="version"></param> /// <param name="employeeId"></param> /// <param name="startDateOfWeek"></param> /// <param name="endDateOfWeek"></param> /// <returns>IList<TimeEntryBO></returns> public IList <TimeEntryBO> SubmitWeekTimeEntryForApproval(string consumerSecretToken, string dataAccessToken, string version, int employeeId, DateTime startDateOfWeek, DateTime endDateOfWeek) { // Create parameters and uri string dateRangeParameter = string.Concat(startDateOfWeek.ToString("yyyy-MM-dd"), " ", endDateOfWeek.ToString("yyyy-MM-dd")); string employeeParameter = string.Format("TimeEntries/Employee/{0}/Submit", Convert.ToString(employeeId)); string queryString = string.Concat("daterange=", dateRangeParameter); string uri = CRestApiHelper.BuildURI(employeeParameter, queryString, version); CStringBuilder postData = new CStringBuilder(); postData.Append("{"); postData.Append("}"); // Make request using Dovico.CommonLibrary CRestApiHelper.MakeAPIRequest method string result = CRestApiHelper.MakeAPIRequest(uri, "POST", CRestApiHelper.MIME_TYPE_APPLICATION_JSON, postData, consumerSecretToken, dataAccessToken); UtilityService.CheckErrorsInDovicoAPI(result); IList <TimeEntryBO> timeEntries = DeserializeObject(result).TimeEntries; return(timeEntries); }
/// <summary> /// Get Time Entries data for employee /// </summary> /// <param name="consumerSecretToken"></param> /// <param name="dataAccessToken"></param> /// <param name="version"></param> /// <param name="employeeId"></param> /// <param name="startDate"></param> /// <param name="endDate"></param> /// <returns>IList<TimeEntryBO></returns> public IList <TimeEntryBO> GetTimeEntries(string consumerSecretToken, string dataAccessToken, string version, int employeeId, DateTime startDate, DateTime?endDate = null) { // Create date variables string fromDate = startDate.ToString("yyyy-MM-dd"); string toDate = endDate == null ? fromDate : ((DateTime)endDate).ToString("yyyy-MM-dd"); // Create query parameters and query string string datetimeParamter = string.Concat(fromDate, " ", toDate); string employeeParameter = string.Concat("Employee/", employeeId.ToString(CultureInfo.InvariantCulture)); string queryString = string.IsNullOrEmpty(datetimeParamter) ? string.Empty : string.Concat("daterange=", datetimeParamter); // Create uri to hit and make request using Dovico.CommonLibrary CRestApiHelper.MakeAPIRequest method string uri = CRestApiHelper.BuildURI(string.Concat("TimeEntries/", employeeParameter, "/"), queryString, version); string result = CRestApiHelper.MakeAPIRequest(uri, "GET", CRestApiHelper.MIME_TYPE_APPLICATION_JSON, String.Empty, consumerSecretToken, dataAccessToken); // Check for errors in return value UtilityService.CheckErrorsInDovicoAPI(result); // Convert data to TimeEntryBO IList <TimeEntryBO> timeEntires = DeserializeObject(result).TimeEntries; return(timeEntires); }
/// <summary> /// For Updating time entry /// </summary> /// <param name="consumerSecretToken"></param> /// <param name="dataAccessToken"></param> /// <param name="version"></param> /// <param name="timeEntrySubmissionDetails"></param> /// <returns>IList<TimeEntryBO></returns> public IList <TimeEntryBO> UpdateTimeEntry(string consumerSecretToken, string dataAccessToken, string version, TimeEntrySubmissionBO timeEntrySubmissionDetails) { string fromDate = timeEntrySubmissionDetails.StartDate.ToString("yyyy-MM-dd"); timeEntrySubmissionDetails.Description = timeEntrySubmissionDetails.Description.Replace("\r", "\\r").Replace("\n", "\\n").Replace("\"", "'"); // Create Post Data CStringBuilder postData = new CStringBuilder(); postData.Append("{"); if (!string.IsNullOrEmpty(timeEntrySubmissionDetails.ClientId)) { postData.Append(string.Concat("\"ClientID\":\"", timeEntrySubmissionDetails.ClientId, "\",")); } postData.Append(string.Concat("\"ProjectID\":\"", timeEntrySubmissionDetails.ProjectId, "\",")); postData.Append(string.Concat("\"TaskID\":\"", timeEntrySubmissionDetails.TaskId, "\",")); postData.Append(string.Concat("\"EmployeeID\":\"", timeEntrySubmissionDetails.EmployeeId, "\",")); postData.Append(string.Concat("\"Date\":\"", fromDate, "\",")); // Convert start/stop time to 24 hours format if (!string.IsNullOrEmpty(timeEntrySubmissionDetails.StartTime)) { postData.Append(string.Concat("\"StartTime\":\"", Convert.ToDateTime(timeEntrySubmissionDetails.StartTime).ToString("HHmm"), "\",")); } if (!string.IsNullOrEmpty(timeEntrySubmissionDetails.StopTime)) { postData.Append(string.Concat("\"StopTime\":\"", Convert.ToDateTime(timeEntrySubmissionDetails.StopTime).ToString("HHmm"), "\",")); } postData.Append(string.Concat("\"Description\":\"", timeEntrySubmissionDetails.Description, "\",")); postData.Append(string.Concat("\"Billable\":\"", timeEntrySubmissionDetails.Billable ? "T" : "F", "\",")); postData.Append(string.Concat("\"TotalHours\":\"", timeEntrySubmissionDetails.Hours, "\",")); postData.Append("\"CustomFields\":"); postData.Append("["); StringBuilder innerPostData = new StringBuilder(); // For Custom Fields if (timeEntrySubmissionDetails.CustomFields != null && timeEntrySubmissionDetails.CustomFields.Count > 0) { foreach (CustomFieldSubmissionBO customField in timeEntrySubmissionDetails.CustomFields) { innerPostData.Append("{"); if (String.IsNullOrEmpty(customField.ID)) { innerPostData.Append(string.Concat("\"ID\":\"", "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF", "\",")); } else { innerPostData.Append(string.Concat("\"ID\":\"", customField.ID, "\",")); } innerPostData.Append(string.Concat("\"TemplateID\":\"", customField.TemplateID, "\",")); innerPostData.Append("\"Values\":"); innerPostData.Append("["); if (customField.Values != null) { foreach (ValueItemsSubmission item in customField.Values) { innerPostData.Append(string.Concat("\"", item.Value, "\",")); } innerPostData.Remove(innerPostData.Length - 1, 1); } innerPostData.Append("]"); innerPostData.Append("},"); } innerPostData.Remove(innerPostData.Length - 1, 1); } postData.Append(innerPostData.ToString()); postData.Append("]"); postData.Append("}"); // Create uri to hit and make request using Dovico.CommonLibrary CRestApiHelper.MakeAPIRequest method string uri = CRestApiHelper.BuildURI(string.Concat("TimeEntries/", timeEntrySubmissionDetails.TimeEntryId), null, version); string result = CRestApiHelper.MakeAPIRequest(uri, "PUT", CRestApiHelper.MIME_TYPE_APPLICATION_JSON, postData, consumerSecretToken, dataAccessToken); UtilityService.CheckErrorsInDovicoAPI(result); IList <TimeEntryBO> timeEntries = DeserializeObject(result).TimeEntries; return(timeEntries); }
// Helper that fires off the request to the REST API and processes the results protected static List <CTimeEntry> ProcessRequest(ref APIRequestResult aRequestResult) { // Make sure the Prev/Next Page URI, if there was an error, etc are reset in the event the user is re-using an object that has already // been used for a different call (don't want a previous call's results giving the caller false information) aRequestResult.ResetResultData(); // Pass the request on to the REST API. If there was an error then exit now CRestApiHelper.MakeAPIRequest(aRequestResult); if (aRequestResult.HadRequestError) { return(null); } // Load in the XML returned from the API call XmlDocument xdDoc = new XmlDocument(); xdDoc.LoadXml(aRequestResult.RequestResult); // Will hold the list of Time Entries that will be returned to the calling function List <CTimeEntry> lstTimeEntries = new List <CTimeEntry>(); // Grab the root element and get the Previous/Next Page URIs from it (when requesting a specific time entry there will be no paging // information returned since a single record is all that is ever returned. If that's the case we want our Previous/Next Page URIs to // hold 'N/A' rather than "" which is why we pass in the URI_NOT_AVAILABLE constant) XmlElement xeDocElement = xdDoc.DocumentElement; aRequestResult.ResultPrevPageURI = CXMLHelper.GetChildNodeValue(xeDocElement, Constants.PREV_PAGE_URI, Constants.URI_NOT_AVAILABLE); aRequestResult.ResultNextPageURI = CXMLHelper.GetChildNodeValue(xeDocElement, Constants.NEXT_PAGE_URI, Constants.URI_NOT_AVAILABLE); XmlElement xeTimeEntry = null, xeClient = null, xeProject = null, xeTask = null; DateTime dtDate = DateTime.Now; // Grab the list of Time Entry nodes and loop through the elements... XmlNodeList xnlTimeEntries = xeDocElement.GetElementsByTagName("TimeEntry"); int iCount = xnlTimeEntries.Count; for (int iIndex = 0; iIndex < iCount; iIndex++) { // Grab the current element and the required sub-elements xeTimeEntry = (XmlElement)xnlTimeEntries[iIndex]; xeClient = (XmlElement)xeTimeEntry.GetElementsByTagName("Client")[0]; xeProject = (XmlElement)xeTimeEntry.GetElementsByTagName("Project")[0]; xeTask = (XmlElement)xeTimeEntry.GetElementsByTagName("Task")[0]; // Parse the date CDateHelper.GetDateFromAPIDateString(CXMLHelper.GetChildNodeValue(xeTimeEntry, "Date"), out dtDate); // Add the current item to our list lstTimeEntries.Add(new CTimeEntry( CXMLHelper.GetChildNodeValue(xeTimeEntry, "ID"), CDovicoID.Parse(CXMLHelper.GetChildNodeValue(xeClient, "ID")), CXMLHelper.GetChildNodeValue(xeClient, "Name"), CDovicoID.Parse(CXMLHelper.GetChildNodeValue(xeProject, "ID")), CXMLHelper.GetChildNodeValue(xeProject, "Name"), CDovicoID.Parse(CXMLHelper.GetChildNodeValue(xeTask, "ID")), CXMLHelper.GetChildNodeValue(xeTask, "Name"), dtDate, CXMLHelper.GetChildNodeValue(xeTimeEntry, "StartTime"), CXMLHelper.GetChildNodeValue(xeTimeEntry, "StopTime"), double.Parse(CXMLHelper.GetChildNodeValue(xeTimeEntry, "TotalHours"), Constants.CULTURE_US_ENGLISH), CXMLHelper.GetChildNodeValue(xeTimeEntry, "Description") )); } // End of the for(int iIndex = 0; iIndex < iCount; iIndex++) loop. // Return the list of Employees to the caller return(lstTimeEntries); }