光格API客户端(LAClient)是一个简单、轻量级的客户端,用于处理基于REST的API。
最基本的用法如下:
LAClient *apiClient = [[LAClient alloc] initWithURL:[NSURL URLWithString:@"https:///api"];
[apiClient getResource:[Person class]
atPath:@"person/123"
callback:^(id resource, NSHTTPURLResponse *response, NSError *error) {
Person *person = resource;
NSLog(@"Person found: %@", person);
}];
将 LightAPIClient 目录(及其所有内容)添加到您的项目中。将 Security.framework 添加到您的项目中。
或者:
将以下内容添加到您的 Podfile 中:
pod 'LAClient', '1.0.0'
您的类可以实现 LARepresentation 协议,并将它们的属性从 API 调用返回的原始数据中映射出来。
或者:
创建代表对象,它们具有与您从 API 获取的 JSON 表示相同的属性,并将您的对象扩展为 LAJsonRepresentation。此类尝试将 API 返回的 JSON 表示反序列化并映射到您的对象模型中。在大多数情况下,这都有效,除非是数组。由于 Objective C 没有泛型,我们无法了解您在数组中期望的对象类型。因此,如果您的对象有一个属性是数组,您需要编写一个自定义的设定器来 'type' 响应。下面是一个例子:
JSON 表示形式
{firstName:"Lana", lastName:"Del-rey", favoriteFood:"pizza", birthday:334179084000, phoneNumbers:[{"type:":"mobile", "number":"123-456-7890"}], uri:"https:///api/person/123"}
类
@interface Person : LAJsonResource
@property (nonatomic, retain) NSString *firstName;
@property (nonatomic, retain) NSString *lastName;
@property (nonatomic, retain) NSString *favoriteFood;
@property (nonatomic, retain) NSDate *birthday;
@property (nonatomic, retain) NSString *uri;
@property (nonatomic, retain) NSArray *phoneNumbers;
@end
...
@implementation Person
/*
Because the deserialization process cannot detect what
type of object the 'phoneNumber' array consists of,
you need to tell it how to desearizlize in this way.
*/
-(void)setPhoneNumbers:(NSArray *)phoneNumbers{
_phoneNumbers = [self typedArrayWithType:[PhoneNumber class] value:phoneNumberrs];
}
/*
If you need to customize the serialization of date formats to suite your api,
you can have your representation implement a dateformmatter to be used
by the client
*/
-(NSDateFormatter*)dateformatter{
if(_dateformatter == nil){
_dateformatter = [[NSDateFormatter alloc] init];
_dateformatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
[_dateformatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
}
return _dateformatter;
}
@end
LAClient *apiClient = [[LAClient alloc] initWithURL:[NSURL URLWithString:@"https:///api"];
可以通过设置属性来启用客户端的附加功能。例如
LAClient *apiClient = [[LAClient alloc] initWithURL:[NSURL URLWithString:@"https:///api"]];
apiClient.connectionUserAgent = @"iOS App V1.2";
apiClient.connectionTimeoutInSeconds = 30;
apiClient.debugEnabled = YES;
apiClient.securityProvider = [[LASimpleOAuthProvider alloc] initWithURL:[NSURL URLWithString:@"https://:8081/oauth"]
securityDomain:@"security_domain"
clientId:@"my_client"
clientSecret:@"my_client_secret"]];
在客户端上设置安全提供程序会导致它使用该安全提供程序来保护对 API 的 HTTP 请求。
apiClient getResourceList:[Person class]
atPath:@"person"
callback:^(id resource, NSHTTPURLResponse *response, NSError *error) {
NSArray *people = resource;
NSLog(@"%d people found", people.count);
}];
apiClient getResource:[Person class]
atPath:@"person/123"
callback:^(id resource, NSHTTPURLResponse *response, NSError *error) {
Person *person = resource;
NSLog(@"Person found: %@", person);
}];
...
person.favoriteFood = @"Lasagna";
[apiClient putResource:person
callback:^(id resource, NSHTTPURLResponse *response, NSError *error) {
if(error != nil && response.statusCode == 200){}
NSLog(@"Person saved");
}
}];