packages feed

join-api (empty) → 0.0.0.0

raw patch · 5 files changed

+152/−0 lines, 5 filesdep +aesondep +basedep +bytestringsetup-changed

Dependencies added: aeson, base, bytestring, lens, text, url, wreq

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2017++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Author name here nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,3 @@+# join-api++Bindings to the API for https://joaoapps.com/join/api/.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ join-api.cabal view
@@ -0,0 +1,30 @@+name:                join-api+version:             0.0.0.0+synopsis:            Bindings for Join push notifications+description:         Bindings to the API for Join's notification system+homepage:            https://github.com/cotrone/join-api#readme+license:             BSD3+license-file:        LICENSE+author:              Kevin Cotrone+maintainer:          kevincotrone@gmail.com+copyright:           2017 Kevin Cotrone+category:            Web+build-type:          Simple+extra-source-files:  README.md+cabal-version:       >=1.10++library+  hs-source-dirs:      src+  exposed-modules:     Network.Join+  default-language:    Haskell2010+  build-depends:       base >= 4.7 && < 5+                     , text >= 1.0 && < 1.3+                     , url >= 2.0 && < 3.0+                     , wreq >= 0.4 && < 0.6+                     , bytestring >= 0.10 && < 0.11+                     , aeson >= 1.0 && < 1.3+                     , lens >= 4.0 && < 5++source-repository head+  type:     git+  location: https://github.com/cotrone/join-api
+ src/Network/Join.hs view
@@ -0,0 +1,87 @@+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE OverloadedStrings #-}+module Network.Join (+    APIKey(..)+  , DeviceId(..)+  , SMS(..)+  , sendSMS+  , sendPush+  ) where++import Data.Aeson+import Data.Text (Text)+import qualified Data.Text as T+import Data.String+import Network.URL+import Network.Wreq+import Data.ByteString.Lazy (ByteString)+import Control.Lens++-- | API Key for the account+newtype APIKey = APIKey {+  unAPIKey :: Text+} deriving (Eq, Ord, Show, IsString)++-- | Device ID to send the message to/through+newtype DeviceId = DeviceId {+  unDeviceId :: Text+} deriving (Eq, Ord, Show, IsString)++-- | SMS Message with phone number to send the+-- message to+data SMS = SMS {+  smsNumber :: Text+, smsText :: Text+} deriving (Eq, Ord, Show)++joinUrlType :: URLType+joinUrlType =+  Absolute $+    Host (HTTP True) "joinjoaomgcd.appspot.com" Nothing++joinPath :: String+joinPath = "_ah/api/messaging/v1/sendPush"++data JoinResponse = JoinResponse {+  joinSuccess :: Bool+, joinUserAuthError :: Bool+, joinKind :: Text+, joinETag :: Text+}++instance FromJSON JoinResponse where+  parseJSON (Object o) =+    JoinResponse+      <$> (o .: "success")+      <*> (o .: "userAuthError")+      <*> (o .: "kind")+      <*> (o .: "etag")++-- | Send an SMS from the given device.+-- If the device doesn't allow join to send SMS,+-- there will be an error in the response+sendSMS :: APIKey -> DeviceId -> SMS -> IO JoinResponse+sendSMS (APIKey key) (DeviceId i) (SMS number msg) = do+  resp <- asJSON =<< (get $ exportURL smsUrl)+  return $ resp ^. responseBody+  where+    smsUrl = URL joinUrlType joinPath params+    params = (fmap T.unpack) <$> [+        ("deviceId", i)+      , ("apikey", key)+      , ("smsnumber", number)+      , ("smstext", msg)+      ]++-- | Send a push notification to a given device+sendPush :: APIKey -> DeviceId -> Text -> IO JoinResponse+sendPush (APIKey key) (DeviceId i) msg = do+  resp <- asJSON =<< (get $ exportURL smsUrl)+  return $ resp ^. responseBody+  where+    smsUrl = URL joinUrlType joinPath params+    params = (fmap T.unpack) <$> [+        ("deviceId", i)+      , ("apikey", key)+      , ("text", msg)+      ]