mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-04-06 18:46:43 +00:00
Update Swift API to use new Keyring REST API
Now uses the correct, documented REST requests (not the legacy ones) and includes a single-identity query.
This commit is contained in:
parent
e349c8fc9c
commit
bc637e1a5c
@ -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
|
||||
|
@ -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<Int> = [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)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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() {
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user