Skip to content

Commit

Permalink
Merge pull request #17 from fealebenpae/master
Browse files Browse the repository at this point in the history
RestClient#stats
  • Loading branch information
mattheworiordan committed Aug 17, 2015
2 parents 9cc97a0 + 169ca8a commit defac0a
Show file tree
Hide file tree
Showing 15 changed files with 610 additions and 122 deletions.
18 changes: 14 additions & 4 deletions ably-ios/ARTHttpPaginatedResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@
#import <ably/ARTHttp.h>
#import <ably/ARTPaginatedResult.h>

@interface ARTHttpPaginatedResult : NSObject <ARTPaginatedResult>
@interface ARTHttpPaginatedResult : ARTPaginatedResult

typedef id (^ARTHttpResponseProcessor)(ARTHttpResponse *);
typedef NSArray *(^ARTHttpResponseProcessor)(ARTHttpResponse *);

- (instancetype)init UNAVAILABLE_ATTRIBUTE;
- (instancetype)initWithHttp:(ARTHttp *)http current:(id)current contentType:(NSString *)contentType relFirst:(ARTHttpRequest *)relFirst relCurrent:(ARTHttpRequest *)relCurrent relNext:(ARTHttpRequest *)relNext responseProcessor:(ARTHttpResponseProcessor)responseProcessor;

+ (id<ARTCancellable>)makePaginatedRequest:(ARTHttp *)http request:(ARTHttpRequest *)request responseProcessor:(ARTHttpResponseProcessor)responseProcessor cb:(ARTPaginatedResultCb)cb;
- (instancetype)initWithHttp:(ARTHttp *)http
items:(NSArray *)items
contentType:(NSString *)contentType
relFirst:(ARTHttpRequest *)relFirst
relCurrent:(ARTHttpRequest *)relCurrent
relNext:(ARTHttpRequest *)relNext
responseProcessor:(ARTHttpResponseProcessor)responseProcessor;

+ (id<ARTCancellable>)makePaginatedRequest:(ARTHttp *)http
request:(ARTHttpRequest *)request
responseProcessor:(ARTHttpResponseProcessor)responseProcessor
callback:(ARTPaginatedResultCallback)callback;

@end
78 changes: 36 additions & 42 deletions ably-ios/ARTHttpPaginatedResult.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,82 +9,70 @@
#import "ARTHttpPaginatedResult.h"
#import "ARTLog.h"
#import "ARTStatus.h"
@interface ARTHttpPaginatedResult ()

@property (readonly, strong, nonatomic) ARTHttp *http;
@property (readonly, strong, nonatomic) id currentValue;
@property (readonly, strong, nonatomic) NSString *contentType;
@property (readonly, strong, nonatomic) ARTHttpRequest *relFirst;
@property (readonly, strong, nonatomic) ARTHttpRequest *relCurrent;
@property (readonly, strong, nonatomic) ARTHttpRequest *relNext;
@property (readonly, strong, nonatomic) ARTHttpResponseProcessor responseProcessor;

@end

@implementation ARTHttpPaginatedResult
@implementation ARTHttpPaginatedResult {
ARTHttp *_http;
NSString *_contentType;
ARTHttpRequest *_relFirst;
ARTHttpRequest *_relCurrent;
ARTHttpRequest *_relNext;
ARTHttpResponseProcessor _responseProcessor;
}

