iOS-MAS-Foundation icon indicating copy to clipboard operation
iOS-MAS-Foundation copied to clipboard

MASRequestBuilder only accept dictionary as body

Open sandersaelmans opened this issue 3 years ago • 0 comments

PR: https://github.com/CAAPIM/iOS-MAS-Foundation/pull/291

Currently the MASRequest builder only accepts an NSDictionary as the request body. This is an issue as not all (of our) endpoints expect a dictionary as the root object of a request's body.

Some possible fixes could be to:

  1. use id as the body's type (this change does not existing impact users when they update).
  2. use NSData as the body's type (breaking change and users have to encode the json themselves. Offers flexibility however).
  3. Introduce a JSONObject class as the body's type (JSONObject would then be initializable with all valid json data types: NSNumber, NSString, Bool, NSDictionary, NSArray, etc. This limits the amount of "valid" types a user could use to set the request body):
@interface MASJsonObject: NSObject

@property (nonatomic, strong) id value;

- (instancetype)init NS_UNAVAILABLE;

- (instancetype)initWithString:(NSString *)string;
- (instancetype)initWithBool:(BOOL)boolean;
- (instancetype)initWithNumber:(NSNumber *)number;
- (instancetype)initWithDictionary:(NSDictionary *)dictionary;
- (instancetype)initWithArray:(NSArray *)array;

@end


@implementation MASJsonObject

- (instancetype)initWithString:(NSString *)string {
   self = [super init];
   if (self)
   {
       self.value = string;
   }
   return self;
}

// etc...

@end

sandersaelmans avatar Jun 30 '21 07:06 sandersaelmans