Skip to content

Commit 2cccc89

Browse files
committed
Add IHasSession/Token/Version client support + missing interfaces
1 parent 928e664 commit 2cccc89

3 files changed

Lines changed: 220 additions & 1 deletion

File tree

ServiceStack.xcodeproj/project.pbxproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
4B16EFE524F93A7F0064D8FD /* AutoCrud.dtos.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B16EFE424F93A7F0064D8FD /* AutoCrud.dtos.swift */; };
11+
4B16EFE624F93A7F0064D8FD /* AutoCrud.dtos.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B16EFE424F93A7F0064D8FD /* AutoCrud.dtos.swift */; };
12+
4B16EFE724F93A7F0064D8FD /* AutoCrud.dtos.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B16EFE424F93A7F0064D8FD /* AutoCrud.dtos.swift */; };
13+
4B16EFE824F93A7F0064D8FD /* AutoCrud.dtos.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B16EFE424F93A7F0064D8FD /* AutoCrud.dtos.swift */; };
1014
52D6D9871BEFF229002C0205 /* ServiceStack.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52D6D97C1BEFF229002C0205 /* ServiceStack.framework */; };
1115
B449E763209C8FA600341288 /* JsonServiceClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = B449E75C209C8FA500341288 /* JsonServiceClient.swift */; };
1216
B449E764209C8FA600341288 /* JsonServiceClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = B449E75C209C8FA500341288 /* JsonServiceClient.swift */; };
@@ -117,6 +121,7 @@
117121
/* End PBXContainerItemProxy section */
118122

119123
/* Begin PBXFileReference section */
124+
4B16EFE424F93A7F0064D8FD /* AutoCrud.dtos.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AutoCrud.dtos.swift; sourceTree = "<group>"; };
120125
52D6D97C1BEFF229002C0205 /* ServiceStack.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ServiceStack.framework; sourceTree = BUILT_PRODUCTS_DIR; };
121126
52D6D9861BEFF229002C0205 /* ServiceStack-iOS Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "ServiceStack-iOS Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
122127
52D6D9E21BEFFF6E002C0205 /* ServiceStack.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ServiceStack.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -272,6 +277,7 @@
272277
8933C7831EB5B7EB000D00A4 /* Tests */ = {
273278
isa = PBXGroup;
274279
children = (
280+
4B16EFE424F93A7F0064D8FD /* AutoCrud.dtos.swift */,
275281
B449E780209C904500341288 /* AutoQueryTests.swift */,
276282
B449E786209C904600341288 /* DateExtensionsTests.swift */,
277283
B449E782209C904500341288 /* JsonServiceClientTests.swift */,
@@ -865,6 +871,7 @@
865871
B449E777209C8FA600341288 /* DateExtensions.swift in Sources */,
866872
B449E763209C8FA600341288 /* JsonServiceClient.swift in Sources */,
867873
B449E767209C8FA600341288 /* Explore.swift in Sources */,
874+
4B16EFE524F93A7F0064D8FD /* AutoCrud.dtos.swift in Sources */,
868875
B449E76F209C8FA600341288 /* DynamicJson.swift in Sources */,
869876
);
870877
runOnlyForDeploymentPostprocessing = 0;
@@ -886,6 +893,7 @@
886893
B449E7AD209C904700341288 /* Template.swift in Sources */,
887894
B449E7A4209C904700341288 /* DateExtensionsTests.swift in Sources */,
888895
B449E792209C904700341288 /* AutoQueryTests.swift in Sources */,
896+
4B16EFE624F93A7F0064D8FD /* AutoCrud.dtos.swift in Sources */,
889897
B449E7B0209C904700341288 /* TechStacksJsonTests.swift in Sources */,
890898
B449E79B209C904700341288 /* JsonTests.swift in Sources */,
891899
);
@@ -947,6 +955,7 @@
947955
B449E7DA209C9DC300341288 /* AutoQueryTests.swift in Sources */,
948956
B449E7AE209C904700341288 /* Template.swift in Sources */,
949957
B449E7A5209C904700341288 /* DateExtensionsTests.swift in Sources */,
958+
4B16EFE724F93A7F0064D8FD /* AutoCrud.dtos.swift in Sources */,
950959
B449E7B1209C904700341288 /* TechStacksJsonTests.swift in Sources */,
951960
B449E79C209C904700341288 /* JsonTests.swift in Sources */,
952961
);
@@ -969,6 +978,7 @@
969978
B449E7AF209C904700341288 /* Template.swift in Sources */,
970979
B449E7A6209C904700341288 /* DateExtensionsTests.swift in Sources */,
971980
B449E794209C904700341288 /* AutoQueryTests.swift in Sources */,
981+
4B16EFE824F93A7F0064D8FD /* AutoCrud.dtos.swift in Sources */,
972982
B449E7B2209C904700341288 /* TechStacksJsonTests.swift in Sources */,
973983
B449E79D209C904700341288 /* JsonTests.swift in Sources */,
974984
);

