Skip to content

Commit

Permalink
Merge pull request #171 from ably/refactor-payload
Browse files Browse the repository at this point in the history
Rename payload -> data, refactor message data encoding.
  • Loading branch information
tcard committed Feb 2, 2016
2 parents 12d5c77 + d5719b3 commit 724d035
Show file tree
Hide file tree
Showing 34 changed files with 413 additions and 952 deletions.
3 changes: 1 addition & 2 deletions ably-ios/ARTAuthTokenParams.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#import "ARTDefault.h"
#import "ARTEncoder.h"
#import "ARTPayload.h"
#import "ARTAuthTokenRequest.h"

@implementation ARTAuthTokenParams
Expand Down Expand Up @@ -130,7 +129,7 @@ - (NSDictionary *)toDictionaryWithUnion:(NSArray *)items {

CCHmac(kCCHmacAlgSHA256, cKey, keyLen, cData, dataLen, hmac);
NSData *mac = [[NSData alloc] initWithBytes:hmac length:sizeof(hmac)];
NSString *str = [ARTBase64PayloadEncoder toBase64:mac];
NSString *str = [mac base64EncodedStringWithOptions:0];
return str;
}

Expand Down
14 changes: 7 additions & 7 deletions ably-ios/ARTBaseMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

#import <Foundation/Foundation.h>
#import "CompatibilityMacros.h"
#import "ARTPayload.h"
#import "ARTDataEncoder.h"
#import "ARTStatus.h"

ART_ASSUME_NONNULL_BEGIN

Expand All @@ -27,18 +28,17 @@ ART_ASSUME_NONNULL_BEGIN
@property (strong, nonatomic) NSString *connectionId;

/// Any transformation applied to the data for this message
@property (strong, nonatomic) NSString *encoding;
@property (strong, nonatomic, art_nullable) NSString *encoding;

/// The message payload.
@property (strong, nonatomic) ARTPayload *payload;
@property (strong, nonatomic, art_nullable) id data;

- (instancetype)decode:(id<ARTPayloadEncoder>)encoder;
- (instancetype)encode:(id<ARTPayloadEncoder>)encoder;
- (ARTStatus *__art_nonnull)decodeWithEncoder:(ARTDataEncoder*)encoder output:(id __art_nonnull*__art_nonnull)output;
- (ARTStatus *__art_nonnull)encodeWithEncoder:(ARTDataEncoder*)encoder output:(id __art_nonnull*__art_nonnull)output;

- (id)content;
- (NSString *)description;

- (instancetype)messageWithPayload:(ARTPayload *)payload;
- (instancetype)messageWithData:(id)data encoding:(NSString *)encoding;

@end

Expand Down
33 changes: 19 additions & 14 deletions ably-ios/ARTBaseMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "ARTBaseMessage.h"
#import "ARTLog.h"
#import "ARTStatus.h"

@implementation ARTBaseMessage

Expand All @@ -26,33 +27,37 @@ - (id)copyWithZone:(NSZone *)zone {
message->_id = self.id;
message->_clientId = self.clientId;
message->_timestamp = self.timestamp;
message->_payload = self.payload;
message->_data = self.data;
message->_connectionId = self.connectionId;
message->_encoding = self.encoding;
return message;
}

