Skip to content

Commit

Permalink
Added happy path notifications for key discovery and auth events
Browse files Browse the repository at this point in the history
  • Loading branch information
followben committed Oct 5, 2012
1 parent 228db04 commit 977ad77
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
17 changes: 13 additions & 4 deletions TPTentClient/TPTentClient.h
Expand Up @@ -72,10 +72,19 @@ extern NSString * const TPTentClientPostTypeProfileModification;
extern NSString * const TPTentClientPostTypeDeleteNotification;

// Notifications and UserInfo dict keys
extern NSString * const TPTentClientDidRegisterWithEntityNotification;
extern NSString * const TPTentClientDidRegisterWithEntityNotificationURLKey;
extern NSString * const TPTentServerDiscoveryCheckingHeadResponseForProfileLinkNotification;
extern NSString * const TPTentServerDiscoveryFellBackToGetResponseForProfileLinkNotification;
extern NSString * const TPTentServerDiscoveryFetchingCanonicalURLsFromProfileNotification;
extern NSString * const TPTentServerDiscoveryCompleteNotification;
extern NSString * const TPTentServerDiscoveryCompleteNotificationCanonicalServerURLKey;
extern NSString * const TPTentServerDiscoveryCompleteNotificationCanonicalEntityURLKey;
extern NSString * const TPTentClientWillAuthorizeWithTentServerNotification;
extern NSString * const TPTentClientDidAuthorizeWithTentServerNotification;
extern NSString * const TPTentClientAuthorizingWithTentServerURLKey;

@protocol TPTentClientDelegate <NSObject>
@optional
- (void)tentClient:(TPTentClient *)tentClient didAuthorizeWithTentServerURL:(NSURL *)url;
@end
// TODO: notes and delegate calls should map one-to-one
- (void)tentClient:(TPTentClient *)tentClient didAuthorizeWithTentServerURL:(NSURL *)canonicalServerURL;
@end

41 changes: 35 additions & 6 deletions TPTentClient/TPTentClient.m
Expand Up @@ -29,6 +29,7 @@

#pragma mark - Constants

#pragma mark Post Types
NSString * const TPTentClientPostTypeStatus = @"https://tent.io/types/post/status/v0.1.0";
NSString * const TPTentClientPostTypeEssay = @"https://tent.io/types/post/essay/v0.1.0";
NSString * const TPTentClientPostTypePhoto = @"https://tent.io/types/post/photo/v0.1.0";
Expand All @@ -37,13 +38,21 @@
NSString * const TPTentClientPostTypeProfileModification = @"https://tent.io/types/post/profile/v0.1.0";
NSString * const TPTentClientPostTypeDeleteNotification = @"https://tent.io/types/post/delete/v0.1.0";

#pragma mark Notifications
NSString * const TPTentServerDiscoveryCheckingHeadResponseForProfileLinkNotification = @"com.thoughtfulpixel.tptentclient.note.discovery.headprofile";
NSString * const TPTentServerDiscoveryFellBackToGetResponseForProfileLinkNotification = @"com.thoughtfulpixel.tptentclient.note.discovery.getprofile";
NSString * const TPTentServerDiscoveryFetchingCanonicalURLsFromProfileNotification = @"com.thoughtfulpixel.tptentclient.note.discovery.locateservers";
NSString * const TPTentServerDiscoveryCompleteNotification = @"com.thoughtfulpixel.tptentclient.note.discovery.complete";
NSString * const TPTentServerDiscoveryCompleteNotificationCanonicalServerURLKey = @"TPTentClientCanonicalServerURLKey";
NSString * const TPTentServerDiscoveryCompleteNotificationCanonicalEntityURLKey = @"TPTentClientCanonicalEntityURLKey";
NSString * const TPTentClientWillAuthorizeWithTentServerNotification = @"com.thoughtfulpixel.tptentclient.note.authorization.willauthorise";
NSString * const TPTentClientDidAuthorizeWithTentServerNotification = @"com.thoughtfulpixel.tptentclient.note.authorization.didauthorise";
NSString * const TPTentClientAuthorizingWithTentServerURLKey = @"TPTentClientServerURLKey";

#pragma mark Private
static NSString * const TPTentClientProfileInfoTypeCore = @"https://tent.io/types/info/core/v0.1.0";
static NSString * const TPTentClientProfileInfoTypeBasic = @"https://tent.io/types/info/basic/v0.1.0";

