Skip to content

Commit

Permalink
Add Client constructor support URL address (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
humdrum authored Apr 19, 2024
1 parent 0aa1f3e commit c957485
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
32 changes: 31 additions & 1 deletion Sources/Core/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public struct ClientOptions {
}
}

public struct RPCAddress {
public struct RPCAddress: Equatable {
public static let tlsPort = 443

let host: String
Expand All @@ -152,6 +152,24 @@ public struct RPCAddress {
}
}

extension URL {
var toRPCAddress: RPCAddress? {
guard let host = self.host else { return nil }

if let port = self.port {
return RPCAddress(host: host, port: port)
} else if let scheme = self.scheme {
if scheme == "https" {
return RPCAddress(host: host, port: RPCAddress.tlsPort)
} else if scheme == "http" {
return RPCAddress(host: host, port: 80)
}
}

return RPCAddress(host: host)
}
}

/**
* `Client` is a normal client that can communicate with the server.
* It has documents and sends changes of the documents in local
Expand Down Expand Up @@ -211,6 +229,18 @@ public actor Client {
self.eventStream = PassthroughSubject()
}

/**
* @param url - the url of the RPC server.
* @param opts - the options of the client.
*/
init?(_ url: URL, _ options: ClientOptions = ClientOptions()) {
guard let rpcAddress = url.toRPCAddress else {
return nil
}

self.init(rpcAddress, options)
}

deinit {
try? self.group.syncShutdownGracefully()
try? self.rpcClient.channel.close().wait()
Expand Down
34 changes: 34 additions & 0 deletions Tests/Unit/Util/URLTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2024 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import XCTest
@testable import Yorkie

final class URLTests: XCTestCase {
func test_url_to_rpc_address() {
var rpc = URL(string: "http://yorkie.dev")?.toRPCAddress
XCTAssertEqual(rpc, RPCAddress(host: "yorkie.dev", port: 80))

rpc = URL(string: "https://yorkie.dev")?.toRPCAddress
XCTAssertEqual(rpc, RPCAddress(host: "yorkie.dev", port: 443))

rpc = URL(string: "https://yorkie.dev:8080")?.toRPCAddress
XCTAssertEqual(rpc, RPCAddress(host: "yorkie.dev", port: 8080))

rpc = URL(string: "yorkie.dev")?.toRPCAddress
XCTAssertEqual(rpc, nil)
}
}
4 changes: 4 additions & 0 deletions Yorkie.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
9A4DC745292DD82500C89478 /* YorkieCountable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A4DC744292DD82500C89478 /* YorkieCountable.swift */; };
9A4DC747292DE2CA00C89478 /* GRPCTypeAlias.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE7B997528E1773A00D56198 /* GRPCTypeAlias.swift */; };
9A5A80D82BD0BD190047B978 /* Attachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A5A80D72BD0BD180047B978 /* Attachment.swift */; };
9A5A80DA2BD238400047B978 /* URLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A5A80D92BD238400047B978 /* URLTests.swift */; };
9A5D9DDC2A710FB40096C40C /* Semaphore in Frameworks */ = {isa = PBXBuildFile; productRef = 9A5D9DDB2A710FB40096C40C /* Semaphore */; };
9A6399A7294C7E1E00BF27F5 /* Logging in Frameworks */ = {isa = PBXBuildFile; productRef = 9A6399A6294C7E1E00BF27F5 /* Logging */; };
9A66B19E295446BF00D10B94 /* RGATreeSplit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A66B19D295446BF00D10B94 /* RGATreeSplit.swift */; };
Expand Down Expand Up @@ -153,6 +154,7 @@
9A4DC744292DD82500C89478 /* YorkieCountable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YorkieCountable.swift; sourceTree = "<group>"; };
9A5A80D52BD0BC8A0047B978 /* DocumentBenchmarkTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentBenchmarkTests.swift; sourceTree = "<group>"; };
9A5A80D72BD0BD180047B978 /* Attachment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Attachment.swift; sourceTree = "<group>"; };
9A5A80D92BD238400047B978 /* URLTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLTests.swift; sourceTree = "<group>"; };
9A66B19D295446BF00D10B94 /* RGATreeSplit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RGATreeSplit.swift; sourceTree = "<group>"; };
9A66B19F2955833E00D10B94 /* LLRBTree.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LLRBTree.swift; sourceTree = "<group>"; };
9A7C75E92A0261DD000B6E5A /* Version.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Version.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -401,6 +403,7 @@
CE3EC94A28D1885F009471BC /* Util */ = {
isa = PBXGroup;
children = (
9A5A80D92BD238400047B978 /* URLTests.swift */,
9A8150512967FCD700DFADFB /* Helper.swift */,
CE3EC95028D195E0009471BC /* LLRBTreeTests.swift */,
CE3EC95D28D2AA9C009471BC /* SplayTreeTests.swift */,
Expand Down Expand Up @@ -928,6 +931,7 @@
CE7B998628E520D600D56198 /* CRDTArrayTests.swift in Sources */,
9AB38FD42A9337DF006FA879 /* PresenceTests.swift in Sources */,
CEA2DA4328F672AD00431B61 /* MoveOperationTests.swift in Sources */,
9A5A80DA2BD238400047B978 /* URLTests.swift in Sources */,
9AFB4EBD2A94DA0600494401 /* ClientIntegrationTests.swift in Sources */,
96DA809128C5B7B400E2C1DA /* GRPCTests.swift in Sources */,
CEBC84072909130900781AE9 /* JSONArrayTests.swift in Sources */,
Expand Down

0 comments on commit c957485

Please sign in to comment.