LAClient 1.1.0

LAClient 1.1.0

测试已测试
语言语言 Obj-CObjective C
LICENSE MIT 协议
发布最后发布2017年3月

Seth JordanBitBlox 维护。



LAClient 1.1.0

  • 作者:
  • 开发者

光格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

API 客户端配置

基本

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 请求。

  • 请注意,'securityDomain' 必须是唯一的,因为每个 LASecurityProvider 对象创建时都要有一个唯一标识。例如,如果您创建了两个不同的 API 客户端来与两个不同的 API 通信,但它们使用了同一个 OAuth 服务器(客户端凭据等),那么您必须使用具有相同密钥链应用程序 ID 的相同的 OAuth 客户端实例,否则您会得到刷新竞争条件。

做出 API 调用

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");
                }
              }];

待办事项

  • 完成演示 - 查找示例 API 进行测试