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)}");
        }
Exemplo n.º 2
0
        private async static Task<bool> UpdateEmployee(BambooHrEmployee bambooHrEmployee)
        {
            var bambooHrClient = new BambooHrClient();
            await bambooHrClient.UpdateEmployee(bambooHrEmployee);

            Console.WriteLine($"Updated employee with ID {bambooHrEmployee.Id}");

            return true;
        }
        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)}");
        }
Exemplo n.º 4
0
        private async static void AddEmployee(string firstName, string lastName, string workEmail)
        {
            var bambooHrClient = new BambooHrClient();

            var bambooHrEmployee = new BambooHrEmployee
            {
                FirstName = firstName,
                LastName = lastName,
                WorkEmail = workEmail
            };

            var url = await bambooHrClient.AddEmployee(bambooHrEmployee);

            Console.WriteLine($"Employee created at {url}");
            Console.WriteLine(bambooHrEmployee.PropsToString());
        }