public async Task<bool> UpdateEmployee(BambooHrEmployee bambooHrEmployee) { if (bambooHrEmployee.Id <= 0) { throw new Exception("ID is required."); } var url = $"/employees/{bambooHrEmployee.Id}"; var request = GetNewRestRequest(url, Method.POST, false); var xml = bambooHrEmployee.ToXml(); request.AddParameter("text/xml", xml, ParameterType.RequestBody); IRestResponse response; try { response = await _iRestClient.ExecuteTaskAsync(request); } catch (Exception ex) { throw new Exception($"Error executing Bamboo request to {url} to update employee '{bambooHrEmployee.FirstName} {bambooHrEmployee.LastName}'.", ex); } if (response.ErrorException != null) throw new Exception($"Error executing Bamboo request to {url} to update employee '{bambooHrEmployee.FirstName} {bambooHrEmployee.LastName}'.", response.ErrorException); if (response.StatusCode == HttpStatusCode.BadRequest) { throw new Exception($"Bad XML trying to update employee with ID {bambooHrEmployee.Id}."); } else if (response.StatusCode == HttpStatusCode.Forbidden) { throw new Exception($"Either you don't have permissions to update the employee, or none of the requested fields can be updated for employee ID {bambooHrEmployee.Id}."); } else if (response.StatusCode == HttpStatusCode.NotFound) { throw new Exception($"Employee not found with the ID {bambooHrEmployee.Id}."); } else if (response.StatusCode == HttpStatusCode.OK) { return true; } throw new Exception($"Bamboo Response threw error code {response.StatusCode} ({response.StatusDescription}) {response.GetBambooHrErrorMessage()} in {nameof(UpdateEmployee)}"); }
public async Task<string> AddEmployee(BambooHrEmployee bambooHrEmployee) { if (string.IsNullOrWhiteSpace(bambooHrEmployee.FirstName)) { throw new Exception("First name is required."); } if (string.IsNullOrWhiteSpace(bambooHrEmployee.LastName)) { throw new Exception("Lastname is required."); } var url = "/employees/"; var request = GetNewRestRequest(url, Method.POST, false); var xml = bambooHrEmployee.ToXml(); request.AddParameter("text/xml", xml, ParameterType.RequestBody); IRestResponse response; try { response = await _iRestClient.ExecuteTaskAsync(request); } catch (Exception ex) { throw new Exception($"Error executing Bamboo request to {url} to add employee '{bambooHrEmployee.FirstName} {bambooHrEmployee.LastName}'", ex); } if (response.ErrorException != null) throw new Exception($"Error executing Bamboo request to {url} to add employee '{bambooHrEmployee.FirstName} {bambooHrEmployee.LastName}'", response.ErrorException); if (response.StatusCode == HttpStatusCode.Conflict) { throw new Exception($"There is already an employee with the email address {bambooHrEmployee.WorkEmail}."); } if (response.StatusCode == HttpStatusCode.Created) { var location = response.Headers.Single(h => h.Name == "Location").Value.ToString(); var id = Regex.Match(location, @"\d+$").Value; bambooHrEmployee.Id = int.Parse(id); if (!string.IsNullOrWhiteSpace(location)) { return location; } throw new Exception("Bamboo Response does not contain Employee"); } throw new Exception($"Bamboo Response threw error code {response.StatusCode} ({response.StatusDescription}) {response.GetBambooHrErrorMessage()} in {nameof(AddEmployee)}"); }