Created
August 20, 2018 17:22
-
-
Save Woody88/defad3921c83fb4147bd3c5cafb1b8ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Salesforce.SObject.Internal where | |
| import Affjax as AX | |
| import Affjax.Internal (decodeWithEitherError) | |
| import Affjax.RequestBody (json, string) | |
| import Affjax.RequestHeader (RequestHeader(..)) | |
| import Affjax.ResponseFormat as ResponseFormat | |
| import Data.Argonaut.Core (fromString, stringify) | |
| import Data.Bifunctor (lmap) | |
| import Data.Either (Either(..)) | |
| import Data.Maybe (Maybe(..), fromMaybe) | |
| import Data.HTTP.Method (Method(..)) | |
| import Foreign.Class (class Encode) | |
| import Foreign.Generic (encodeJSON) | |
| import Prelude ((<$>), (<>), ($), bind, pure, discard) | |
| import Salesforce.Connection.Types (Connection(..)) | |
| import Salesforce.SObject.Types (SObjectError, SObjectId, SObjectName, InsertResults, InsertResult(..), sobjectId) | |
| import Salesforce.Types (Salesforce, salesforce) | |
| import Salesforce.Util (Endpoint(..), baseUrl) | |
| import Effect.Console (logShow, log) | |
| import Effect.Class (liftEffect) | |
| import Effect.Aff (Aff) | |
| insert :: forall a. Encode a => SObjectName -> a -> Salesforce SObjectError String | |
| insert sobjName sobj = salesforce \conn'@(Connection conn) -> do | |
| liftEffect $ log $ "JSON: " <> encodeJSON sobj | |
| res <- AX.request $ AX.defaultRequest { url = baseUrl conn' $ SObject sobjName | |
| , method = Left POST | |
| , responseFormat = ResponseFormat.json | |
| , content = Just $ json $ fromString $ encodeJSON sobj | |
| , headers = [RequestHeader "Authorization" (authHeader conn.access_token $ fromMaybe "" conn.token_type)] | |
| } | |
| liftEffect $ logShow $ lmap (\s -> "Error: " <> AX.printResponseFormatError s) $ (\s -> "Body: " <> stringify s) <$> res.body | |
| pure $ decodeWithEitherError $ res { body = stringify <$> res.body } | |
| where | |
| authHeader token type_ = type_ <> " " <> token |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment