Skip to content

Commit ab2b2ec

Browse files
author
Marco Zocca
committed
v0.6
1 parent 43f4bd0 commit ab2b2ec

File tree

6 files changed

+147
-55
lines changed

6 files changed

+147
-55
lines changed

ms-graph-api.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ library
2020
hs-source-dirs: src
2121
exposed-modules:
2222
MSGraphAPI.User
23+
MSGraphAPI.Users.User
24+
MSGraphAPI.Users.Group
2325
MSGraphAPI.Drive
24-
MSGraphAPI.Files.DriveItems
26+
MSGraphAPI.Files.DriveItem
2527
Network.OAuth2.Provider.AzureAD
2628
Network.OAuth2.Session
2729
MSGraphAPI.Internal.Common
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module MSGraphAPI.Files.DriveItems where
1+
module MSGraphAPI.Files.DriveItem where
22

33
import GHC.Generics (Generic(..))
44

@@ -12,9 +12,20 @@ import Network.OAuth.OAuth2.Internal (AccessToken(..))
1212
import Network.HTTP.Req (Req)
1313
-- text
1414
import Data.Text (Text, pack, unpack)
15+
-- time
16+
import Data.Time (LocalTime)
1517

1618
import qualified MSGraphAPI.Internal.Common as MSG (get, getLbs, post, Collection, aesonOptions)
1719

20+
data DriveItem = DriveItem {
21+
diId :: Text
22+
, diName :: Text
23+
, diLastModifiedDateTime :: LocalTime
24+
} deriving (Eq, Ord, Show, Generic)
25+
instance A.FromJSON DriveItem where
26+
parseJSON = A.genericParseJSON (MSG.aesonOptions "di")
27+
28+
1829
-- | download a complete file from user's directory
1930
--
2031
-- @GET \/me\/drive\/items\/{item-id}\/content@

src/MSGraphAPI/User.hs

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,35 @@
11
module MSGraphAPI.User where
22

3-
import GHC.Generics (Generic(..))
4-
5-
import Data.List (sort, sortBy, stripPrefix, uncons)
6-
import Data.Maybe (listToMaybe, fromMaybe)
7-
-- import Data.Ord (comparing)
8-
import Data.Char (toLower)
9-
import Data.String (IsString(..))
10-
import Data.Word (Word)
11-
12-
-- aeson
13-
import qualified Data.Aeson as A (ToJSON(..), FromJSON(..), genericParseJSON, defaultOptions, Options(..), withObject, withText, (.:), (.:?), object, (.=), Key, Value, camelTo2)
14-
import qualified Data.Aeson.Types as A (Parser, Object)
15-
-- import qualified Data.Aeson.KeyMap as AKV (KeyMap, lookup)
16-
-- containers
17-
-- import qualified Data.Map as M (Map, empty, insert, lookup)
18-
-- hoauth
19-
import Network.OAuth.OAuth2.Internal (AccessToken(..))
20-
-- req
21-
import Network.HTTP.Req (Req)
22-
-- text
23-
import Data.Text (Text, pack, unpack)
24-
-- time
25-
import Data.Time.LocalTime (ZonedTime, zonedTimeToLocalTime)
26-
27-
import qualified MSGraphAPI.Internal.Common as MSG (get, post, aesonOptions)
28-
29-
-- | Get user information
30-
--
31-
-- @GET \/users\/{user-id}@
32-
--
33-
-- https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http#request
34-
get :: Text -- ^ user id
35-
-> AccessToken -> Req User
36-
get uid = MSG.get ["users", uid] mempty
37-
38-
-- | Get information on signed-in user
39-
--
40-
-- Calling the \/me endpoint requires a signed-in user and therefore a delegated permission. Application permissions are not supported when using the \/me endpoint.
41-
--
42-
-- @GET \/me@
43-
--
44-
-- https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http#request-1
45-
getMe :: AccessToken -> Req User
46-
getMe = MSG.get ["me"] mempty
47-
48-
49-
data User = User {
50-
uId :: Text
51-
, uUserPrincipalName :: Text
52-
, uDisplayName :: Text
53-
} deriving (Eq, Ord, Show, Generic)
54-
instance A.FromJSON User where
55-
parseJSON = A.genericParseJSON (MSG.aesonOptions "u")
3+
4+
5+
-- import Data.List (sort, sortBy, stripPrefix, uncons)
6+
-- import Data.Maybe (listToMaybe, fromMaybe)
7+
-- -- import Data.Ord (comparing)
8+
-- import Data.Char (toLower)
9+
-- import Data.String (IsString(..))
10+
-- import Data.Word (Word)
11+
12+
-- -- aeson
13+
-- import qualified Data.Aeson as A (ToJSON(..), FromJSON(..), genericParseJSON, defaultOptions, Options(..), withObject, withText, (.:), (.:?), object, (.=), Key, Value, camelTo2)
14+
-- import qualified Data.Aeson.Types as A (Parser, Object)
15+
-- -- import qualified Data.Aeson.KeyMap as AKV (KeyMap, lookup)
16+
-- -- containers
17+
-- -- import qualified Data.Map as M (Map, empty, insert, lookup)
18+
-- -- hoauth
19+
-- import Network.OAuth.OAuth2.Internal (AccessToken(..))
20+
-- -- req
21+
-- import Network.HTTP.Req (Req)
22+
-- -- text
23+
-- import Data.Text (Text, pack, unpack)
24+
-- -- time
25+
-- import Data.Time.LocalTime (ZonedTime, zonedTimeToLocalTime)
26+
27+
-- import qualified MSGraphAPI.Internal.Common as MSG (get, post, aesonOptions)
28+
-- -- import MSGraphAPI.Users.User (User)
29+
30+
31+
32+
33+
5634