Sources/JsonServiceClient.swift

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ public protocol IPut {}
2121
public protocol IDelete {}
2222
public protocol IPatch {}
2323

24+
public protocol IHasSessionId {
25+
var sessionId: String? { get set }
26+
}
27+
public protocol IHasBearerToken {
28+
var bearerToken: String? { get set }
29+
}
30+
public protocol IMeta {
31+
var meta: [String: String] { get set }
32+
}
33+
public protocol IHasVersion {
34+
var version: Int? { get set }
35+
}
36+
2437
public protocol ServiceClient {
2538
func get<T: IReturn>(_ request: T) throws -> T.Return where T: JsonSerializable
2639
func get<T: IReturnVoid>(_ request: T) throws -> Void where T: JsonSerializable
@@ -70,7 +83,7 @@ public protocol ServiceClient {
7083
func getDataAsync(_ url: String) -> Promise<Data>
7184
}
7285

73-
open class JsonServiceClient: ServiceClient {
86+
open class JsonServiceClient: ServiceClient, IHasBearerToken, IHasSessionId, IHasVersion {
7487
open var baseUrl: String
7588
open var replyUrl: String
7689
open var domain: String
@@ -83,6 +96,10 @@ open class JsonServiceClient: ServiceClient {
8396
open var requestFilter: ((NSMutableURLRequest) -> Void)?
8497
open var responseFilter: ((URLResponse) -> Void)?
8598

99+
open var bearerToken: String?
100+
open var sessionId: String?
101+
open var version: Int?
102+
86103
public struct Global {
87104
static var requestFilter: ((NSMutableURLRequest) -> Void)?
88105
static var responseFilter: ((URLResponse) -> Void)?
@@ -188,6 +205,8 @@ open class JsonServiceClient: ServiceClient {
188205
open func createUrl<T: JsonSerializable>(dto: T, query: [String: String] = [:]) -> String {
189206
var requestUrl = replyUrl + T.typeName
190207

208+
populateRequestDto(dto)
209+
191210
var sb = ""
192211
for pi in T.properties {
193212
if let strValue = pi.jsonValueAny(instance: dto)?.stripQuotes() {
@@ -209,11 +228,30 @@ open class JsonServiceClient: ServiceClient {
209228
return requestUrl
210229
}
211230

231+
func populateRequestDto<T: JsonSerializable>(_ request: T) {
232+
if let token = self.bearerToken, var hasToken = request as? IHasBearerToken {
233+
if hasToken.bearerToken == nil {
234+
hasToken.bearerToken = token
235+
}
236+
}
237+
if let session = self.sessionId, var hasSession = request as? IHasSessionId {
238+
if hasSession.sessionId == nil {
239+
hasSession.sessionId = session
240+
}
241+
}
242+
if let version = self.version, var hasVersion = request as? IHasVersion {
243+
if hasVersion.version == nil {
244+
hasVersion.version = version
245+
}
246+
}
247+
}
248+
212249
open func createRequestDto<T: JsonSerializable>(url: String, httpMethod: String, request: T?) -> NSMutableURLRequest {
213250
var contentType: String?
214251
var requestBody: Data?
215252

216253
if let dto = request {
254+
populateRequestDto(dto)
217255
contentType = "application/json"
218256
requestBody = dto.toJson().data(using: String.Encoding.utf8)
219257
}

Tests/AutoCrud.dtos.swift

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/* Options:
2+
Date: 2020-08-29 00:20:08
3+
SwiftVersion: 4.0
4+
Version: 5.93
5+
Tip: To override a DTO option, remove "//" prefix before updating
6+
BaseUrl: http://localhost:5000
7+
8+
//BaseClass:
9+
//AddModelExtensions: True
10+
//AddServiceStackTypes: True
11+
//IncludeTypes:
12+
//ExcludeTypes:
13+
//ExcludeGenericBaseTypes: False
14+
//AddResponseStatus: False
15+
//AddImplicitVersion:
16+
//AddDescriptionAsComments: True
17+
//InitializeCollections: True
18+
//TreatTypesAsStrings:
19+
//DefaultImports: Foundation,ServiceStack
20+
*/
21+
22+
import Foundation
23+
import ServiceStack
24+
25+
public class AllInterfaces : IReturn, IHasVersion, IHasBearerToken, IHasSessionId, IGet
26+
{
27+
public typealias Return = AllInterfaces
28+
29+
required public init(){}
30+
public var version:Int?
31+
public var bearerToken:String?
32+
public var sessionId:String?
33+
public var meta:[String:String] = [:]
34+
}
35+
36+
// @Route("/mytables", "POST")
37+
// @DataContract
38+
public class CreateMyTable : IReturn, IPost
39+
{
40+
public typealias Return = IdResponse
41+
42+
required public init(){}
43+
// @DataMember(Order=2)
44+
public var name:String?
45+
}
46+
47+
// @Route("/mytables/{Id}", "DELETE")
48+
// @DataContract
49+
public class DeleteMyTable : IReturn, IDelete
50+
{
51+
public typealias Return = IdResponse
52+
53+
required public init(){}
54+
// @DataMember(Order=1)
55+
public var id:Int64?
56+
}
57+
58+
// @Route("/mytables/{Id}", "PATCH")
59+
// @DataContract
60+
public class PatchMyTable : IReturn, IPatch
61+
{
62+
public typealias Return = IdResponse
63+
64+
required public init(){}
65+
// @DataMember(Order=1)
66+
public var id:Int64?
67+
68+
// @DataMember(Order=2)
69+
public var name:String?
70+
}
71+
72+
// @Route("/mytables/{Id}", "PUT")
73+
// @DataContract
74+
public class UpdateMyTable : IReturn, IPut
75+
{
76+
public typealias Return = IdResponse
77+
78+
required public init(){}
79+
// @DataMember(Order=1)
80+
public var id:Int64?
81+
82+
// @DataMember(Order=2)
83+
public var name:String?
84+
}
85+
86+
87+
// @DataContract
88+
public class IdResponse
89+
{
90+
required public init(){}
91+
// @DataMember(Order=1)
92+
public var id:String?
93+
94+
// @DataMember(Order=2)
95+
public var responseStatus:ResponseStatus?
96+
}
97+
98+
// @DataContract
99+
public class MyTable
100+
{
101+
required public init(){}
102+
// @DataMember(Order=1)
103+
public var id:Int64?
104+
105+
// @DataMember(Order=2)
106+
public var name:String?
107+
}
108+
109+
extension AllInterfaces : JsonSerializable
110+
{
111+
public static var typeName:String { return "AllInterfaces" }
112+
public static var metadata = Metadata.create([
113+
Type<AllInterfaces>.optionalProperty("version", get: { $0.version }, set: { $0.version = $1 }),
114+
Type<AllInterfaces>.optionalProperty("bearerToken", get: { $0.bearerToken }, set: { $0.bearerToken = $1 }),
115+
Type<AllInterfaces>.optionalProperty("sessionId", get: { $0.sessionId }, set: { $0.sessionId = $1 }),
116+
Type<AllInterfaces>.objectProperty("meta", get: { $0.meta }, set: { $0.meta = $1 }),
117+
])
118+
}
119+
120+
extension CreateMyTable : JsonSerializable
121+
{
122+
public static var typeName:String { return "CreateMyTable" }
123+
public static var metadata = Metadata.create([
124+
Type<CreateMyTable>.optionalProperty("name", get: { $0.name }, set: { $0.name = $1 }),
125+
])
126+
}
127+
128+
extension DeleteMyTable : JsonSerializable
129+
{
130+
public static var typeName:String { return "DeleteMyTable" }
131+
public static var metadata = Metadata.create([
132+
Type<DeleteMyTable>.optionalProperty("id", get: { $0.id }, set: { $0.id = $1 }),
133+
])
134+
}
135+
136+
extension PatchMyTable : JsonSerializable
137+
{
138+
public static var typeName:String { return "PatchMyTable" }
139+
public static var metadata = Metadata.create([
140+
Type<PatchMyTable>.optionalProperty("id", get: { $0.id }, set: { $0.id = $1 }),
141+
Type<PatchMyTable>.optionalProperty("name", get: { $0.name }, set: { $0.name = $1 }),
142+
])
143+
}
144+
145+
extension UpdateMyTable : JsonSerializable
146+
{
147+
public static var typeName:String { return "UpdateMyTable" }
148+
public static var metadata = Metadata.create([
149+
Type<UpdateMyTable>.optionalProperty("id", get: { $0.id }, set: { $0.id = $1 }),
150+
Type<UpdateMyTable>.optionalProperty("name", get: { $0.name }, set: { $0.name = $1 }),
151+
])
152+
}
153+
154+
extension IdResponse : JsonSerializable
155+
{
156+
public static var typeName:String { return "IdResponse" }
157+
public static var metadata = Metadata.create([
158+
Type<IdResponse>.optionalProperty("id", get: { $0.id }, set: { $0.id = $1 }),
159+
Type<IdResponse>.optionalProperty("responseStatus", get: { $0.responseStatus }, set: { $0.responseStatus = $1 }),
160+
])
161+
}
162+
163+
extension MyTable : JsonSerializable
164+
{
165+
public static var typeName:String { return "MyTable" }
166+
public static var metadata = Metadata.create([
167+
Type<MyTable>.optionalProperty("id", get: { $0.id }, set: { $0.id = $1 }),
168+
Type<MyTable>.optionalProperty("name", get: { $0.name }, set: { $0.name = $1 }),
169+
])
170+
}
171+

0 commit comments

Comments
 (0)