· 7 years ago · Mar 30, 2018, 02:16 PM
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE TypeApplications #-}
4{-# LANGUAGE TypeOperators #-}
5
6
7module Example where
8
9import Data.Text (Text)
10import Data.Aeson
11import Data.Proxy
12import GHC.Generics
13import Servant.Client
14import Haskforce
15import Haskforce.API.Resources
16
17
18data Account = Account
19 { name :: Text
20 } deriving (Generic, Show)
21
22instance SFObject Account where
23 sobjectName _ = "Account"
24
25instance ToJSON Account
26instance FromJSON Account where
27 parseJSON = genericParseJSON defaultOptions {
28 fieldLabelModifier = adjustFromJsonField'
29 }
30
31adjustFromJsonField' :: String -> String -- << this seems tedious
32adjustFromJsonField' "name" = "Name"
33adjustFromJsonField' x = x
34
35--- cred and tokenRequest would usually be in a config file and would be handled elsewhere.
36cred :: UserCred
37cred =
38 UserCred (Just . Username $ "woodmai.cm")
39 (Just . Userpassword $ "21231!")
40 (Just . SecretKey $ "WbLqWxHdbet5")
41 "3MVG9CEn_O.qhb_CMW4shC2zmPCd6wsm30EsxL4XCp5bRtEPTGvFjv_3AjWufKnyiS.o"
42 "333210859132"
43
44tokenRequest :: TokenRequest
45tokenRequest =
46 TokenRequest (GrantType Password)
47 (UserPassword)
48 cred
49 Nothing
50 Nothing
51 Nothing
52 Nothing
53
54sfclient = login tokenRequest salesforceUrl "v42.0"
55
56getAcc :: IO (Either ServantError (SObject Account))
57getAcc = do
58 (Right client) <- sfclient
59 getSObjectRow (SObjectId "0016A00000JcdjK") (Proxy @Account) client
60
61acc :: IO (SObject Account, SFClient)
62acc = do
63 (Right client) <- sfclient
64 (Right h) <- getSObjectRow (SObjectId "0016A00000JcdjK") (Proxy @Account) client
65 return (h, client)
66
67updateAcc :: IO (Either ServantError NoContent)
68updateAcc = do
69 (SObject (objId, objData), client) <- acc
70 updateSObjectRow objId (updateName $ sobject objData) client
71
72updateName :: Account -> Account
73updateName acc = acc { name = "NewAccount4" }
74
75deleteAccount :: IO (Either ServantError NoContent)
76deleteAccount = do
77 (Right client) <- sfclient
78 deleteSObjectRow (SObjectId "0016A00000JcdjK") (Proxy @Account) client
79
80
81----- Execution of Example -----
82λ> (Right account) <- getAcc
83*Example
84λ> account
85SObject (SObjectId "0016A00000JcdjKQAR",SObjectData {attributes = SObjectAttr {sobjectType = SObjectType "Account", sobjetUrl = "/services/data/v42.0/sobjects/Account/0016A00000JcdjKQAR"}, sobject = Account
86{name = "NewAccount4"}})
87λ> getSData account
88SObjectData {attributes = SObjectAttr {sobjectType = SObjectType "Account", sobjetUrl = "/services/data/v42.0/sobjects/Account/0016A00000JcdjKQAR"}, sobject = Account {name = "NewAccount4"}}
89*Example
90λ> sobject . getSData $ account
91Account {name = "NewAccount4"}