Access the full API documentation here: http://apidocs.peoplehr.com/
Please see below two example code snippets calling the People API in Objective-C:
- (BOOL)tryConnection
{
// try to connect with the API and see if we can
// set the ENDPOINT
NSString *url = @"https://api.peoplehr.net/Employee";
// Construct the JSON string
NSString *post = [NSString stringWithFormat:@"{\"APIKey\": \"%@\", \"Action\": \"CheckAuthentication\", \"EmailAddress\": \"%@\", \"Password\": \"%@\"}", APIText.text, emailAddress.text, password.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *error;
// GET The data
NSData *jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error] : nil;
if (error) NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription);
int status = [[results objectForKey:@"Status"] intValue];
NSLog(@"%d", status);
if (status != 0)
{
NSString *error = [results objectForKey:@"Message"];
NSLog(@"%@", error);
errorMessage.text = error;
errorMessage.hidden = NO;
}
if (status == 0)
return YES;
return NO;
}
-(void)FirstTimeSetup
{
// use this as a one off to retrieve employee data from database and store it locally with a code
NSString *url = @"https://api.peoplehr.net/Employee";
NSString *post = [NSString stringWithFormat:@"{\"APIKey\":\"%@\", \"Action\": \"GetAllEmployeeDetail\", \"IncludeLeavers\": \"false\"}", API];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *error;
NSData *jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error] : nil;
if (error) NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription);
// padd the employee date
[Employee FirstTimeSetup:results];
}
+ (void)FirstTimeSetup:(NSDictionary*)data
{
AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[app.managedObjectContext setMergePolicy:NSOverwriteMergePolicy];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Employee" inManagedObjectContext:app.managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *fetchedObjects = [app.managedObjectContext executeFetchRequest:fetchRequest error:&error];
for (Employee *emp in fetchedObjects) {
[app.managedObjectContext deleteObject:emp];
}
Employee *emp;
NSArray* employees = [data objectForKey:@"Result"];
int loop = 1;
for (NSDictionary *employee in employees)
{
NSDictionary *data = employee[@"EmployeeId"];
NSString *empid = data[@"DisplayValue"];
emp = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:app.managedObjectContext];
emp.empid = empid;
// build the code
data = employee[@"StartDate"];
NSString *startDate = data[@"DisplayValue"]; // 2000-06-19
NSString *year = [startDate substringWithRange:NSMakeRange(9, 1)];
NSString *month = [startDate substringWithRange:NSMakeRange(6, 1)];
NSString *code = [NSString stringWithFormat:@"%@%@%04d",year,month,9999 - loop];
emp.code = code;
data = employee[@"EmailId"];
NSString *email = data[@"DisplayValue"];
emp.email = email;
NSLog(@"%@ %@ > %@",email, empid, code);
data = employee[@"FirstName"];
NSString *firstName = data[@"DisplayValue"];
data = employee[@"LastName"];
NSString *lastName = data[@"DisplayValue"];
emp.name = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
if (![app.managedObjectContext save:&error])
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
loop++;
}
}
Thanks,
Customer Services Team