- (instancetype)messageWithPayload:(ARTPayload *)payload {
- (instancetype)messageWithData:(id)data encoding:(NSString *)encoding {
ARTBaseMessage *message = [self copy];
message.payload = payload;
message.data = data;
message.encoding = encoding;
return message;
}

- (instancetype)decode:(id<ARTPayloadEncoder>)encoder {
ARTPayload *payload = self.payload;
[encoder decode:payload output:&payload];
return [self messageWithPayload:payload];
- (ARTStatus *)decodeWithEncoder:(ARTDataEncoder*)encoder output:(id *)output {
ARTDataEncoderOutput *decoded = [encoder decode:self.data encoding:self.encoding];
*output = [self copy];
((ARTBaseMessage *)*output).data = decoded.data;
((ARTBaseMessage *)*output).encoding = decoded.encoding;
return decoded.status;
}

- (instancetype)encode:(id<ARTPayloadEncoder>)encoder {
ARTPayload *payload = self.payload;
[encoder encode:payload output:&payload];
return [self messageWithPayload:payload];
- (ARTStatus *)encodeWithEncoder:(ARTDataEncoder*)encoder output:(id *)output {
ARTDataEncoderOutput *encoded = [encoder encode:self.data];
*output = [self copy];
((ARTBaseMessage *)*output).data = encoded.data;
((ARTBaseMessage *)*output).encoding = [self.encoding artAddEncoding:encoded.encoding];
return encoded.status;
}

- (id)content {
// FIXME: payload.payload
return self.payload.payload;
return self.data;
}

- (NSString *)description {
Expand All @@ -62,7 +67,7 @@ - (NSString *)description {
[description appendFormat:@" connectionId: %@,\n", self.connectionId];
[description appendFormat:@" timestamp: %@,\n", self.timestamp];
[description appendFormat:@" encoding: %@,\n", self.encoding];
[description appendFormat:@" payload: %@\n", self.payload];
[description appendFormat:@" data: %@\n", self.data];
[description appendFormat:@"}"];
return description;
}
Expand Down
4 changes: 2 additions & 2 deletions ably-ios/ARTChannel+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#import "ARTChannel.h"
#import "ARTLog.h"

@protocol ARTPayloadEncoder;

ART_ASSUME_NONNULL_BEGIN

@interface ARTChannel()

@property (nonatomic, strong, art_null_resettable) ARTChannelOptions *options;
@property (nonatomic, strong, readonly) ARTDataEncoder *dataEncoder;

- (ARTMessage *__art_nonnull)encodeMessageIfNeeded:(ARTMessage *__art_nonnull)message;
- (void)internalPostMessages:(id)data callback:(art_nullable ARTErrorCallback)callback;

@end
Expand Down
12 changes: 5 additions & 7 deletions ably-ios/ARTChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

#import <Foundation/Foundation.h>
#import "ARTTypes.h"

@protocol ARTPayloadEncoder;
#import "ARTDataEncoder.h"

@class ARTChannelOptions;
@class ARTMessage;
Expand All @@ -21,13 +20,12 @@ ART_ASSUME_NONNULL_BEGIN
@interface ARTChannel : NSObject

@property (nonatomic, strong, readonly) NSString *name;
@property (readonly, getter=getLogger) ARTLog *logger;

@property (readonly, strong, nonatomic) id<ARTPayloadEncoder> payloadEncoder;

- (instancetype)initWithName:(NSString *)name andOptions:(ARTChannelOptions *)options;
- (instancetype)initWithName:(NSString *)name andOptions:(ARTChannelOptions *)options andLogger:(ARTLog *)logger;

- (void)publish:(art_nullable id)payload callback:(art_nullable ARTErrorCallback)callback;
- (void)publish:(art_nullable id)payload name:(art_nullable NSString *)name callback:(art_nullable ARTErrorCallback)callback;
- (void)publish:(art_nullable id)data callback:(art_nullable ARTErrorCallback)callback;
- (void)publish:(art_nullable id)data name:(art_nullable NSString *)name callback:(art_nullable ARTErrorCallback)callback;

- (void)publishMessage:(ARTMessage *)message callback:(art_nullable ARTErrorCallback)callback;
- (void)publishMessages:(__GENERIC(NSArray, ARTMessage *) *)messages callback:(art_nullable ARTErrorCallback)callback;
Expand Down
31 changes: 20 additions & 11 deletions ably-ios/ARTChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@

#import "ARTChannel+Private.h"

#import "ARTPayload.h"
#import "ARTDataEncoder.h"
#import "ARTMessage.h"
#import "ARTChannelOptions.h"
#import "ARTNSArray+ARTFunctional.h"

@implementation ARTChannel

- (instancetype)initWithName:(NSString *)name andOptions:(ARTChannelOptions *)options {
- (instancetype)initWithName:(NSString *)name andOptions:(ARTChannelOptions *)options andLogger:(ARTLog *)logger {
if (self = [super init]) {
_name = name;
_options = options;
_payloadEncoder = [ARTPayload defaultPayloadEncoder:options.cipherParams];
self.options = options;
_dataEncoder = [[ARTDataEncoder alloc] initWithCipherParams:_options.cipherParams logger:logger];
_logger = logger;
}
return self;
}
Expand All @@ -30,8 +31,6 @@ - (void)setOptions:(ARTChannelOptions *)options {
} else {
_options = options;
}
// FIXME: odd, always JSON?!
_payloadEncoder = [ARTJsonPayloadEncoder instance];
}

- (void)publish:(id)data callback:(ARTErrorCallback)callback {
Expand All @@ -43,15 +42,25 @@ - (void)publish:(id)data name:(NSString *)name callback:(ARTErrorCallback)callba
}

- (void)publishMessages:(NSArray *)messages callback:(ARTErrorCallback)callback {
messages = [messages artMap:^(ARTMessage *message) {
return [message encode:self.payloadEncoder];
}];
[self internalPostMessages:[messages artMap:^id(ARTMessage *message) {
return [self encodeMessageIfNeeded:message];
}] callback:callback];
}

[self internalPostMessages:messages callback:callback];
- (ARTMessage *)encodeMessageIfNeeded:(ARTMessage *)message {
if (!self.dataEncoder) {
return message;
}
ARTStatus *status = [message encodeWithEncoder:self.dataEncoder output:&message];
if (status.state != ARTStateOk) {
[self.logger error:@"ARTChannel: error encoding data, status: %tu", status];
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"message encoding failed" userInfo:nil];
}
return message;
}

- (void)publishMessage:(ARTMessage *)message callback:(ARTErrorCallback)callback {
[self internalPostMessages:[message encode:self.payloadEncoder] callback:callback];
[self internalPostMessages:[self encodeMessageIfNeeded:message] callback:callback];
}

- (BOOL)history:(ARTDataQuery *)query callback:(void (^)(__GENERIC(ARTPaginatedResult, ARTMessage *) *, NSError *))callback error:(NSError **)errorPtr {
Expand Down
44 changes: 44 additions & 0 deletions ably-ios/ARTDataEncoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// ARTDataEncoder.h
// ably-ios
//
// Created by Jason Choy on 18/12/2014.
// Copyright (c) 2014 Ably. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "CompatibilityMacros.h"
#import "ARTStatus.h"
#import "ARTCrypto.h"

@class ARTCipherParams;

ART_ASSUME_NONNULL_BEGIN

@interface ARTDataEncoderOutput : NSObject

@property (readonly, nonatomic, art_nullable) id data;
@property (readonly, nonatomic, art_nullable) NSString *encoding;
@property (readonly, nonatomic) ARTStatus *status;

- initWithData:(id __art_nullable)data encoding:(NSString *__art_nullable)encoding status:(ARTStatus *)status;

@end

@interface ARTDataEncoder : NSObject

- (instancetype)initWithCipherParams:(ARTCipherParams *)params logger:(ARTLog *)logger;
- (ARTDataEncoderOutput *)encode:(id __art_nullable)data;
- (ARTDataEncoderOutput *)decode:(id __art_nullable)data encoding:(NSString *__art_nullable)encoding;

@end

@interface NSString (ARTDataEncoder)

- (NSString *)artAddEncoding:(NSString *)encoding;
- (NSString *)artLastEncoding;
- (NSString *)artRemoveLastEncoding;

@end

ART_ASSUME_NONNULL_END
Loading

0 comments on commit 724d035

Please sign in to comment.