- (instancetype)initWithHttp:(ARTHttp *)http current:(id)current contentType:(NSString *)contentType relFirst:(ARTHttpRequest *)relFirst relCurrent:(ARTHttpRequest *)relCurrent relNext:(ARTHttpRequest *)relNext responseProcessor:(ARTHttpResponseProcessor)responseProcessor {
- (instancetype)initWithHttp:(ARTHttp *)http items:(NSArray *)items contentType:(NSString *)contentType relFirst:(ARTHttpRequest *)relFirst relCurrent:(ARTHttpRequest *)relCurrent relNext:(ARTHttpRequest *)relNext responseProcessor:(ARTHttpResponseProcessor)responseProcessor {
self = [super init];
if (self) {
_http = http;
_currentValue = current;
_contentType = contentType;

_items = [items copy];
_contentType = [contentType copy];

_hasFirst = !!relFirst;
_relFirst = relFirst;

_hasCurrent = !!relCurrent;
_relCurrent = relCurrent;

_hasNext = !!relNext;
_relNext = relNext;

_responseProcessor = responseProcessor;
}
return self;
}

- (id)currentItems {
return self.currentValue;
}

- (BOOL)hasFirst {
return !!self.relFirst;
}

- (BOOL)hasCurrent {
return !!self.relCurrent;
}

- (BOOL)hasNext {
return !!self.relNext;
}

- (void)first:(ARTPaginatedResultCb)cb {
[ARTHttpPaginatedResult makePaginatedRequest:self.http request:self.relFirst responseProcessor:self.responseProcessor cb:cb];
- (void)first:(ARTPaginatedResultCallback)callback {
[ARTHttpPaginatedResult makePaginatedRequest:_http request:_relFirst responseProcessor:_responseProcessor callback:callback];
}

- (void)current:(ARTPaginatedResultCb)cb {
[ARTHttpPaginatedResult makePaginatedRequest:self.http request:self.relCurrent responseProcessor:self.responseProcessor cb:cb];
- (void)current:(ARTPaginatedResultCallback)callback {
[ARTHttpPaginatedResult makePaginatedRequest:_http request:_relCurrent responseProcessor:_responseProcessor callback:callback];
}

- (void)next:(ARTPaginatedResultCb)cb {
[ARTHttpPaginatedResult makePaginatedRequest:self.http request:self.relNext responseProcessor:self.responseProcessor cb:cb];
- (void)next:(ARTPaginatedResultCallback)callback {
[ARTHttpPaginatedResult makePaginatedRequest:_http request:_relNext responseProcessor:_responseProcessor callback:callback];
}


+ (id<ARTCancellable>)makePaginatedRequest:(ARTHttp *)http request:(ARTHttpRequest *)request responseProcessor:(ARTHttpResponseProcessor)responseProcessor cb:(ARTPaginatedResultCb)cb {
+ (id<ARTCancellable>)makePaginatedRequest:(ARTHttp *)http request:(ARTHttpRequest *)request responseProcessor:(ARTHttpResponseProcessor)responseProcessor callback:(ARTPaginatedResultCallback)callback {
return [http makeRequest:request cb:^(ARTHttpResponse *response) {
if (!response) {
ARTErrorInfo * info = [[ARTErrorInfo alloc] init];
[info setCode:40000 message:@"ARTHttpPaginatedResult got no response"];
[ARTStatus state:ARTStatusError info:info];
cb([ARTStatus state:ARTStatusError info:info], nil);
callback([ARTStatus state:ARTStatusError info:info], nil);
return;
}

if (response.status < 200 || response.status >= 300) {
ARTErrorInfo * info = [[ARTErrorInfo alloc] init];
[info setCode:40000 message:[NSString stringWithFormat:@"ARTHttpPaginatedResult response.status invalid: %d", response.status]];
cb([ARTStatus state:ARTStatusError info:info], nil);
callback([ARTStatus state:ARTStatusError info:info], nil);

return;
}

id currentValue = responseProcessor(response);
NSArray *items = responseProcessor(response);

NSString *contentType = response.contentType;
NSDictionary *links = response.links;
Expand All @@ -93,7 +81,13 @@ - (void)next:(ARTPaginatedResultCb)cb {
ARTHttpRequest *currentRelRequest = [request requestWithRelativeUrl:[links objectForKey:@"current"]];
ARTHttpRequest *nextRelRequest = [request requestWithRelativeUrl:[links objectForKey:@"next"]];

cb(ARTStatusOk, [[ARTHttpPaginatedResult alloc] initWithHttp:http current:currentValue contentType:contentType relFirst:firstRelRequest relCurrent:currentRelRequest relNext:nextRelRequest responseProcessor:responseProcessor]);
ARTPaginatedResult *result = [[ARTHttpPaginatedResult alloc] initWithHttp:http
items:items
contentType:contentType
relFirst:firstRelRequest
relCurrent:currentRelRequest
relNext:nextRelRequest responseProcessor:responseProcessor];
callback([ARTStatus state:ARTStatusOk], result);
}];
}

Expand Down
28 changes: 19 additions & 9 deletions ably-ios/ARTPaginatedResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,26 @@
#import <Foundation/Foundation.h>
#import <ably/ARTStatus.h>

@protocol ARTPaginatedResult
@interface ARTPaginatedResult : NSObject {
@protected
NSArray *_items;
BOOL _hasFirst;
BOOL _hasCurrent;
BOOL _hasNext;
}

- (id)currentItems;
- (BOOL)hasFirst;
- (BOOL)hasCurrent;
- (BOOL)hasNext;
@property (nonatomic, strong, readonly) NSArray *items;

typedef void (^ARTPaginatedResultCb)(ARTStatus *status, id<ARTPaginatedResult> result);
- (void)first:(ARTPaginatedResultCb)cb;
- (void)current:(ARTPaginatedResultCb)cb;
- (void)next:(ARTPaginatedResultCb)cb;
@property (nonatomic, readonly) BOOL hasFirst;
@property (nonatomic, readonly) BOOL hasCurrent;
@property (nonatomic, readonly) BOOL hasNext;

@property (nonatomic, readonly) BOOL isLast;

typedef void(^ARTPaginatedResultCallback)(ARTStatus *status, ARTPaginatedResult *result);

- (void)first:(ARTPaginatedResultCallback)callback;
- (void)current:(ARTPaginatedResultCallback)callback;
- (void)next:(ARTPaginatedResultCallback)callback;

@end
29 changes: 29 additions & 0 deletions ably-ios/ARTPaginatedResult.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// ARTPaginatedResult.m
// ably
//
// Created by Yavor Georgiev on 10.08.15.
// Copyright (c) 2015 г. Ably. All rights reserved.
//

#import "ARTPaginatedResult.h"

@implementation ARTPaginatedResult

- (void)first:(ARTPaginatedResultCallback)callback {
NSAssert(false, @"-[ARTPaginatedResult first] should always be overriden.");
}

- (void)current:(ARTPaginatedResultCallback)callback {
NSAssert(false, @"-[ARTPaginatedResult current] should always be overriden.");
}

- (void)next:(ARTPaginatedResultCallback)callback {
NSAssert(false, @"-[ARTPaginatedResult next] should always be overriden.");
}

- (BOOL)isLast {
return !self.hasNext;
}

@end
17 changes: 9 additions & 8 deletions ably-ios/ARTRealtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import <ably/ARTClientOptions.h>
#import <ably/ARTPresenceMessage.h>
#import <ably/ARTPaginatedResult.h>
#import <ably/ARTStats.h>


#define ART_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
Expand Down Expand Up @@ -54,8 +55,8 @@ typedef NS_ENUM(NSUInteger, ARTRealtimeConnectionState) {
- (void)publish:(id)payload withName:(NSString *)name cb:(ARTStatusCallback)cb;
- (void)publish:(id)payload cb:(ARTStatusCallback)cb;

- (id<ARTCancellable>)history:(ARTPaginatedResultCb)cb;
- (id<ARTCancellable>)historyWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCb)cb;
- (id<ARTCancellable>)history:(ARTPaginatedResultCallback)callback;
- (id<ARTCancellable>)historyWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCallback)callback;



Expand All @@ -80,10 +81,10 @@ typedef void (^ARTRealtimeChannelStateCb)(ARTRealtimeChannelState, ARTStatus *);

@interface ARTPresence : NSObject
- (instancetype) initWithChannel:(ARTRealtimeChannel *) channel;
- (id<ARTCancellable>)get:(ARTPaginatedResultCb) cb;
- (id<ARTCancellable>)getWithParams:(NSDictionary *) queryParams cb:(ARTPaginatedResultCb) cb;
- (id<ARTCancellable>)history:(ARTPaginatedResultCb)cb;
- (id<ARTCancellable>)historyWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCb)cb;
- (id<ARTCancellable>)get:(ARTPaginatedResultCallback)callback;
- (id<ARTCancellable>)getWithParams:(NSDictionary *) queryParams cb:(ARTPaginatedResultCallback)callback;
- (id<ARTCancellable>)history:(ARTPaginatedResultCallback)callback;
- (id<ARTCancellable>)historyWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCallback)callback;

- (void)enter:(id)data cb:(ARTStatusCallback)cb;
- (void)update:(id)data cb:(ARTStatusCallback)cb;
Expand Down Expand Up @@ -125,8 +126,8 @@ typedef void (^ARTRealtimeChannelPresenceCb)(ARTPresenceMessage *);

typedef void (^ARTRealtimePingCb)(ARTStatus *);
- (void)ping:(ARTRealtimePingCb) cb;
- (id<ARTCancellable>)stats:(ARTPaginatedResultCb)cb;
- (id<ARTCancellable>)statsWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCb)cb;

- (id<ARTCancellable>)stats:(ARTStatsQuery *)query callback:(ARTPaginatedResultCallback)callback;

- (ARTRealtimeChannel *)channel:(NSString *)channelName;
- (ARTRealtimeChannel *)channel:(NSString *)channelName cipherParams:(ARTCipherParams *)cipherParams;
Expand Down
32 changes: 14 additions & 18 deletions ably-ios/ARTRealtime.m
Original file line number Diff line number Diff line change
Expand Up @@ -471,17 +471,17 @@ -(void) throwOnDisconnectedOrFailed {
[NSException raise:@"realtime cannot perform action in disconnected or failed state" format:@"state: %d", (int)self.realtime.state];
}
}
- (id<ARTCancellable>)history:(ARTPaginatedResultCb)cb {
- (id<ARTCancellable>)history:(ARTPaginatedResultCallback)callback {
[self throwOnDisconnectedOrFailed];
return [self.restChannel history:cb];
return [self.restChannel history:callback];
}

- (id<ARTCancellable>)historyWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCb)cb {
- (id<ARTCancellable>)historyWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCallback)callback {
[self throwOnDisconnectedOrFailed];
if([queryParams objectForKey:@"until_attach"] != nil && self.state != ARTRealtimeChannelAttached) {
[NSException raise:@"Cannot ask for history with param untilAttach when not attached" format:@""];
}
return [self.restChannel historyWithParams:queryParams cb:cb];
return [self.restChannel historyWithParams:queryParams cb:callback];
}


Expand Down Expand Up @@ -903,12 +903,8 @@ - (void)ping:(ARTRealtimePingCb) cb {
[self.transport sendPing];
}

- (id<ARTCancellable>)stats:(ARTPaginatedResultCb)cb {
return [self.rest stats:cb];
}

- (id<ARTCancellable>)statsWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCb)cb {
return [self.rest statsWithParams:queryParams cb:cb];
- (id<ARTCancellable>)stats:(ARTStatsQuery *)query callback:(ARTPaginatedResultCallback)callback {
return [self.rest stats:query callback:callback];
}

- (ARTRealtimeChannel *)channel:(NSString *)channelName {
Expand Down Expand Up @@ -1591,23 +1587,23 @@ -(instancetype) initWithChannel:(ARTRealtimeChannel *) channel {
}
return self;
}
-(id<ARTCancellable>) getWithParams:(NSDictionary *) queryParams cb:(ARTPaginatedResultCb) cb {
-(id<ARTCancellable>) getWithParams:(NSDictionary *) queryParams cb:(ARTPaginatedResultCallback)callback {
[self.channel throwOnDisconnectedOrFailed];
return [self.channel.restChannel.presence getWithParams:queryParams cb:cb];
return [self.channel.restChannel.presence getWithParams:queryParams cb:callback];
}

-(id<ARTCancellable>) get:(ARTPaginatedResultCb) cb {
-(id<ARTCancellable>) get:(ARTPaginatedResultCallback)callback {
[self.channel throwOnDisconnectedOrFailed];
return [self.channel.restChannel.presence get:cb];
return [self.channel.restChannel.presence get:callback];
}
- (id<ARTCancellable>)history:(ARTPaginatedResultCb)cb {
- (id<ARTCancellable>)history:(ARTPaginatedResultCallback)callback {
[self.channel throwOnDisconnectedOrFailed];
return [self.channel.restChannel.presence history:cb];
return [self.channel.restChannel.presence history:callback];
}

- (id<ARTCancellable>) historyWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCb)cb {
- (id<ARTCancellable>) historyWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCallback)callback {
[self.channel throwOnDisconnectedOrFailed];
return [self.channel.restChannel.presence historyWithParams:queryParams cb:cb];
return [self.channel.restChannel.presence historyWithParams:queryParams cb:callback];
}


Expand Down
17 changes: 8 additions & 9 deletions ably-ios/ARTRest.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#import <ably/ARTTypes.h>
#import <ably/ARTClientOptions.h>
#import <ably/ARTPaginatedResult.h>

#import <ably/ARTStats.h>

@class ARTLog;
@class ARTCipherParams;
Expand All @@ -24,18 +24,18 @@
- (id<ARTCancellable>)publish:(id)payload withName:(NSString *)name cb:(ARTStatusCallback)cb;
- (id<ARTCancellable>)publish:(id)payload cb:(ARTStatusCallback)cb;

- (id<ARTCancellable>)history:(ARTPaginatedResultCb)cb;
- (id<ARTCancellable>)historyWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCb)cb;
- (id<ARTCancellable>)history:(ARTPaginatedResultCallback)callback;
- (id<ARTCancellable>)historyWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCallback)callback;

@property (readonly, strong, nonatomic) ARTRestPresence *presence;
@end

@interface ARTRestPresence : NSObject
- (instancetype) initWithChannel:(ARTRestChannel *) channel;
- (id<ARTCancellable>)get:(ARTPaginatedResultCb)cb;
- (id<ARTCancellable>)getWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCb)cb;
- (id<ARTCancellable>)history:(ARTPaginatedResultCb)cb;
- (id<ARTCancellable>)historyWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCb)cb;
- (id<ARTCancellable>)get:(ARTPaginatedResultCallback)callback;
- (id<ARTCancellable>)getWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCallback)callback;
- (id<ARTCancellable>)history:(ARTPaginatedResultCallback)callback;
- (id<ARTCancellable>)historyWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCallback)callback;
@end

@interface ARTRest : NSObject
Expand All @@ -50,8 +50,7 @@
- (id<ARTCancellable>) token:(ARTAuthTokenParams *) keyName tokenCb:(void (^)(ARTStatus * status, ARTTokenDetails *)) cb;

- (id<ARTCancellable>)time:(void(^)(ARTStatus * status, NSDate *time))cb;
- (id<ARTCancellable>)stats:(ARTPaginatedResultCb)cb;
- (id<ARTCancellable>)statsWithParams:(NSDictionary *)queryParams cb:(ARTPaginatedResultCb)cb;
- (id<ARTCancellable>)stats:(ARTStatsQuery *)query callback:(ARTPaginatedResultCallback)callback;
- (id<ARTCancellable>)internetIsUp:(void (^)(bool isUp)) cb;
- (ARTRestChannel *)channel:(NSString *)channelName;
- (ARTRestChannel *)channel:(NSString *)channelName cipherParams:(ARTCipherParams *)cipherParams;
Expand Down
Loading

0 comments on commit defac0a

Please sign in to comment.