Skip to content

Commit ca14529

Browse files
committed
Updated ApiClient of objc sdk.
When create api instance, if we don't pass the `apiClient` parameter, then get api client instance from the pool.
1 parent 095771d commit ca14529

File tree

15 files changed

+186
-129
lines changed

15 files changed

+186
-129
lines changed

modules/swagger-codegen/src/main/resources/objc/SWGApiClient.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,12 @@ +(unsigned long)requestQueueSize {
134134
}
135135

136136
+(NSNumber*) nextRequestId {
137-
long nextId = ++requestId;
138-
if(loggingEnabled)
139-
NSLog(@"got id %ld", nextId);
140-
return [NSNumber numberWithLong:nextId];
137+
@synchronized(self) {
138+
long nextId = ++requestId;
139+
if(loggingEnabled)
140+
NSLog(@"got id %ld", nextId);
141+
return [NSNumber numberWithLong:nextId];
142+
}
141143
}
142144

143145
+(NSNumber*) queueRequest {

modules/swagger-codegen/src/main/resources/objc/api-body.mustache

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#import "{{classname}}.h"
33
#import "SWGFile.h"
44
#import "SWGQueryParamCollection.h"
5-
#import "SWGApiClient.h"
65
{{#imports}}#import "{{import}}.h"
76
{{/imports}}
87
{{newline}}
@@ -12,8 +11,34 @@
1211
@end
1312

1413
@implementation {{classname}}
14+
1515
static NSString * basePath = @"{{basePath}}";
1616

17+
#pragma mark - Initialize methods
18+
19+
- (id) init {
20+
self = [super init];
21+
if (self) {
22+
self.apiClient = [SWGApiClient sharedClientFromPool:basePath];
23+
self.defaultHeaders = [NSMutableDictionary dictionary];
24+
}
25+
return self;
26+
}
27+
28+
- (id) initWithApiClient:(SWGApiClient *)apiClient {
29+
self = [super init];
30+
if (self) {
31+
if (apiClient) {
32+
self.apiClient = apiClient;
33+
}
34+
else {
35+
self.apiClient = [SWGApiClient sharedClientFromPool:basePath];
36+
}
37+
self.defaultHeaders = [NSMutableDictionary dictionary];
38+
}
39+
return self;
40+
}
41+
1742
+({{classname}}*) apiWithHeader:(NSString*)headerValue key:(NSString*)key {
1843
static {{classname}}* singletonAPI = nil;
1944

@@ -32,21 +57,10 @@ static NSString * basePath = @"{{basePath}}";
3257
return basePath;
3358
}
3459

35-
-(SWGApiClient*) apiClient {
36-
return [SWGApiClient sharedClientFromPool:basePath];
37-
}
38-
3960
-(void) addHeader:(NSString*)value forKey:(NSString*)key {
4061
[self.defaultHeaders setValue:value forKey:key];
4162
}
4263

43-
-(id) init {
44-
self = [super init];
45-
self.defaultHeaders = [NSMutableDictionary dictionary];
46-
[self apiClient];
47-
return self;
48-
}
49-
5064
-(void) setHeaderValue:(NSString*) value
5165
forKey:(NSString*)key {
5266
[self.defaultHeaders setValue:value forKey:key];
@@ -177,8 +191,6 @@ static NSString * basePath = @"{{basePath}}";
177191
{{/requiredParams}}
178192
{{/requiredParamCount}}
179193

180-
SWGApiClient* client = [SWGApiClient sharedClientFromPool:basePath];
181-
182194
{{#returnContainer}}
183195
// response is in a container
184196
{{>apiBodyResponseWithContainer}}{{/returnContainer}}
@@ -206,3 +218,6 @@ static NSString * basePath = @"{{basePath}}";
206218
{{newline}}
207219
{{/operations}}
208220
@end
221+
222+
223+

modules/swagger-codegen/src/main/resources/objc/api-header.mustache

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
{{#imports}}#import "{{import}}.h"
33
{{/imports}}
44
#import "SWGObject.h"
5+
#import "SWGApiClient.h"
56
{{newline}}
67

78
{{#operations}}
89
@interface {{classname}}: NSObject
910

11+
@property(nonatomic, assign)SWGApiClient *apiClient;
12+
1013
-(void) addHeader:(NSString*)value forKey:(NSString*)key;
1114
-(unsigned long) requestQueueSize;
1215
+({{classname}}*) apiWithHeader:(NSString*)headerValue key:(NSString*)key;
@@ -34,4 +37,4 @@
3437
{{/operation}}
3538

3639
{{/operations}}
37-
@end
40+
@end

modules/swagger-codegen/src/main/resources/objc/apiBodyResponseWithContainer.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// {{returnContainer}} container response type
2-
return [client dictionary: requestUrl
2+
return [self.apiClient dictionary: requestUrl
33
method: @"{{httpMethod}}"
44
queryParams: queryParams
55
body: bodyDictionary
@@ -34,4 +34,4 @@
3434
{{/returnBaseType}}
3535
{{/isListContainer}}
3636
}];
37-
37+

modules/swagger-codegen/src/main/resources/objc/apiNonPrimitiveResponse.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{^returnTypeIsPrimitive}}
22
// comples response type
3-
return [client dictionary: requestUrl
3+
return [self.apiClient dictionary: requestUrl
44
method: @"{{httpMethod}}"
55
queryParams: queryParams
66
body: bodyDictionary

modules/swagger-codegen/src/main/resources/objc/apiPrimitiveResponse.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// primitive response type
2-
{{#returnBaseType}}return [client stringWithCompletionBlock: requestUrl
2+
{{#returnBaseType}}return [self.apiClient stringWithCompletionBlock: requestUrl
33
method: @"{{httpMethod}}"
44
queryParams: queryParams
55
body: bodyDictionary
@@ -17,7 +17,7 @@
1717
{{/returnBaseType}}
1818
{{^returnBaseType}}
1919
// no return base type
20-
return [client stringWithCompletionBlock: requestUrl
20+
return [self.apiClient stringWithCompletionBlock: requestUrl
2121
method: @"{{httpMethod}}"
2222
queryParams: queryParams
2323
body: bodyDictionary
@@ -32,4 +32,4 @@
3232
completionBlock(nil);
3333
}];
3434
{{/returnBaseType}}
35-
35+

modules/swagger-codegen/src/main/resources/objc/voidResponse.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
return [client stringWithCompletionBlock: requestUrl
1+
return [self.apiClient stringWithCompletionBlock: requestUrl
22
method: @"{{httpMethod}}"
33
queryParams: queryParams
44
body: bodyDictionary

samples/client/petstore/objc/PetstoreClient/PetstoreClientTests/PetApiTest.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ - (void)tearDown {
1212
[super tearDown];
1313
}
1414

15+
- (void)testCreatePetApi {
16+
SWGPetApi *api1 = [[SWGPetApi alloc] init];
17+
SWGPetApi *api2 = [[SWGPetApi alloc] init];
18+
XCTAssertEqual(api1.apiClient, api2.apiClient);
19+
20+
SWGApiClient *client = [[SWGApiClient alloc] init];
21+
SWGPetApi *api3 = [[SWGPetApi alloc] initWithApiClient:client];
22+
XCTAssertNotEqual(api1.apiClient, api3.apiClient);
23+
}
24+
1525
- (void)testCreateAndGetPet {
1626
XCTestExpectation *expectation = [self expectationWithDescription:@"testGetPetById"];
1727
SWGPet* pet = [self createPet];

samples/client/petstore/objc/client/SWGApiClient.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,12 @@ +(unsigned long)requestQueueSize {
134134
}
135135

136136
+(NSNumber*) nextRequestId {
137-
long nextId = ++requestId;
138-
if(loggingEnabled)
139-
NSLog(@"got id %ld", nextId);
140-
return [NSNumber numberWithLong:nextId];
137+
@synchronized(self) {
138+
long nextId = ++requestId;
139+
if(loggingEnabled)
140+
NSLog(@"got id %ld", nextId);
141+
return [NSNumber numberWithLong:nextId];
142+
}
141143
}
142144

143145
+(NSNumber*) queueRequest {

samples/client/petstore/objc/client/SWGPetApi.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
#import "SWGPet.h"
33
#import "SWGFile.h"
44
#import "SWGObject.h"
5+
#import "SWGApiClient.h"
56

67

78
@interface SWGPetApi: NSObject
89

10+
@property(nonatomic, assign)SWGApiClient *apiClient;
11+
12+
-(instancetype) initWithApiClient:(SWGApiClient *)apiClient;
913
-(void) addHeader:(NSString*)value forKey:(NSString*)key;
1014
-(unsigned long) requestQueueSize;
1115
+(SWGPetApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key;
@@ -150,4 +154,4 @@
150154

151155

152156

153-
@end
157+
@end

0 commit comments

Comments
 (0)