diff --git a/swift-client-api/client_util.swift b/swift-client-api/client_util.swift index 8e74c45b..f111770a 100644 --- a/swift-client-api/client_util.swift +++ b/swift-client-api/client_util.swift @@ -1,6 +1,6 @@ /* Serval DNA Client Swift test program -Copyright (C) 2016-2017 Flinders University +Copyright (C) 2016-2018 Flinders University This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -28,6 +28,7 @@ var arg0 : String = "" func usage() { // Once no longer supporting Swift 3, change this to a multi-line string literal. print("Usage: \(arg0) [options] keyring --pin PIN list") + print(" \(arg0) [options] keyring --pin PIN get SID") print(" \(arg0) [options] keyring --pin PIN add [ did DID ] [ name NAME ]") print(" \(arg0) [options] keyring --pin PIN remove SID") print(" \(arg0) [options] keyring --pin PIN set SID [ did DID ] [ name NAME ]") @@ -126,6 +127,27 @@ func keyring(_ args: inout [String], configuration: ServalRestfulClient.Configur print("Done", to: &debugStream) request.close() + case "get": + let sid = SubscriberId(fromHex: args.remove(at: 0))! + precondition(args.isEmpty) + print("Getting (sid=\(sid.hexUpper))...") + let semaphore = DispatchSemaphore(value: 0) + let client = ServalRestfulClient(configuration: configuration) + let request = ServalKeyring.getIdentity(client: client, sid: sid, pin: pin) { (identity, error) in + if let error = error { + print(error, to: &errorStream) + status = 2 + } + else if let identity = identity { + printIdentity(identity: identity) + } + semaphore.signal() + } + print("Waiting...", to: &debugStream) + semaphore.wait() + print("Done", to: &debugStream) + request.close() + case "add": var did : String? = nil var name : String? = nil diff --git a/swift-client-api/package/Sources/keyring.swift b/swift-client-api/package/Sources/keyring.swift index 87c786cb..bd884f5c 100644 --- a/swift-client-api/package/Sources/keyring.swift +++ b/swift-client-api/package/Sources/keyring.swift @@ -1,6 +1,6 @@ /* Serval DNA Swift API -Copyright (C) 2016 Flinders University +Copyright (C) 2016-2018 Flinders University This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -35,7 +35,8 @@ public class ServalKeyring { { var param = [String: String]() if pin != nil { param["pin"] = pin } - return client.createRequest(path: "restful/keyring/identities.json", + return client.createRequest(verb: "GET", + path: "restful/keyring/identities.json", query: param) { (statusCode, json, error) in if let error = error { completionHandler(nil, error) @@ -125,7 +126,7 @@ public class ServalKeyring { return } if !text.isEmpty { - did = text + did = text } } } @@ -148,13 +149,14 @@ public class ServalKeyring { } private static func singleIdentityRequest(client: ServalRestfulClient = ServalRestfulClient(), + verb: String, path: String, query: [String: String] = [:], successStatusCodes: Set = [200], completionHandler: @escaping (Identity?, Error?) -> Void) -> ServalRestfulClient.Request { - return client.createRequest(path: path, query: query) { (statusCode, json, error) in + return client.createRequest(verb:verb, path: path, query: query) { (statusCode, json, error) in if let error = error { completionHandler(nil, error) return @@ -205,12 +207,28 @@ public class ServalKeyring { if name != nil { param["name"] = name } if pin != nil { param["pin"] = pin } return self.singleIdentityRequest(client: client, + verb: "POST", path: "restful/keyring/add", query: param, successStatusCodes: [201], completionHandler: completionHandler) } + public static func getIdentity(client: ServalRestfulClient = ServalRestfulClient(), + sid: SubscriberId, + pin: String? = nil, + completionHandler: @escaping (Identity?, Error?) -> Void) + -> ServalRestfulClient.Request + { + var param = [String: String]() + if pin != nil { param["pin"] = pin } + return self.singleIdentityRequest(client: client, + verb: "GET", + path: "restful/keyring/\(sid.hexUpper)", + query: param, + completionHandler: completionHandler) + } + public static func removeIdentity(client: ServalRestfulClient = ServalRestfulClient(), sid: SubscriberId, pin: String? = nil, @@ -219,7 +237,11 @@ public class ServalKeyring { { var param = [String: String]() if pin != nil { param["pin"] = pin } - return self.singleIdentityRequest(client: client, path: "restful/keyring/\(sid.hexUpper)/remove", query: param, completionHandler: completionHandler) + return self.singleIdentityRequest(client: client, + verb: "DELETE", + path: "restful/keyring/\(sid.hexUpper)", + query: param, + completionHandler: completionHandler) } public static func setIdentity(client: ServalRestfulClient = ServalRestfulClient(), @@ -234,7 +256,11 @@ public class ServalKeyring { if did != nil { param["did"] = did } if name != nil { param["name"] = name } if pin != nil { param["pin"] = pin } - return self.singleIdentityRequest(client: client, path: "restful/keyring/\(sid.hexUpper)/set", query: param, completionHandler: completionHandler) + return self.singleIdentityRequest(client: client, + verb: "PATCH", + path: "restful/keyring/\(sid.hexUpper)", + query: param, + completionHandler: completionHandler) } } diff --git a/swift-client-api/package/Sources/restful_client.swift b/swift-client-api/package/Sources/restful_client.swift index 476060fb..ef3b3c10 100644 --- a/swift-client-api/package/Sources/restful_client.swift +++ b/swift-client-api/package/Sources/restful_client.swift @@ -1,6 +1,6 @@ /* Serval DNA Swift API -Copyright (C) 2016 Flinders University +Copyright (C) 2016-2018 Flinders University This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -64,7 +64,8 @@ public class ServalRestfulClient { self.configuration = configuration } - public func createRequest(path: String, + public func createRequest(verb: String, + path: String, query: [String: String] = [:], completionHandler: @escaping (Int?, Any?, Error?) -> Void) -> Request? @@ -86,7 +87,7 @@ public class ServalRestfulClient { let session = URLSession(configuration: URLSessionConfiguration.default) debugPrint(url, to: &debugStream) var request = URLRequest(url: url) - request.httpMethod = "GET" + request.httpMethod = verb if !self.configuration.username.isEmpty { let data = (self.configuration.username + ":" + self.configuration.password).data(using: String.Encoding.utf8) if let base64 = data?.base64EncodedString() { diff --git a/tests/keyringswift b/tests/keyringswift index 96d3f29b..0f585737 100755 --- a/tests/keyringswift +++ b/tests/keyringswift @@ -3,7 +3,7 @@ # Tests for Keyring Swift API. # # Copyright 2015 Serval Project Inc. -# Copyright 2017 Flinders University +# Copyright 2017-2018 Flinders University # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -74,6 +74,16 @@ test_keyringList() { assert_keyring_list $IDENTITY_COUNT } +doc_keyringGet="Swift API get single keyring identity" +test_keyringGet() { + executeSwiftOk keyring get "$SIDA1" + tfw_cat --stdout --stderr + assert_stdout_keyvalue sid "$SIDA1" + assert_stdout_keyvalue identity "$IDA1" + assert_stdout_keyvalue did "$DIDA1" + assert_stdout_keyvalue name "$NAMEA1" +} + doc_keyringListPin="Swift API list keyring identities, with PIN" setup_keyringListPin() { IDENTITY_COUNT=3