use nightscout port number if available for missing cases

- use the server port number if available when pulling from /api/v1/entries endpoint
- use the server port number if available to check connection at /api/v1/experiments/test
- fixes JohanDegraeve/xdripswift#400
This commit is contained in:
Paul Plant 2023-01-28 11:20:04 +01:00
parent 7eb822c1a1
commit 8f98f6d1c2
3 changed files with 20 additions and 4 deletions

View File

@ -9,6 +9,8 @@ extension Endpoint {
/// - parameters:
/// - hostAndScheme : hostname, eg http://www.mysite.com or https://www.mysite.com - must include the scheme - IF HOST DOESN'T START WITH A KNOWN SCHEME, THEN A FATAL ERROR WILL BE THROWN - known scheme's can be found in type EndPointScheme
/// - count : maximum number of readings to get
/// - token: the Nightscout token used for authentication (optional)
/// - port: Nightscout server port number (optional)
static func getEndpointForLatestNSEntries(hostAndScheme:String, count: Int, token: String?) -> Endpoint {
// split hostAndScheme in host and scheme
@ -30,10 +32,11 @@ extension Endpoint {
}
return Endpoint(
host:host,
scheme:scheme!,
host: host,
scheme: scheme!,
path: "/api/v1/entries/sgv.json",
queryItems: queryItems
queryItems: queryItems,
port: UserDefaults.standard.nightScoutPort
)
}
}

View File

@ -17,6 +17,9 @@ struct Endpoint {
/// array of URLQueryItem
let queryItems: [URLQueryItem]
/// the Nightscout server port number
let port: Int
/// gets url
var url: URL? {
var components = URLComponents()
@ -25,6 +28,11 @@ struct Endpoint {
components.path = path
components.queryItems = queryItems
// add the port number component only if it exists (this avoids URLComponents adding a port number of 0 as port number is stored as non-optional integer in UserDefaults)
if port != 0 {
components.port = port
}
return components.url
}
}

View File

@ -57,7 +57,12 @@ class SettingsViewNightScoutSettingsViewModel {
private func testNightScoutCredentials() {
// unwrap siteUrl and apiKey
guard let siteUrl = UserDefaults.standard.nightScoutUrl else {return}
guard var siteUrl = UserDefaults.standard.nightScoutUrl else {return}
// add port number if it exists
if UserDefaults.standard.nightScoutPort != 0 {
siteUrl += ":" + UserDefaults.standard.nightScoutPort.description
}
if let url = URL(string: siteUrl) {