-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathsign_in_reports.go
89 lines (76 loc) · 2.44 KB
/
sign_in_reports.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package msgraph
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
)
// SignInReports Client performs operations on Sign in reports.
type SignInReportsClient struct {
BaseClient Client
}
// NewSignInReportsClient returns a new SignInReportsClient.
func NewSignInReportsClient() *SignInReportsClient {
return &SignInReportsClient{
BaseClient: NewClient(VersionBeta),
}
}
// List returns a list of Sign-in Reports, optionally queried using OData.
func (c *SignInReportsClient) List(ctx context.Context, query odata.Query) (*[]SignInReport, int, error) {
unknownError := func(resp *http.Response, o *odata.OData) bool {
if resp != nil && resp.StatusCode == http.StatusBadRequest && o != nil && o.Error != nil {
return o.Error.Match(odata.ErrorUnknownUnsupportedQuery)
}
return false
}
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
ConsistencyFailureFunc: unknownError,
DisablePaging: query.Top > 0,
OData: query,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: "/auditLogs/signIns",
},
})
if err != nil {
return nil, status, fmt.Errorf("SignInLogsClient.BaseClient.Get(): %v", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var data struct {
SignInLogs []SignInReport `json:"value"`
}
if err := json.Unmarshal(respBody, &data); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &data.SignInLogs, status, nil
}
// Get retrieves a Sign-in Report.
func (c *SignInReportsClient) Get(ctx context.Context, id string, query odata.Query) (*SignInReport, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
OData: query,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/auditLogs/signIns/%s", id),
},
})
if err != nil {
return nil, status, fmt.Errorf("SignInLogsClient.BaseClient.Get(): %v", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var signInReport SignInReport
if err := json.Unmarshal(respBody, &signInReport); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &signInReport, status, nil
}