Skip to content

Commit

Permalink
Extract logic to compute REST path with version
Browse files Browse the repository at this point in the history
  • Loading branch information
mokagio committed Apr 12, 2024
1 parent 281cf4b commit d1c738c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
8 changes: 8 additions & 0 deletions Sources/BasicBlogAPIObjc/ServiceRemoteWordPressComREST.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,12 @@ NS_SWIFT_NAME(path(forEndpoint:withVersion:));

@end

@interface WordPressComRESTVersionedPathBuilder: NSObject

+ (NSString *)pathForEndpoint:(NSString *)endpoint
withVersion:(WordPressComRESTAPIVersion)apiVersion
NS_SWIFT_NAME(path(forEndpoint:withVersion:));

@end

NS_ASSUME_NONNULL_END
56 changes: 31 additions & 25 deletions Sources/BasicBlogAPIObjc/ServiceRemoteWordPressComREST.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
#import "ServiceRemoteWordPressComREST.h"
#import "WPKit-Swift.h"

static NSString* const ServiceRemoteWordPressComRESTApiVersionStringInvalid = @"invalid_api_version";
static NSString* const ServiceRemoteWordPressComRESTApiVersionString_1_0 = @"rest/v1";
static NSString* const ServiceRemoteWordPressComRESTApiVersionString_1_1 = @"rest/v1.1";
static NSString* const ServiceRemoteWordPressComRESTApiVersionString_1_2 = @"rest/v1.2";
static NSString* const ServiceRemoteWordPressComRESTApiVersionString_1_3 = @"rest/v1.3";
static NSString* const ServiceRemoteWordPressComRESTApiVersionString_2_0 = @"wpcom/v2";

@implementation ServiceRemoteWordPressComREST

- (instancetype)initWithWordPressComRestApi:(WordPressComRestApi *)wordPressComRestApi {
Expand All @@ -22,13 +15,39 @@ - (instancetype)initWithWordPressComRestApi:(WordPressComRestApi *)wordPressComR
return self;
}

#pragma mark - Request URL construction

- (NSString *)pathForEndpoint:(NSString *)resourceUrl
withVersion:(WordPressComRESTAPIVersion)apiVersion
{
NSParameterAssert([resourceUrl isKindOfClass:[NSString class]]);

return [WordPressComRESTVersionedPathBuilder pathForEndpoint:resourceUrl
withVersion:apiVersion];
}

@end

static NSString* const ServiceRemoteWordPressComRESTApiVersionStringInvalid = @"invalid_api_version";
static NSString* const ServiceRemoteWordPressComRESTApiVersionString_1_0 = @"rest/v1";
static NSString* const ServiceRemoteWordPressComRESTApiVersionString_1_1 = @"rest/v1.1";
static NSString* const ServiceRemoteWordPressComRESTApiVersionString_1_2 = @"rest/v1.2";
static NSString* const ServiceRemoteWordPressComRESTApiVersionString_1_3 = @"rest/v1.3";
static NSString* const ServiceRemoteWordPressComRESTApiVersionString_2_0 = @"wpcom/v2";

@implementation WordPressComRESTVersionedPathBuilder

#pragma mark - API Version
+ (NSString *)pathForEndpoint:(NSString *)endpoint withVersion:(WordPressComRESTAPIVersion)apiVersion
{
NSString *apiVersionString = [self apiVersionStringWithEnumValue:apiVersion];

- (NSString *)apiVersionStringWithEnumValue:(WordPressComRESTAPIVersion)apiVersion
return [NSString stringWithFormat:@"%@/%@", apiVersionString, endpoint];
}

+ (NSString *)apiVersionStringWithEnumValue:(WordPressComRESTAPIVersion)apiVersion
{
NSString *result = nil;

switch (apiVersion) {
case WordPressComRESTAPIVersion_1_0:
result = ServiceRemoteWordPressComRESTApiVersionString_1_0;
Expand All @@ -37,7 +56,7 @@ - (NSString *)apiVersionStringWithEnumValue:(WordPressComRESTAPIVersion)apiVersi
case WordPressComRESTAPIVersion_1_1:
result = ServiceRemoteWordPressComRESTApiVersionString_1_1;
break;

case WordPressComRESTAPIVersion_1_2:
result = ServiceRemoteWordPressComRESTApiVersionString_1_2;
break;
Expand All @@ -55,20 +74,7 @@ - (NSString *)apiVersionStringWithEnumValue:(WordPressComRESTAPIVersion)apiVersi
result = ServiceRemoteWordPressComRESTApiVersionStringInvalid;
break;
}

return result;
}

#pragma mark - Request URL construction

- (NSString *)pathForEndpoint:(NSString *)resourceUrl
withVersion:(WordPressComRESTAPIVersion)apiVersion
{
NSParameterAssert([resourceUrl isKindOfClass:[NSString class]]);

NSString *apiVersionString = [self apiVersionStringWithEnumValue:apiVersion];

return [NSString stringWithFormat:@"%@/%@", apiVersionString, resourceUrl];
return result;
}

@end

0 comments on commit d1c738c

Please sign in to comment.