forked from sourcenetwork/defradb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR(ACP): Add HTTP Client For Adding Policy Command
- Loading branch information
1 parent
f3cdc1b
commit 7b586d7
Showing
5 changed files
with
93 additions
and
2 deletions.
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,75 @@ | ||
// Copyright 2024 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package http | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
"github.com/sourcenetwork/defradb/client" | ||
) | ||
|
||
type acpHandler struct{} | ||
|
||
func (s *acpHandler) AddPolicy(rw http.ResponseWriter, req *http.Request) { | ||
db, ok := req.Context().Value(dbContextKey).(client.DB) | ||
if !ok { | ||
responseJSON(rw, http.StatusBadRequest, errorResponse{NewErrFailedToGetContext("db")}) | ||
return | ||
} | ||
|
||
var addPolicyRequest AddPolicyRequest | ||
if err := requestJSON(req, &addPolicyRequest); err != nil { | ||
responseJSON(rw, http.StatusBadRequest, errorResponse{err}) | ||
return | ||
} | ||
|
||
addPolicyResult, err := db.AddPolicy( | ||
req.Context(), | ||
addPolicyRequest.Identity, | ||
addPolicyRequest.Policy, | ||
) | ||
if err != nil { | ||
responseJSON(rw, http.StatusBadRequest, errorResponse{err}) | ||
return | ||
} | ||
|
||
responseJSON(rw, http.StatusOK, addPolicyResult) | ||
} | ||
|
||
func (h *acpHandler) bindRoutes(router *Router) { | ||
successResponse := &openapi3.ResponseRef{ | ||
Ref: "#/components/responses/success", | ||
} | ||
errorResponse := &openapi3.ResponseRef{ | ||
Ref: "#/components/responses/error", | ||
} | ||
acpAddPolicySchema := &openapi3.SchemaRef{ | ||
Ref: "#/components/schemas/add_policy_request", | ||
} | ||
|
||
acpAddPolicyRequest := openapi3.NewRequestBody(). | ||
WithRequired(true). | ||
WithJSONSchemaRef(acpAddPolicySchema) | ||
|
||
acpAddPolicy := openapi3.NewOperation() | ||
acpAddPolicy.OperationID = "add policy" | ||
acpAddPolicy.Description = "Add a policy using acp module" | ||
acpAddPolicy.Tags = []string{"acp_policy"} | ||
acpAddPolicy.Responses = openapi3.NewResponses() | ||
acpAddPolicy.Responses.Set("200", successResponse) | ||
acpAddPolicy.Responses.Set("400", errorResponse) | ||
acpAddPolicy.RequestBody = &openapi3.RequestBodyRef{ | ||
Value: acpAddPolicyRequest, | ||
} | ||
|
||
router.AddRoute("/acp/policy", http.MethodPost, acpAddPolicy, h.AddPolicy) | ||
} |
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