-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Discovery: DID registration by clients
- Loading branch information
Showing
11 changed files
with
924 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,40 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"github.com/nuts-foundation/go-did/vc" | ||
"github.com/nuts-foundation/nuts-node/core" | ||
"net/http" | ||
) | ||
|
||
// Invoker is the interface for the client that invokes the remote Discovery Service. | ||
type Invoker interface { | ||
// Register registers a Verifiable Presentation on the remote Discovery Service. | ||
Register(ctx context.Context, serviceEndpointURL string, presentation vc.VerifiablePresentation) error | ||
} | ||
|
||
var _ Invoker = &HTTPInvoker{} | ||
|
||
// HTTPInvoker is a | ||
type HTTPInvoker struct { | ||
client core.HTTPRequestDoer | ||
} | ||
|
||
func (h HTTPInvoker) Register(ctx context.Context, serviceEndpointURL string, presentation vc.VerifiablePresentation) error { | ||
requestBody, _ := presentation.MarshalJSON() | ||
httpRequest, err := http.NewRequestWithContext(ctx, http.MethodPost, serviceEndpointURL, bytes.NewReader(requestBody)) | ||
if err != nil { | ||
return err | ||
} | ||
httpResponse, err := h.client.Do(httpRequest) | ||
if err != nil { | ||
return fmt.Errorf("failed to invoke remote Discovery Service (url=%s): %w", serviceEndpointURL, err) | ||
} | ||
defer httpResponse.Body.Close() | ||
if err := core.TestResponseCode(201, httpResponse); err != nil { | ||
return fmt.Errorf("non-OK response from remote Discovery Service (url=%s): %w", serviceEndpointURL, err) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.