NSString * const TPTentClientDidRegisterWithEntityNotification = @"com.thoughtfulpixel.tptentclient.notification.didregisterwithentity";
NSString * const TPTentClientDidRegisterWithEntityNotificationURLKey = @"TPTentClientDidRegisterWithEntityURL";



#pragma mark

Expand Down Expand Up @@ -97,6 +106,10 @@ - (void)authorizeForTentServerURL:(NSURL *)url
self.httpClient.delegate = self;
}

[[NSNotificationCenter defaultCenter] postNotificationName:TPTentClientWillAuthorizeWithTentServerNotification
object:nil
userInfo:@{TPTentClientAuthorizingWithTentServerURLKey: url}];

[self.httpClient registerForBaseURLWithSuccess:success failure:failure];
}

Expand Down Expand Up @@ -174,9 +187,9 @@ - (void)postPostWithType:(NSString *)postType permissions:(NSDictionary *)permis

- (void)httpClientDidRegisterWithBaseURL:(TPTentHTTPClient *)httpClient
{
[[NSNotificationCenter defaultCenter] postNotificationName:TPTentClientDidRegisterWithEntityNotification
[[NSNotificationCenter defaultCenter] postNotificationName:TPTentClientDidAuthorizeWithTentServerNotification
object:nil
userInfo:@{TPTentClientDidRegisterWithEntityNotification: httpClient.baseURL}];
userInfo:@{TPTentClientAuthorizingWithTentServerURLKey: httpClient.baseURL}];

if ([self.delegate respondsToSelector:@selector(tentClient:didAuthorizeWithTentServerURL:)]) {
[self.delegate tentClient:self didAuthorizeWithTentServerURL:httpClient.baseURL];
Expand All @@ -191,6 +204,10 @@ - (void)headTentServerWithDiscoveryHTTPClient:(AFHTTPClient *)discoveryHTTPClien
success:(void (^)(NSURL *canonicalServerURL, NSURL *canonicalEntityURL))success
failure:(void (^)(NSError *error))failure
{
[[NSNotificationCenter defaultCenter] postNotificationName:TPTentServerDiscoveryCheckingHeadResponseForProfileLinkNotification
object:nil
userInfo:nil];

NSMutableURLRequest *headRequest = [discoveryHTTPClient requestWithMethod:@"HEAD" path:@"/" parameters:nil];

AFHTTPRequestOperation *headOperation = [[AFHTTPRequestOperation alloc] initWithRequest:headRequest];
Expand Down Expand Up @@ -253,6 +270,10 @@ - (void)getTentServerWithDiscoveryHTTPClient:(AFHTTPClient *)discoveryHTTPClient
success:(void (^)(NSURL *canonicalServerURL, NSURL *canonicalEntityURL))success
failure:(void (^)(NSError *error))failure
{
[[NSNotificationCenter defaultCenter] postNotificationName:TPTentServerDiscoveryFellBackToGetResponseForProfileLinkNotification
object:nil
userInfo:nil];

[discoveryHTTPClient getPath:@"/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSURL *profileURL = [self profileURLFromCompletedGetOperation:operation
responseString:[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]];
Expand Down Expand Up @@ -323,6 +344,10 @@ - (void)getCanonicalURLsFromProfileURL:(NSURL *)url
success:(void (^)(NSURL *canonicalServerURL, NSURL *canonicalEntityURL))success
failure:(void (^)(NSError *error))failure
{
[[NSNotificationCenter defaultCenter] postNotificationName:TPTentServerDiscoveryFetchingCanonicalURLsFromProfileNotification
object:nil
userInfo:nil];

AFHTTPClient *aHTTPClient = [[TPTentHTTPClient alloc] initWithBaseURL:url];

NSMutableURLRequest *getRequest = [aHTTPClient requestWithMethod:@"GET" path:[NSString string] parameters:nil];
Expand All @@ -347,6 +372,10 @@ - (void)getCanonicalURLsFromProfileURL:(NSURL *)url
return;
}

[[NSNotificationCenter defaultCenter] postNotificationName:TPTentServerDiscoveryCompleteNotification
object:nil
userInfo:@{TPTentServerDiscoveryCompleteNotificationCanonicalServerURLKey: tentServerURL,
TPTentServerDiscoveryCompleteNotificationCanonicalEntityURLKey: tentEntityURL}];
if (success) {
success(tentServerURL, tentEntityURL);
}
Expand Down

0 comments on commit 977ad77

Please sign in to comment.