From 8f98f6d1c2b99e00fd41353c0d778a372e42cf58 Mon Sep 17 00:00:00 2001 From: Paul Plant Date: Sat, 28 Jan 2023 11:20:04 +0100 Subject: [PATCH] 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 --- xdrip/Managers/NightScout/Endpoint+NightScout.swift | 9 ++++++--- xdrip/Utilities/Network/Endpoint.swift | 8 ++++++++ .../SettingsViewNightScoutSettingsViewModel.swift | 7 ++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/xdrip/Managers/NightScout/Endpoint+NightScout.swift b/xdrip/Managers/NightScout/Endpoint+NightScout.swift index a9a731d0..57e93013 100644 --- a/xdrip/Managers/NightScout/Endpoint+NightScout.swift +++ b/xdrip/Managers/NightScout/Endpoint+NightScout.swift @@ -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 ) } } diff --git a/xdrip/Utilities/Network/Endpoint.swift b/xdrip/Utilities/Network/Endpoint.swift index 59bd0321..69a7dc51 100644 --- a/xdrip/Utilities/Network/Endpoint.swift +++ b/xdrip/Utilities/Network/Endpoint.swift @@ -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 } } diff --git a/xdrip/View Controllers/SettingsNavigationController/SettingsViewController/SettingsViewModels/SettingsViewNightScoutSettingsViewModel.swift b/xdrip/View Controllers/SettingsNavigationController/SettingsViewController/SettingsViewModels/SettingsViewNightScoutSettingsViewModel.swift index 73247ab1..d9833c01 100644 --- a/xdrip/View Controllers/SettingsNavigationController/SettingsViewController/SettingsViewModels/SettingsViewNightScoutSettingsViewModel.swift +++ b/xdrip/View Controllers/SettingsNavigationController/SettingsViewController/SettingsViewModels/SettingsViewNightScoutSettingsViewModel.swift @@ -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) {