5735

src/MSGraphAPI/Users/Group.hs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
-- | User
2+
module MSGraphAPI.Users.Group where
3+
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+
-- hoauth
9+
import Network.OAuth.OAuth2.Internal (AccessToken(..))
10+
-- req
11+
import Network.HTTP.Req (Req)
12+
-- text
13+
import Data.Text (Text, pack, unpack)
14+
15+
import qualified MSGraphAPI.Internal.Common as MSG (Collection(..), get, post, aesonOptions)
16+
import MSGraphAPI.Files.DriveItem (DriveItem)
17+
18+
-- | Groups are collections of principals with shared access to resources in Microsoft services or in your app. Different principals such as users, other groups, devices, and applications can be part of groups.
19+
--
20+
-- httpstea://learn.microsoft.com/en-us/graph/api/resources/groups-overview?view=graph-rest-1.0&tabs=http
21+
data Group = Group {
22+
gId :: Text
23+
, gDisplayName :: Text
24+
, gDescription :: Text
25+
} deriving (Eq, Ord, Show, Generic)
26+
instance A.FromJSON Group where
27+
parseJSON = A.genericParseJSON (MSG.aesonOptions "g")
28+
29+
-- | Get the teams in Microsoft Teams that the user is a direct member of.
30+
--
31+
-- @GET \/users\/{id | user-principal-name}\/joinedTeams@
32+
--
33+
-- https://learn.microsoft.com/en-us/graph/api/user-list-joinedteams?view=graph-rest-1.0&tabs=http
34+
getUserJoinedTeams :: Text -- ^ User ID
35+
-> AccessToken -> Req (MSG.Collection Group)
36+
getUserJoinedTeams uid = MSG.get ["users", uid, "joinedTeams"] mempty
37+
38+
-- | Get the 'DriveItem's in the 'Group' storage, starting from the root item
39+
--
40+
-- @GET \/groups\/{group-id}\/drive\/root\/children@
41+
--
42+
-- https://learn.microsoft.com/en-us/graph/api/driveitem-list-children?view=graph-rest-1.0&tabs=http
43+
getGroupsDriveItems :: Text -- ^ Group ID
44+
-> AccessToken -> Req (MSG.Collection DriveItem)
45+
getGroupsDriveItems gid = MSG.get ["groups", gid, "drive", "root", "children"] mempty
46+
47+
48+
-- data X = X { xName :: Text } deriving (Eq, Ord, Show, Generic)
49+
-- instance A.FromJSON X where
50+
-- parseJSON = A.genericParseJSON (MSG.aesonOptions "x")
51+
52+
-- pt0 :: Either String (MSG.Collection DriveItem)
53+
-- pt0 = A.eitherDecode t0
54+
-- where
55+
-- t0 = "{\r\n \"value\": [\r\n {\"name\": \"myfile.jpg\" },\r\n {\"name\": \"Documents\" },\r\n {\"name\": \"Photos\" },\r\n {\"name\": \"my sheet(1).xlsx\"}\r\n ]\r\n}"

src/MSGraphAPI/Users/User.hs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
-- | User
2+
module MSGraphAPI.Users.User where
3+
4+
import GHC.Generics (Generic(..))
5+
6+
-- aeson
7+
import qualified Data.Aeson as A (ToJSON(..), FromJSON(..), genericParseJSON, defaultOptions, Options(..), withObject, withText, (.:), (.:?), object, (.=))
8+
-- hoauth
9+
import Network.OAuth.OAuth2.Internal (AccessToken(..))
10+
-- req
11+
import Network.HTTP.Req (Req)
12+
-- text
13+
import Data.Text (Text, pack, unpack)
14+
15+
import qualified MSGraphAPI.Internal.Common as MSG (get, post, aesonOptions)
16+
17+
18+
data User = User {
19+
uId :: Text
20+
, uUserPrincipalName :: Text
21+
, uDisplayName :: Text
22+
} deriving (Eq, Ord, Show, Generic)
23+
instance A.FromJSON User where
24+
parseJSON = A.genericParseJSON (MSG.aesonOptions "u")
25+
26+
27+
-- | Get user information
28+
--
29+
-- @GET \/users\/{user-id}@
30+
--
31+
-- https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http#request
32+
get :: Text -- ^ user id
33+
-> AccessToken -> Req User
34+
get uid = MSG.get ["users", uid] mempty
35+
36+
-- | Get information on signed-in user
37+
--
38+
-- Calling the \/me endpoint requires a signed-in user and therefore a delegated permission. Application permissions are not supported when using the \/me endpoint.
39+
--
40+
-- @GET \/me@
41+
--
42+
-- https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http#request-1
43+
getMe :: AccessToken -> Req User
44+
getMe = MSG.get ["me"] mempty

src/Network/OAuth2/Provider/AzureAD.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ data OAuthCfg = OAuthCfg {
4545
}
4646

4747
-- | NB : scopes @openid@ and @offline_access@ are ALWAYS requested since the library assumes we have access to refresh tokens and ID tokens
48+
--
49+
-- Reference on Microsoft Graph permissions : https://learn.microsoft.com/en-us/graph/permissions-reference
4850
azureADApp :: OAuthCfg -- ^ OAuth configuration
4951
-> IdpApplication 'AuthorizationCode AzureAD
5052
azureADApp (OAuthCfg appname clid sec scopes authstate reduri) = defaultAzureADApp{

0 commit comments

Comments
 (0)