Skip to content

Commit

Permalink
Add example program and to build (#35)
Browse files Browse the repository at this point in the history
- Fix example code in README
  • Loading branch information
donatello authored May 2, 2024
1 parent dde52fe commit c6ce618
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:

- name: Configure
run: |
cabal configure --enable-tests --enable-benchmarks --test-show-details=direct
cabal configure --enable-tests --enable-benchmarks --test-show-details=direct -fexamples
cabal build all --dry-run
# The last step generates dist-newstyle/cache/plan.json for the cache key.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ testMail =
from = MailAddress "jane@example.com" "Jane Smith"
subject = "Email Subject"
content = fromList [mailContentText "Example Content"]
in mail [to] from subject content
in mail [to] from subject $ Just content

main :: IO ()
main = do
Expand Down
28 changes: 28 additions & 0 deletions examples/SendEmail.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{-# LANGUAGE OverloadedStrings #-}

import Data.List.NonEmpty (fromList)
import Data.Text (Text)
import Network.SendGridV3.Api
import Control.Lens ((^.))
import Network.Wreq (responseStatus, statusCode)

sendGridApiKey :: Text
sendGridApiKey = "SG..."

testMail :: Mail () ()
testMail =
let to = personalization $ fromList [MailAddress "john@example.com" "John Doe"]
from = MailAddress "jane@example.com" "Jane Smith"
subject = "Email Subject"
content = fromList [mailContentText "Example Content"]
in mail [to] from subject $ Just content

main :: IO ()
main = do
sendgridSettings <- mkSendGridSettings sendGridApiKey

-- Send an email, overriding options as needed
eResponse <- sendMail sendgridSettings (testMail { _mailSendAt = Just 1516468000 })
case eResponse of
Left httpException -> error $ show httpException
Right response -> print (response ^. responseStatus . statusCode)
30 changes: 29 additions & 1 deletion sendgrid-v3.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ maintainer: buesing.marcel@googlemail.com
-- copyright:
category: Network
build-type: Simple
extra-source-files: ChangeLog.md
extra-doc-files:
ChangeLog.md
README.md
extra-source-files:
examples/*.hs
stack.yaml
tested-with: GHC == 8.10.7
, GHC == 9.0.2
, GHC == 9.2.8
Expand Down Expand Up @@ -60,3 +65,26 @@ test-suite test
, tasty-hunit >= 0.10
, text >= 1.2
, wreq >= 0.5

Flag examples
Description: Build the examples
Default: False
Manual: True

common examples-settings
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010
default-extensions: OverloadedStrings
build-depends: base >= 4.7 && < 5
, lens
, sendgrid-v3
, text
, wreq
hs-source-dirs: examples
if !flag(examples)
buildable: False

executable SendEmail
import: examples-settings
scope: private
main-is: SendEmail.hs

0 comments on commit c6ce618

Please sign in to comment.