Added some progress towards search.

Change-Id: I755f6c05a011461ba78441d5e1cfb8d8dfeb19e1
This commit is contained in:
Nick O'Neill 2013-12-11 11:57:22 -08:00
parent 4e54a3515b
commit 5aa6fbff61
5 changed files with 86 additions and 40 deletions

View File

@ -8,6 +8,7 @@
#import "LAAppDelegate.h"
#import "LACamliFile.h"
#import "LAViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
@implementation LAAppDelegate
@ -27,7 +28,9 @@
NSAssert(credentials[@"camlistore_password"], @"no camlistore password specified");
self.client = [[LACamliClient alloc] initWithServer:[NSURL URLWithString:credentials[@"camlistore_url"]] username:credentials[@"camlistore_username"] andPassword:credentials[@"camlistore_password"]];
[(LAViewController *)self.window.rootViewController setClient:self.client];
self.library = [[ALAssetsLibrary alloc] init];
return YES;

View File

@ -28,9 +28,10 @@
@property BOOL authorizing;
- (id)initWithServer:(NSURL *)server username:(NSString *)username andPassword:(NSString *)password;
- (void)discoveryWithUsername:(NSString *)user andPassword:(NSString *)pass;
- (void)getRecentItemsWithCompletion:(void (^)(NSArray *objects))completion;
- (BOOL)fileAlreadyUploaded:(LACamliFile *)file;
- (void)addFile:(LACamliFile *)file withCompletion:(void (^)())completion;

View File

@ -44,6 +44,48 @@
return self;
}
#pragma mark - discovery
// if we don't have blobroot with which to make these requests, we need to find it first
- (void)discoveryWithUsername:(NSString *)user andPassword:(NSString *)pass
{
self.authorizing = YES;
NSURLSessionConfiguration *discoverConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
discoverConfig.HTTPAdditionalHeaders = @{@"Accept": @"text/x-camli-configuration", @"Authorization": [NSString stringWithFormat:@"Basic %@",[self encodedAuth]]};
NSURLSession *discoverSession = [NSURLSession sessionWithConfiguration:discoverConfig delegate:self delegateQueue:nil];
NSURL *discoveryURL = [self.serverURL URLByAppendingPathComponent:@"ui/"];
NSURLSessionDataTask *data = [discoverSession dataTaskWithURL:discoveryURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
self.authorizing = NO;
if (error) {
LALog(@"error discovery: %@",error);
} else {
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
if (res.statusCode != 200) {
LALog(@"error with discovery: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
} else {
NSError *parseError;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
self.blobRoot = json[@"blobRoot"];
self.isAuthorized = YES;
[self.uploadQueue setSuspended:NO];
LALog(@"good discovery");
}
}
}];
[data resume];
}
#pragma mark - upload methods
- (BOOL)fileAlreadyUploaded:(LACamliFile *)file
{
NSParameterAssert(file);
@ -80,49 +122,40 @@
[self.uploadQueue addOperation:op];
}
// if we don't have blobroot with which to make these requests, we need to find it first
- (void)discoveryWithUsername:(NSString *)user andPassword:(NSString *)pass
{
self.authorizing = YES;
NSURLSessionConfiguration *discoverConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
discoverConfig.HTTPAdditionalHeaders = @{@"Accept": @"text/x-camli-configuration", @"Authorization": [NSString stringWithFormat:@"Basic %@",[self encodedAuth]]};
NSURLSession *discoverSession = [NSURLSession sessionWithConfiguration:discoverConfig delegate:self delegateQueue:nil];
NSURL *discoveryURL = [self.serverURL URLByAppendingPathComponent:@"ui/"];
NSURLSessionDataTask *data = [discoverSession dataTaskWithURL:discoveryURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
self.authorizing = NO;
if (error) {
LALog(@"error discovery: %@",error);
} else {
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
if (res.statusCode != 200) {
LALog(@"error with discovery: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
} else {
NSError *parseError;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
self.blobRoot = json[@"blobRoot"];
self.isAuthorized = YES;
[self.uploadQueue setSuspended:NO];
LALog(@"good discovery");
}
}
}];
[data resume];
}
- (NSURL *)statUrl
{
return [[self.serverURL URLByAppendingPathComponent:self.blobRoot] URLByAppendingPathComponent:@"camli/stat"];
}
#pragma mark - getting stuff
- (void)getRecentItemsWithCompletion:(void (^)(NSArray *objects))completion
{
NSMutableArray *objects = [NSMutableArray array];
NSURL *recentUrl = [[[[self.serverURL URLByAppendingPathComponent:@"my-search"] URLByAppendingPathComponent:@"camli"] URLByAppendingPathComponent:@"search"] URLByAppendingPathComponent:@"query"];
LALog(@"reent url: %@",recentUrl);
NSDictionary *formData = @{@"describe": @{@"thumbnailSize": @"200"}, @"sort": @"1", @"limit": @"50", @"constraint": @{@"logical": @{@"op": @"and", @"a":@{@"camliType":@"permanode"}, @"b":@{@"permanode": @{@"modTime": @{}}}}}};
LALog(@"form data: %@",formData);
NSMutableURLRequest *recentRequest = [NSMutableURLRequest requestWithURL:recentUrl];
[recentRequest setHTTPMethod:@"POST"];
[recentRequest setHTTPBody:[NSKeyedArchiver archivedDataWithRootObject:formData]];
NSURLSessionDataTask *recentData = [self.session dataTaskWithRequest:recentRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
LALog(@"got some response: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
[recentData resume];
// completion([NSArray arrayWithArray:objects]);
}
#pragma mark - utility
- (NSString *)encodedAuth
{
NSString *auth = [NSString stringWithFormat:@"%@:%@",self.username,self.password];

View File

@ -8,6 +8,10 @@
#import <UIKit/UIKit.h>
@class LACamliClient;
@interface LAViewController : UIViewController
@property LACamliClient *client;
@end

View File

@ -7,6 +7,7 @@
//
#import "LAViewController.h"
#import "LACamliClient.h"
@interface LAViewController ()
@ -17,6 +18,10 @@
- (void)viewDidLoad
{
[super viewDidLoad];
[self.client getRecentItemsWithCompletion:^(NSArray *objects) {
LALog(@"got objects: %@",objects);
}];
}
#pragma mark - collection methods