Follower mode : use API key if set in settings

This commit is contained in:
Johan Degraeve 2021-01-03 11:17:24 +01:00
parent 22ec7c6c21
commit a7f2981e81
3 changed files with 15 additions and 8 deletions

View File

@ -10,7 +10,7 @@ extension Endpoint {
/// - 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
/// - olderThan : only readings with timestamp > olderThan
static func getEndpointForLatestNSEntries(hostAndScheme:String, count: Int, olderThan timeStamp:Date) -> Endpoint {
static func getEndpointForLatestNSEntries(hostAndScheme:String, count: Int, olderThan timeStamp:Date, token: String?) -> Endpoint {
// split hostAndScheme in host and scheme
let (host, scheme) = EndPointScheme.getHostAndScheme(hostAndScheme: hostAndScheme)
@ -18,14 +18,21 @@ extension Endpoint {
// if scheme nil then looks like a coding error, throw fatal error
guard scheme != nil else {fatalError("in getEndpointForLatestNSEntries, hostAndScheme doesn't start with a known scheme name")}
// create quertyItems
var queryItems = [
URLQueryItem(name: "count", value: count.description),
URLQueryItem(name: "find[dateString][$gte]", value: timeStamp.ISOStringFromDate())]
// if token not nil, then add also the token
if let token = token {
queryItems.append(URLQueryItem(name: "token", value: token))
}
return Endpoint(
host:host,
scheme:scheme!,
path: "/api/v1/entries/sgv.json",
queryItems: [
URLQueryItem(name: "count", value: count.description),
URLQueryItem(name: "find[dateString][$gte]", value: timeStamp.ISOStringFromDate())
]
queryItems: queryItems
)
}
}

View File

@ -157,7 +157,7 @@ class NightScoutFollowManager:NSObject {
let count = Int(-timeStampOfFirstBgReadingToDowload.timeIntervalSinceNow / 60 + 1)
// ceate endpoint to get latest entries
let latestEntriesEndpoint = Endpoint.getEndpointForLatestNSEntries(hostAndScheme: nightScoutUrl, count: count, olderThan: timeStampOfFirstBgReadingToDowload)
let latestEntriesEndpoint = Endpoint.getEndpointForLatestNSEntries(hostAndScheme: nightScoutUrl, count: count, olderThan: timeStampOfFirstBgReadingToDowload, token: UserDefaults.standard.nightScoutAPIKey)
// create downloadTask and start download
if let url = latestEntriesEndpoint.url {

View File

@ -196,9 +196,9 @@ extension SettingsViewNightScoutSettingsViewModel: SettingsViewModelProtocol {
// if nightscout upload not enabled then only first row is shown
if UserDefaults.standard.nightScoutEnabled {
// in follower mode, only two first rows to be shown : nightscout enabled button and url
// in follower mode, only 5 first rows to be shown : nightscout enabled button, url, port number, api key, option to test
if !UserDefaults.standard.isMaster {
return 2
return 5
}
// if schedule not enabled then show all rows except the last which is to edit the schedule