使用 Cocoa 操作 Qiita API 的客户端
Qiita 是一个程序员使用的,用于轻松记录和公开编程 Tips、经验和笔记的技术信息共享服务。
AFQiitaClient
是 AFHTTPClient
的子类,与 AFNetworking 的源代码一起,可以将它添加到项目中并使用。
AFQiitaClient *client = [[AFQiitaClient alloc] init];
[client authenticateWithUsername:@"ngs"
password:@"qwerty1234!?"
success:success:^ {
NSLog(@"Success");
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
AFQiitaResponse
有一个返回 NSURL 类型的实例方法的成员:`firstURL`, `prevURL`, `nextURL`, 和 `prevURL`。
[client tagsWithSuccess:^(AFQiitaResponse *response) {
if(response.nextURL) {
[client getURL:response.nextURL
success:^(AFQiitaResponse *response) { ... }
failure:^(NSError *error) { ... }];
} else {
NSLog(@"No more tags");
}
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
[client rateLimitWithSuccess:^(NSInteger remaining, NSInteger limit){
NSLog(@"Success: Limted to %d, %d remaining", limit, remaining);
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
[client currentUserWithSuccess:^(AFQiitaResponse *response) {
AFQiitaUser *me = [response first];
NSLog(@"Success: Hello, my name is %@", me.name);
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
[client userWithUsername:@"yaotti@github"
success:^(AFQiitaResponse *response) {
AFQiitaUser *user = [response first];
NSLog(@"Success: %@ has %d followers", user.name, user.followers);
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
[client itemsWithUsername:@"yaotti@github"
success:^(AFQiitaResponse *response) {
AFQiitaItem *item = nil;
while(item = response.next)
NSLog(@"Post: %@", item.title);
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
[client stockedItemsWithUsername:@"yaotti@github"
success:^(AFQiitaResponse *response) {
AFQiitaItem *item = nil;
while(item = response.next)
NSLog(@"Post: %@", item.title);
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
[client itemsWithTag:@"Rails"
success:^(AFQiitaResponse *response) {
AFQiitaItem *item = nil;
while(item = response.next)
NSLog(@"Post: %@", item.title);
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
[client tagsWithSuccess:^(AFQiitaResponse *response) {
AFQiitaTag *tag = nil;
while(tag = response.next)
NSLog(@"Tag: %@", tag.name);
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
[client itemsWithSearchString:@"Hackathon"
stocked:NO
success:^(AFQiitaResponse *response) {
AFQiitaItem *item = nil;
while(item = response.next)
NSLog(@"Post: %@", item.title);
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
[client recentItemsWithSuccess:^(AFQiitaResponse *response) {
AFQiitaItem *item = nil;
while(item = response.next)
NSLog(@"Post: %@", item.title);
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
[client stockedItemsWithSuccess:^(AFQiitaResponse *response) {
AFQiitaItem *item = nil;
while(item = response.next)
NSLog(@"Post: %@", item.title);
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
AFQiitaItem *item = [[AFQiitaItem alloc]
initWithTitle:@"テスト!"
body:@"[AFQiitaClient](http://github.com/ngs/AFQiitaClient) から投稿テスト"];
[item addTag:[AFQiitaTag tagWithName:@"iOS"
versions:@"5.1.1", @"6.0", nil]];
[item setTweet:YES]; // Posts URL to Twitter
[item setGist:YES]; // Share code blocks on Gist
[client createItem:item
success:success:^(AFQiitaResponse *response) {
AFQiitaItem *createdItem = [response first];
NSLog(@"Success! UUID is %@", createdItem.uuid);
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
AFQiitaItem *item = ...; // Received from API
[item setTitle:@"テスト!(追記アリ)"];
[item setBody:@"[AFQiitaClient](http://github.com/ngs/AFQiitaClient) から投稿テスト\n\n##追記 (2012/10/13)\n\n* 非公開にしてみた"];
[item removeTag:[item.tags objectAtIndex:2]];
[item addTag:[AFQiitaTag tagWithName:@"Rails"]];
[item publicize]; // 限定公開のものを public に変更のみ可能
[client updateItem:item
success:success:^(AFQiitaResponse *response) {
AFQiitaItem *updatedItem = [response first];
NSLog(@"Success! Updated at %@", updatedItem.updatedAt);
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
[client deleteItem:item // Alias for deleteItemWithUUID:(NSString *)uuid
success:success:^(AFQiitaResponse *response) {
NSLog(@"Successfully deleted");
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
[client itemWithUUID:@"1a43e55e7209c8f3c565"
success:success:^ {
AFQiitaItem *item = [response first];
NSLog(@"Success: Title is %@, posted by %@ at %@",
item.title, item.user.name, item.createdAt);
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
[client stockItem:item // Alias for stockItemWithUUID:(NSString *)uuid
success:success:^ {
NSLog(@"Successfully stocked");
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
[client unstockItem:item // Alias for unstockItemWithUUID:(NSString *)uuid
success:success:^ {
NSLog(@"Successfully unstocked");
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
AFQiitaClient 以 MIT 许可证分发。有关详细信息,请参阅 LICENSE 文件。