From 76bcbe8fb5001d1c19a2e12e575a5144f34d57bf Mon Sep 17 00:00:00 2001 From: Alexander Kobjolke Date: Sun, 26 Nov 2023 21:12:12 +0100 Subject: [PATCH] =?UTF-8?q?Query=20tankerk=C3=B6nig=20API=20with=20given?= =?UTF-8?q?=20apikey=20and=20station?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/TK2MQTT.hs | 17 ++++--- tk2mqtt.cabal | 119 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 tk2mqtt.cabal diff --git a/src/TK2MQTT.hs b/src/TK2MQTT.hs index 1fbaeb8..c892e8e 100644 --- a/src/TK2MQTT.hs +++ b/src/TK2MQTT.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE OverloadedStrings #-} module TK2MQTT ( runTK2MQTT, @@ -8,12 +8,17 @@ module TK2MQTT ( import System.Environment qualified as Env import System.IO +import Control.Monad.IO.Class import Control.Exception qualified as Exception import System.IO.Error qualified as IOError -runTK2MQTT :: IO () -runTK2MQTT = do - putStrLn "Hello" +import Data.Aeson +import Network.HTTP.Req + +runTK2MQTT :: String -> String -> IO () +runTK2MQTT apikey stationId = runReq defaultHttpConfig $ do + r <- req GET (https "creativecommons.tankerkoenig.de" /: "json" /: "prices.php") NoReqBody jsonResponse ("apikey" =: apikey <> "ids" =: stationId) + liftIO $ print (responseBody r :: Value) eitherToError :: Show a => Either a b -> IO b eitherToError = either (Exception.throwIO . IOError.userError . show) return @@ -23,4 +28,6 @@ handleError e = do hPutStrLn stderr $ "I ran into an issue: " <> show e defaultMain :: IO () -defaultMain = Exception.catch runTK2MQTT handleError +defaultMain = do + [apikey, stationId] <- Env.getArgs + Exception.catch (runTK2MQTT apikey stationId) handleError diff --git a/tk2mqtt.cabal b/tk2mqtt.cabal new file mode 100644 index 0000000..a2da516 --- /dev/null +++ b/tk2mqtt.cabal @@ -0,0 +1,119 @@ +cabal-version: 1.12 + +-- This file has been generated from package.yaml by hpack version 0.34.7. +-- +-- see: https://github.com/sol/hpack + +name: tk2mqtt +version: 0.0.1.0 +description: Tankerkönig-to-MQTT gateway +author: Alexander Kobjolke +maintainer: alex@jakalx.net +copyright: Alexander Kobjolke 2023 +license: MIT +build-type: Simple +extra-source-files: + README.org + +library + exposed-modules: + TK2MQTT + TK2MQTT.Internal + other-modules: + Paths_tk2mqtt + hs-source-dirs: + src + default-extensions: + BlockArguments + OverloadedStrings + ImportQualifiedPost + ghc-options: -Wall -fdefer-typed-holes + build-depends: + aeson + , base >=4.13 && <5 + , containers + , directory + , net-mqtt + , req + , text + , time + default-language: GHC2021 + +executable tk2mqtt + main-is: tk2mqtt.hs + other-modules: + Paths_tk2mqtt + hs-source-dirs: + app + default-extensions: + BlockArguments + OverloadedStrings + ImportQualifiedPost + ghc-options: -Wall -fdefer-typed-holes + build-depends: + aeson + , base >=4.13 && <5 + , containers + , directory + , net-mqtt + , req + , text + , time + , tk2mqtt + default-language: GHC2021 + +test-suite doctest + type: exitcode-stdio-1.0 + main-is: Doctest.hs + other-modules: + Paths_tk2mqtt + hs-source-dirs: + test/doctest + default-extensions: + BlockArguments + OverloadedStrings + ImportQualifiedPost + ghc-options: -Wall -fdefer-typed-holes + build-tool-depends: + doctest:doctest + build-depends: + aeson + , base >=4.13 && <5 + , containers + , directory + , net-mqtt + , req + , text + , time + default-language: Haskell2010 + +test-suite spec + type: exitcode-stdio-1.0 + main-is: Spec.hs + other-modules: + TK2MQTTSpec + Paths_tk2mqtt + hs-source-dirs: + test/spec + default-extensions: + BlockArguments + OverloadedStrings + ImportQualifiedPost + ghc-options: -Wall -fdefer-typed-holes + cpp-options: -DTEST + build-tool-depends: + hspec-discover:hspec-discover + build-depends: + QuickCheck + , aeson + , base >=4.13 && <5 + , containers + , directory + , hspec + , net-mqtt + , quickcheck-instances + , req + , text + , time + , tk2mqtt + default-language: GHC2021