MyNixOS website logo
Description

Environment Monad with automatic resource refreshment.

Please see README.md

RefreshT -- Environment Monad with automatic resource refreshment

Overview

refresht package provides similar interface as ReaderT-monad, but it comes with automatic refreshment mechanism. In other words, the RefreshT monad transformer provides the general way to maintain the freshness of the external environment, with respoec to the specified condition or exceptions.

The typical usage is to communicate with the server which requires periodic refreshment of access tokens, such as Google API.

Usage

The following pseudo-code illustrates the typical usage:

import Control.Monad.Refresh

import Network.Wreq               (getWith, defaults)
import Control.Lens               ((&), (.~), (^.))
import Data.Time                  (addUTCTime, getCurrentTime, UTCTime)
import Data.ByetString.Lazy.Char8 (unpack)
import Control.Exception          (fromException)

data User = User { expiration  :: UTCTime
                 , accessToken :: String
                 }

main :: IO ()
main = do
  time <- getCurrentTime
  evalRefreshT conf (User (3600 `addUTCTime` time) "initialtoken") $ do
    rsc <- withEnv $ \User{..} ->
      getWith (defaults & auth .~ oauth2Bearer accessToken)
              "https://example.com/api/resource"
    putStrLn $ print rsc

conf :: RefreshSetting User IO
conf = defaultRefreshSetting
     & refresher         .~ update
     & shouldRefresh     .~ checkExpr
     & isRefreshingError .~ isRefreshing
  where
    shouldRefresh usr = do -- checks expiration
      now <- getCurrentTime
      return $ now <= expiration usr
          
    update usr = do
      -- Refreshed token for the service
      bdy <- getWith
        (defaults & param "token" .~ [accessToken usr])
        "https://example.com/api/refresh_token"
      let token = unpack $ bdy ^. responseBody
          usr'  = usr { accessToken = token
                      , expiration = ...
                      }

      -- Save updates in local file, or db.
      writeFile "database" (show usr')
      return usr'

    -- 401 Unauthorized exception should cause refreshment
    isRefreshing e =
      case fromException e of
        Just Error401 -> True
        _ -> False
Metadata

Version

0.1.1.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows