ctucx.git: smartied

[nimlang] smarthome server

commit 10b75c89b785799533ffb160a981ee1cfd7e8935
parent c34b63eaf65d687c095b4eaaeb0d697ca3a3c5ce
Author: Leah (ctucx) <leah@ctu.cx>
Date: Sat, 27 Feb 2021 12:49:26 +0100

influx: use authtoken
2 files changed, 6 insertions(+), 7 deletions(-)
M
src/influx.nim
|
10
+++++-----
M
src/types.nim
|
3
+--
diff --git a/src/influx.nim b/src/influx.nim
@@ -1,12 +1,12 @@
-import asyncdispatch, strutils, tables, httpclient, options, json, base64, uri
+import asyncdispatch, strutils, tables, httpclient, options, json, uri
 import types, vars
 
 proc existsDatabase* (config: InfluxConfig, databaseName: string): Future[bool] {.async.}=
   try:
     var client = newAsyncHttpClient()
 
-    if config.username.isSome and config.password.isSome:
-      client.headers["Authorization"] = "Basic " & base64.encode(config.username.get & ":" & config.password.get)
+    if config.authToken.isSome:
+      client.headers["Authorization"] = "Token " & $config.authToken.get
 
     let baseUrl  = "http://" & config.host & ":" & $config.port & "/"
     let response = await client.getContent(baseUrl & "query?" & encodeQuery({"db": databaseName, "q": "select * FROM test LIMIT 1"}))

@@ -25,8 +25,8 @@ proc insertDatabase* (config: InfluxConfig, databaseName: string, tableName: str
   try:
     var client = newAsyncHttpClient()
 
-    if config.username.isSome and config.password.isSome:
-      client.headers["Authorization"] = "Basic " & base64.encode(config.username.get & ":" & config.password.get)
+    if config.authToken.isSome:
+      client.headers["Authorization"] = "Token " & $config.authToken.get
 
     let baseUrl  = "http://" & config.host & ":" & $config.port & "/"
 
diff --git a/src/types.nim b/src/types.nim
@@ -107,8 +107,7 @@ type LacrosseConfig* = object
 type InfluxConfig* = object
   host*:                string
   port*:                int
-  username*:            Option[string]
-  password*:            Option[string]
+  authToken*:           Option[string]
   powermetersDatabase*: Option[string]
   sensorsDatabase*:     Option[string]