Skip to content

Commit

Permalink
use nightscout port number if available for missing cases
Browse files Browse the repository at this point in the history
- 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#400
  • Loading branch information
paulplant committed Jan 28, 2023
1 parent 7eb822c commit 8f98f6d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
9 changes: 6 additions & 3 deletions xdrip/Managers/NightScout/Endpoint+NightScout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
}
}
8 changes: 8 additions & 0 deletions xdrip/Utilities/Network/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down

0 comments on commit 8f98f6d

Please sign in to comment.