-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for B2CUserFlow CRUD operations #179
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c198774
Add support for B2CUserFlow CRUD operations
jarifibrahim 2e885d6
minor fixes
jarifibrahim d2d750f
Use string for userflow type
jarifibrahim 0000795
minor fixup
jarifibrahim e4be3b4
Remove ;qa file
jarifibrahim 1764468
Merge branch 'main' into userflow-support
jarifibrahim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package msgraph | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/manicminer/hamilton/odata" | ||
) | ||
|
||
// B2CUserFlowClient performs operations on Users. | ||
type B2CUserFlowClient struct { | ||
BaseClient Client | ||
} | ||
|
||
// NewB2CUserFlowClient returns a new B2CUserFlowClient. | ||
func NewB2CUserFlowClient(tenantId string) *B2CUserFlowClient { | ||
return &B2CUserFlowClient{ | ||
BaseClient: NewClient(VersionBeta, tenantId), | ||
} | ||
} | ||
|
||
// List returns a list of B2C UserFlows, optionally queried using OData. | ||
func (c *B2CUserFlowClient) List(ctx context.Context, query odata.Query) (*[]B2CUserFlow, int, error) { | ||
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{ | ||
OData: query, | ||
ValidStatusCodes: []int{http.StatusOK}, | ||
Uri: Uri{ | ||
Entity: "/identity/b2cUserFlows", | ||
HasTenantId: true, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, status, fmt.Errorf("B2CUserFlowClient.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 { | ||
UserFlows []B2CUserFlow `json:"value"` | ||
} | ||
if err := json.Unmarshal(respBody, &data); err != nil { | ||
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err) | ||
} | ||
|
||
return &data.UserFlows, status, nil | ||
} | ||
|
||
// Create creates a new B2CUserFlow. | ||
func (c *B2CUserFlowClient) Create(ctx context.Context, userflow B2CUserFlow) (*B2CUserFlow, int, error) { | ||
var status int | ||
|
||
body, err := json.Marshal(userflow) | ||
if err != nil { | ||
return nil, status, fmt.Errorf("json.Marshal(): %v", err) | ||
} | ||
|
||
resp, status, _, err := c.BaseClient.Post(ctx, PostHttpRequestInput{ | ||
Body: body, | ||
OData: odata.Query{ | ||
Metadata: odata.MetadataFull, | ||
}, | ||
ValidStatusCodes: []int{http.StatusCreated}, | ||
Uri: Uri{ | ||
Entity: "/identity/b2cUserFlows", | ||
HasTenantId: true, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, status, fmt.Errorf("B2CUserFlowClient.BaseClient.Post(): %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 newUserFlow B2CUserFlow | ||
if err := json.Unmarshal(respBody, &newUserFlow); err != nil { | ||
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err) | ||
} | ||
|
||
return &newUserFlow, status, nil | ||
} | ||
|
||
func (c *B2CUserFlowClient) Get(ctx context.Context, id string, query odata.Query) (*B2CUserFlow, int, error) { | ||
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{ | ||
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc, | ||
OData: query, | ||
ValidStatusCodes: []int{http.StatusOK}, | ||
Uri: Uri{ | ||
Entity: fmt.Sprintf("/identity/b2cUserFlows/%s", id), | ||
HasTenantId: true, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, status, fmt.Errorf("B2CUserFlowClient.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 userflow B2CUserFlow | ||
if err := json.Unmarshal(respBody, &userflow); err != nil { | ||
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err) | ||
} | ||
|
||
return &userflow, status, nil | ||
} | ||
|
||
// Update amends an existing B2CUserFlow. | ||
func (c *B2CUserFlowClient) Update(ctx context.Context, userflow B2CUserFlow) (int, error) { | ||
var status int | ||
if userflow.ID == nil { | ||
return status, fmt.Errorf("cannot update userflow with nil ID") | ||
} | ||
|
||
userflowID := *userflow.ID | ||
userflow.ID = nil | ||
|
||
body, err := json.Marshal(userflow) | ||
if err != nil { | ||
return status, fmt.Errorf("json.Marshal(): %v", err) | ||
} | ||
|
||
_, status, _, err = c.BaseClient.Patch(ctx, PatchHttpRequestInput{ | ||
Body: body, | ||
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc, | ||
ValidStatusCodes: []int{ | ||
http.StatusOK, | ||
http.StatusNoContent, | ||
}, | ||
Uri: Uri{ | ||
Entity: fmt.Sprintf("/identity/b2cUserFlows//%s", userflowID), | ||
HasTenantId: true, | ||
}, | ||
}) | ||
if err != nil { | ||
return status, fmt.Errorf("B2CUserFlowClient.BaseClient.Patch(): %v", err) | ||
} | ||
|
||
return status, nil | ||
} | ||
|
||
// Delete removes a B2CUserFlow. | ||
func (c *B2CUserFlowClient) Delete(ctx context.Context, id string) (int, error) { | ||
_, status, _, err := c.BaseClient.Delete(ctx, DeleteHttpRequestInput{ | ||
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc, | ||
ValidStatusCodes: []int{http.StatusNoContent}, | ||
Uri: Uri{ | ||
Entity: fmt.Sprintf("/identity/b2cUserFlows/%s", id), | ||
HasTenantId: true, | ||
}, | ||
}) | ||
if err != nil { | ||
return status, fmt.Errorf("B2CUserFlowClient.BaseClient.Delete(): %v", err) | ||
} | ||
|
||
return status, nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package msgraph_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/manicminer/hamilton/internal/test" | ||
"github.com/manicminer/hamilton/internal/utils" | ||
"github.com/manicminer/hamilton/msgraph" | ||
"github.com/manicminer/hamilton/odata" | ||
) | ||
|
||
func TestB2CUserFlowClient(t *testing.T) { | ||
c := test.NewTest(t) | ||
defer c.CancelFunc() | ||
|
||
userFlowType := msgraph.SIGNUP_USERFLOWTYPE | ||
userflow := testB2CUserFlowClient_Create(t, c, msgraph.B2CUserFlow{ | ||
ID: utils.StringPtr("test b2c user flow"), | ||
UserFlowType: &userFlowType, | ||
UserFlowTypeVersion: utils.Float32Ptr(3.0), | ||
}) | ||
testB2CUserFlowClient_Get(t, c, *userflow.ID) | ||
userflow.DefaultLanguageTag = utils.StringPtr("en") | ||
testB2CUserFlowClient_Update(t, c, *userflow) | ||
testB2CUserFlowClient_List(t, c) | ||
testGroupsClient_Delete(t, c, *userflow.ID) | ||
} | ||
|
||
func testB2CUserFlowClient_Create(t *testing.T, c *test.Test, u msgraph.B2CUserFlow) *msgraph.B2CUserFlow { | ||
userflow, status, err := c.B2CUserFlowClient.Create(c.Context, u) | ||
if err != nil { | ||
t.Fatalf("B2CUserFlowclient.Create(): %v", err) | ||
} | ||
if status < 200 || status >= 300 { | ||
t.Fatalf("B2CUserFlowClient.Create(): invalid status: %d", status) | ||
} | ||
if userflow == nil { | ||
t.Fatal("B2CUserFlowClient.Create(): userflow was nil") | ||
} | ||
if userflow.ID == nil { | ||
t.Fatal("B2CUserFlowClient.Create(): userflow.ID was nil") | ||
} | ||
return userflow | ||
} | ||
|
||
func testB2CUserFlowClient_Get(t *testing.T, c *test.Test, id string) *msgraph.B2CUserFlow { | ||
userflow, status, err := c.B2CUserFlowClient.Get(c.Context, id, odata.Query{}) | ||
if err != nil { | ||
t.Fatalf("B2CUserFlowClient.Get(): %v", err) | ||
} | ||
if status < 200 || status >= 300 { | ||
t.Fatalf("B2CUserFlowClient.Get(): invalid status: %d", status) | ||
} | ||
if userflow == nil { | ||
t.Fatal("B2CUserFlowClient.Get(): userflow was nil") | ||
} | ||
return userflow | ||
} | ||
|
||
func testB2CUserFlowClient_List(t *testing.T, c *test.Test) *[]msgraph.B2CUserFlow { | ||
userflows, _, err := c.B2CUserFlowClient.List(c.Context, odata.Query{Top: 10}) | ||
if err != nil { | ||
t.Fatalf("B2CUserFlowClient.List(): %v", err) | ||
} | ||
if userflows == nil { | ||
t.Fatal("B2CUserFlowClient.List(): userflows was nil") | ||
} | ||
return userflows | ||
} | ||
|
||
func testB2CUserFlowClient_Update(t *testing.T, c *test.Test, u msgraph.B2CUserFlow) { | ||
status, err := c.B2CUserFlowClient.Update(c.Context, u) | ||
if err != nil { | ||
t.Fatalf("B2CUserFlowClient.Update(): %v", err) | ||
} | ||
if status < 200 || status >= 300 { | ||
t.Fatalf("B2CUserFlowClient.Update(): invalid status: %d", status) | ||
} | ||
} |
This file contains 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
B2cUserFlow has a bunch of fields https://learn.microsoft.com/en-us/graph/api/resources/b2cidentityuserflow?view=graph-rest-beta . I've added only the minimal ones.
I can add more fields if required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will want to add these in time, but I'm happy to merge with these fields for now.