|
| 1 | +module MSGraphAPI.ChangeNotifications.Subscription where |
| 2 | + |
| 3 | +import Data.List.NonEmpty (NonEmpty) |
| 4 | +import GHC.Generics (Generic(..)) |
| 5 | + |
| 6 | +-- aeson |
| 7 | +import qualified Data.Aeson as A (ToJSON(..), FromJSON(..), eitherDecode, genericParseJSON, defaultOptions, Options(..), withObject, withText, (.:), (.:?), object, (.=)) |
| 8 | +import qualified Data.Aeson.Encoding as A (text) |
| 9 | +-- hoauth |
| 10 | +import Network.OAuth.OAuth2.Internal (AccessToken(..)) |
| 11 | +-- req |
| 12 | +import Network.HTTP.Req (Req) |
| 13 | +-- text |
| 14 | +import Data.Text (Text, pack, unpack) |
| 15 | +-- time |
| 16 | +import Data.Time (LocalTime) |
| 17 | + |
| 18 | +import qualified MSGraphAPI.Internal.Common as MSG (Collection(..), get, post, aesonOptions) |
| 19 | +import MSGraphAPI.Files.DriveItem (DriveItem) |
| 20 | + |
| 21 | +-- | A subscription allows a client app to receive change notifications about changes to data in Microsoft Graph. |
| 22 | +-- |
| 23 | +-- https://learn.microsoft.com/en-us/graph/api/resources/subscription?view=graph-rest-1.0 |
| 24 | +data Subscription = Subscription { |
| 25 | + cnsId :: Text |
| 26 | + , cnsChangeType :: NonEmpty ChangeType |
| 27 | + , cnsClientState :: Text |
| 28 | + , cnsExpirationDateTime :: LocalTime |
| 29 | + , cnsNotificationUrl :: Text -- ^ The URL of the endpoint that will receive the change notifications. This URL must make use of the HTTPS protocol. Any query string parameter included in the notificationUrl property will be included in the HTTP POST request when Microsoft Graph sends the change notifications. |
| 30 | + , cnsResource :: Text -- ^ Specifies the resource that will be monitored for changes. Do not include the base URL (https://graph.microsoft.com/v1.0/) |
| 31 | + } deriving (Eq, Show, Generic) |
| 32 | +instance A.FromJSON Subscription where |
| 33 | + parseJSON = A.genericParseJSON (MSG.aesonOptions "cns") |
| 34 | + |
| 35 | + |
| 36 | +data ChangeType = CTCreated |
| 37 | + | CTUpdated |
| 38 | + | CTDeleted |
| 39 | + deriving (Eq, Show, Generic) |
| 40 | +instance A.FromJSON ChangeType where |
| 41 | + parseJSON = A.withText "ChangeType" $ \t -> |
| 42 | + case t of |
| 43 | + "created" -> pure CTCreated |
| 44 | + "updated" -> pure CTUpdated |
| 45 | + "deleted" -> pure CTDeleted |
| 46 | + x -> fail $ unwords ["ChangeType : unexpected value:", unpack x] |
| 47 | +instance A.ToJSON ChangeType where |
| 48 | + toEncoding = \case |
| 49 | + CTCreated -> A.text "created" |
| 50 | + CTUpdated -> A.text "updated" |
| 51 | + CTDeleted -> A.text "deleted" |
0 commit comments