Query tankerkönig API with given apikey and station

This commit is contained in:
Alexander Kobjolke 2023-11-26 21:12:12 +01:00
parent b5d61674d8
commit 76bcbe8fb5
2 changed files with 131 additions and 5 deletions

View file

@ -1,4 +1,4 @@
{-# LANGUAGE RecordWildCards #-} {-# LANGUAGE OverloadedStrings #-}
module TK2MQTT ( module TK2MQTT (
runTK2MQTT, runTK2MQTT,
@ -8,12 +8,17 @@ module TK2MQTT (
import System.Environment qualified as Env import System.Environment qualified as Env
import System.IO import System.IO
import Control.Monad.IO.Class
import Control.Exception qualified as Exception import Control.Exception qualified as Exception
import System.IO.Error qualified as IOError import System.IO.Error qualified as IOError
runTK2MQTT :: IO () import Data.Aeson
runTK2MQTT = do import Network.HTTP.Req
putStrLn "Hello"
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 :: Show a => Either a b -> IO b
eitherToError = either (Exception.throwIO . IOError.userError . show) return eitherToError = either (Exception.throwIO . IOError.userError . show) return
@ -23,4 +28,6 @@ handleError e = do
hPutStrLn stderr $ "I ran into an issue: " <> show e hPutStrLn stderr $ "I ran into an issue: " <> show e
defaultMain :: IO () defaultMain :: IO ()
defaultMain = Exception.catch runTK2MQTT handleError defaultMain = do
[apikey, stationId] <- Env.getArgs
Exception.catch (runTK2MQTT apikey stationId) handleError

119
tk2mqtt.cabal Normal file
View file

@ -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