Skip to content

Commit

Permalink
Merge branch 'unauthenticated-requests'
Browse files Browse the repository at this point in the history
  • Loading branch information
bjsvedin committed Aug 26, 2023
2 parents 9e0ae9c + 2e260b7 commit 65f506c
Show file tree
Hide file tree
Showing 22 changed files with 2,094 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LiveCompleteModelApi<Model : HasId<UUID>>(
root: String,
multiplexSocketUrl: String,
path: String,
token: String,
token: String?,
headers: Map<String, String> = mapOf(),
): LiveCompleteModelApi<Model> = LiveCompleteModelApi(
read = LiveReadModelApi.create(root, path, token, headers),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class LiveFullReadModelApi<Model : HasId<UUID>>(
root: String,
multiplexSocketUrl: String,
path: String,
token: String,
token: String?,
headers: Map<String, String> = mapOf(),
): LiveFullReadModelApi<Model> = LiveFullReadModelApi(
read = LiveReadModelApi.create(root, path, token, headers),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class LiveObserveModelApi<Model : HasId<UUID>>(
companion object {
inline fun <reified Model : HasId<UUID>> create(
multiplexUrl: String,
token: String,
token: String?,
headers: Map<String, String>,
path: String
): LiveObserveModelApi<Model> = LiveObserveModelApi(
openSocket = { query ->
multiplexedSocket<ListChange<Model>, Query<Model>>(
"$multiplexUrl?jwt=$token",
if (token != null) "$multiplexUrl?jwt=$token" else multiplexUrl,
path
)
.switchMap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.util.*

class LiveReadModelApi<Model : HasId<UUID>>(
val url: String,
token: String,
token: String?,
headers: Map<String, String> = mapOf(),
val serializer: KSerializer<Model>,
val querySerializer: KSerializer<Query<Model>>,
Expand All @@ -29,13 +29,13 @@ class LiveReadModelApi<Model : HasId<UUID>>(
inline fun <reified Model : HasId<UUID>> create(
root: String,
path: String,
token: String,
token: String?,
headers: Map<String, String> = mapOf(),
): LiveReadModelApi<Model> =
LiveReadModelApi("$root$path", token, headers, defaultJsonMapper.serializersModule.serializer(), defaultJsonMapper.serializersModule.serializer())
}

private val authHeaders = headers + mapOf("Authorization" to "Bearer $token")
private val authHeaders = token?.let { headers + mapOf("Authorization" to "Bearer $it") } ?: headers

override fun list(query: Query<Model>): Single<List<Model>> = HttpClient.call(
url = "$url/query",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import java.util.*

class LiveWriteModelApi<Model : HasId<UUID>>(
val url: String,
token: String,
token: String?,
headers: Map<String, String>,
val serializer: KSerializer<Model>
) : WriteModelApi<Model>() {
Expand All @@ -24,13 +24,13 @@ class LiveWriteModelApi<Model : HasId<UUID>>(
inline fun <reified Model : HasId<UUID>> create(
root: String,
path: String,
token: String,
token: String?,
headers: Map<String, String> = mapOf(),
): LiveWriteModelApi<Model> =
LiveWriteModelApi("$root$path", token, headers, defaultJsonMapper.serializersModule.serializer())
}

private val authHeaders = headers + mapOf("Authorization" to "Bearer $token")
private val authHeaders = token?.let { headers + mapOf("Authorization" to "Bearer $it") } ?: headers

override fun post(value: Model): Single<Model> = HttpClient.call(
url = url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class LiveCompleteModelApiCompanion {
}
public static let INSTANCE = LiveCompleteModelApiCompanion()

public func create<Model : HasId>(root: String, multiplexSocketUrl: String, path: String, token: String, headers: Dictionary<String, String> = dictionaryOf()) -> LiveCompleteModelApi<Model> {
public func create<Model : HasId>(root: String, multiplexSocketUrl: String, path: String, token: String?, headers: Dictionary<String, String> = dictionaryOf()) -> LiveCompleteModelApi<Model> {
return LiveCompleteModelApi<Model>(read: LiveReadModelApiCompanion.INSTANCE.create(root: root, path: path, token: token, headers: headers), write: LiveWriteModelApiCompanion.INSTANCE.create(root: root, path: path, token: token, headers: headers), observe: LiveObserveModelApiCompanion.INSTANCE.create(multiplexUrl: multiplexSocketUrl, token: token, headers: headers, path: path));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public final class LiveFullReadModelApiCompanion {
}
public static let INSTANCE = LiveFullReadModelApiCompanion()

public func create<Model : HasId>(root: String, multiplexSocketUrl: String, path: String, token: String, headers: Dictionary<String, String> = dictionaryOf()) -> LiveFullReadModelApi<Model> {
public func create<Model : HasId>(root: String, multiplexSocketUrl: String, path: String, token: String?, headers: Dictionary<String, String> = dictionaryOf()) -> LiveFullReadModelApi<Model> {
return LiveFullReadModelApi<Model>(read: LiveReadModelApiCompanion.INSTANCE.create(root: root, path: path, token: token, headers: headers), observe: LiveObserveModelApiCompanion.INSTANCE.create(multiplexUrl: multiplexSocketUrl, token: token, headers: headers, path: path));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public final class LiveObserveModelApiCompanion {
}
public static let INSTANCE = LiveObserveModelApiCompanion()

public func create<Model : HasId>(multiplexUrl: String, token: String, headers: Dictionary<String, String>, path: String) -> LiveObserveModelApi<Model> {
return LiveObserveModelApi<Model>(openSocket: { (query) -> Observable<Array<Model>> in xObservableToListObservable((multiplexedSocket(url: "\(String(kotlin: multiplexUrl))?jwt=\(String(kotlin: token))", path: path) as Observable<WebSocketIsh<ListChange<Model>, Query<Model>>>)
public func create<Model : HasId>(multiplexUrl: String, token: String?, headers: Dictionary<String, String>, path: String) -> LiveObserveModelApi<Model> {
return LiveObserveModelApi<Model>(openSocket: { (query) -> Observable<Array<Model>> in xObservableToListObservable((multiplexedSocket(url: token != nil ? "\(String(kotlin: multiplexUrl))?jwt=\(String(kotlin: token))" : multiplexUrl, path: path) as Observable<WebSocketIsh<ListChange<Model>, Query<Model>>>)
.switchMap { (it) -> Observable<ListChange<Model>> in
it.send(query)
return it.messages.catchError({ (it) -> Observable<ListChange<Model>> in Observable.never() })
Expand Down
12 changes: 8 additions & 4 deletions ios/KtorBatteries/Classes/client/live/LiveReadModelApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import Foundation
public final class LiveReadModelApi<Model : HasId> : ReadModelApi<Model> {
public var url: String
public var serializer: Model.Type
public init(url: String, token: String, headers: Dictionary<String, String> = dictionaryOf(), serializer: Model.Type) {
public var querySerializer: Query<Model>.Type
public init(url: String, token: String?, headers: Dictionary<String, String> = dictionaryOf(), serializer: Model.Type, querySerializer: Query<Model>.Type) {
self.url = url
self.serializer = serializer
self.authHeaders = headers.plus(dictionaryOf(Pair("Authorization", "Bearer \(String(kotlin: token))")))
self.querySerializer = querySerializer
self.authHeaders = (token).map { (it) in
return headers.plus(dictionaryOf(Pair("Authorization", "Bearer \(String(kotlin: it))")))
} ?? headers
super.init()
//Necessary properties should be initialized now
}
Expand Down Expand Up @@ -40,7 +44,7 @@ public final class LiveReadModelApiCompanion {
}
public static let INSTANCE = LiveReadModelApiCompanion()

public func create<Model : HasId>(root: String, path: String, token: String, headers: Dictionary<String, String> = dictionaryOf()) -> LiveReadModelApi<Model> {
return LiveReadModelApi<Model>(url: "\(String(kotlin: root))\(String(kotlin: path))", token: token, headers: headers, serializer: Model.self);
public func create<Model : HasId>(root: String, path: String, token: String?, headers: Dictionary<String, String> = dictionaryOf()) -> LiveReadModelApi<Model> {
return LiveReadModelApi<Model>(url: "\(String(kotlin: root))\(String(kotlin: path))", token: token, headers: headers, serializer: Model.self, querySerializer: Query<Model>.self);
}
}
8 changes: 5 additions & 3 deletions ios/KtorBatteries/Classes/client/live/LiveWriteModelApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import Foundation
public final class LiveWriteModelApi<Model : HasId> : WriteModelApi<Model> {
public var url: String
public var serializer: Model.Type
public init(url: String, token: String, headers: Dictionary<String, String>, serializer: Model.Type) {
public init(url: String, token: String?, headers: Dictionary<String, String>, serializer: Model.Type) {
self.url = url
self.serializer = serializer
self.authHeaders = headers.plus(dictionaryOf(Pair("Authorization", "Bearer \(String(kotlin: token))")))
self.authHeaders = (token).map { (it) in
return headers.plus(dictionaryOf(Pair("Authorization", "Bearer \(String(kotlin: it))")))
} ?? headers
super.init()
//Necessary properties should be initialized now
}
Expand Down Expand Up @@ -65,7 +67,7 @@ public final class LiveWriteModelApiCompanion {
}
public static let INSTANCE = LiveWriteModelApiCompanion()

public func create<Model : HasId>(root: String, path: String, token: String, headers: Dictionary<String, String> = dictionaryOf()) -> LiveWriteModelApi<Model> {
public func create<Model : HasId>(root: String, path: String, token: String?, headers: Dictionary<String, String> = dictionaryOf()) -> LiveWriteModelApi<Model> {
return LiveWriteModelApi<Model>(url: "\(String(kotlin: root))\(String(kotlin: path))", token: token, headers: headers, serializer: Model.self);
}
}
12 changes: 6 additions & 6 deletions ios/KtorBatteries/Classes/client/mock/MockTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ public final class MockTable<Model : HasId> where Model.ID == UUID {
}

public func addItem(item: Model) -> Model {
var array49 = self.data
array49[item._id] = item
self.data = array49
var array58 = self.data
array58[item._id] = item
self.data = array58
self.signals.onNext(SignalData(item: item, created: true, deleted: false))
return item
}

public func replaceItem(item: Model) -> Model {
var array53 = self.data
array53[item._id] = item
self.data = array53
var array62 = self.data
array62[item._id] = item
self.data = array62
self.signals.onNext(SignalData(item: item, created: false, deleted: false))
return item
}
Expand Down
Loading

0 comments on commit 65f506c

Please sign in to comment.