From 38848d166e45c8bae77e479ea4f086984f11871a Mon Sep 17 00:00:00 2001 From: mehmet-yoti Date: Tue, 8 Aug 2023 13:23:11 +0100 Subject: [PATCH] SDK-2254:go-create-qr-code added --- digital_identity_client.go | 5 ++ digitalidentity/create_qr_code_builder.go | 38 ++++++++++ .../create_qr_code_builder_test.go | 73 +++++++++++++++++++ digitalidentity/service.go | 36 +++++++++ 4 files changed, 152 insertions(+) create mode 100644 digitalidentity/create_qr_code_builder.go create mode 100644 digitalidentity/create_qr_code_builder_test.go diff --git a/digital_identity_client.go b/digital_identity_client.go index 971ef352..63f01548 100644 --- a/digital_identity_client.go +++ b/digital_identity_client.go @@ -71,3 +71,8 @@ func (client *DigitalIdentityClient) CreateShareSession(shareSession *digitalide func (client *DigitalIdentityClient) GetSession(sessionID string) (share digitalidentity.ShareSessionResult, err error) { return digitalidentity.GetSession(client.HTTPClient, sessionID, client.GetSdkID(), client.getAPIURL(), client.Key) } + +// Create a sharing session QR code to initiate a sharing process based on a policy +func (client *DigitalIdentityClient) CreateShareQrCode(sessionID string) (share digitalidentity.CreateShareQrCodeResult, err error) { + return digitalidentity.CreateShareQrCode(client.HTTPClient, sessionID, client.GetSdkID(), client.getAPIURL(), client.Key) +} diff --git a/digitalidentity/create_qr_code_builder.go b/digitalidentity/create_qr_code_builder.go new file mode 100644 index 00000000..d0ddde4b --- /dev/null +++ b/digitalidentity/create_qr_code_builder.go @@ -0,0 +1,38 @@ +package digitalidentity + +import ( + "encoding/json" +) + +// ShareSessionBuilder builds a session +type CreateShareQrCodeBuilder struct { + createShareQrCode CreateShareQrCodeResult + err error +} + +// ShareSession represents a sharesession +type CreateShareQrCodeResult struct { + id string + uri string +} + +// func (b *CreateShareQrCodeBuilder) withBaseUrl(baseUrl string) *CreateShareQrCodeBuilder { +// b.baseURL = baseUrl +// return b +// } + +// Build constructs the ShareSession +func (builder *CreateShareQrCodeBuilder) Build() (CreateShareQrCodeResult, error) { + return builder.createShareQrCode, builder.err +} + +// MarshalJSON returns the JSON encoding +func (createShareQrCode CreateShareQrCodeResult) MarshalJSON() ([]byte, error) { + return json.Marshal(&struct { + Id string `json:"id"` + Uri string `json:"uri"` + }{ + Id: createShareQrCode.id, + Uri: createShareQrCode.uri, + }) +} diff --git a/digitalidentity/create_qr_code_builder_test.go b/digitalidentity/create_qr_code_builder_test.go new file mode 100644 index 00000000..f8c0c4aa --- /dev/null +++ b/digitalidentity/create_qr_code_builder_test.go @@ -0,0 +1,73 @@ +package digitalidentity + +import ( + "encoding/json" + "fmt" + "testing" +) + +func TestCreateShareQrCodeBuilder_Build(t *testing.T) { + expectedID := "SOME_ID" + expectedURI := "https://example.com/qr" + + builder := CreateShareQrCodeBuilder{ + createShareQrCode: CreateShareQrCodeResult{ + id: expectedID, + uri: expectedURI, + }, + } + + result, err := builder.Build() + + if err != nil { + t.Errorf("Expected no error, but got: %v", err) + } + if result.id != expectedID { + t.Errorf("Expected ID: %s, but got: %s", expectedID, result.id) + } + if result.uri != expectedURI { + t.Errorf("Expected URI: %s, but got: %s", expectedURI, result.uri) + } +} + +func TestCreateShareQrCode_MarshalJSON(t *testing.T) { + createShareQrCode := CreateShareQrCodeResult{ + id: "SOME_ID", + uri: "https://example.com/qr", + } + + expectedJSON := `{"id":"SOME_ID","uri":"https://example.com/qr"}` + + resultJSON, err := json.Marshal(createShareQrCode) + if err != nil { + t.Errorf("Expected no error, but got: %v", err) + } + if string(resultJSON) != expectedJSON { + t.Errorf("Expected JSON:\n%s\nbut got:\n%s\n", expectedJSON, string(resultJSON)) + } +} + +func ExampleCreateShareQrCodeBuilder_Build() { + builder := CreateShareQrCodeBuilder{ + createShareQrCode: CreateShareQrCodeResult{ + id: "SOME_ID", + uri: "https://example.com/qr", + }, + } + + result, err := builder.Build() + + if err != nil { + fmt.Printf("Error: %v\n", err) + return + } + + data, err := json.Marshal(result) + if err != nil { + fmt.Printf("Error: %v\n", err) + return + } + + fmt.Println(string(data)) + // Output: {"id":"SOME_ID","uri":"https://example.com/qr"} +} diff --git a/digitalidentity/service.go b/digitalidentity/service.go index 74b71b49..9e4afb66 100644 --- a/digitalidentity/service.go +++ b/digitalidentity/service.go @@ -12,6 +12,10 @@ import ( const identitySesssionCreationEndpoint = "v2/sessions" const identitySessionRetrieval = "v2/sessions/%s" +const identitySessionQrCodeCreation = "/v2/sessions/%s/qr-codes" +const identitySessionQrCodeRetrieval = "/v2/qr-codes/%s" +const identitySessionReceiptRetrieval = "/v2/receipts/%s" +const identitySessionReceiptKeyRetrieval = "/v2/wrapped-item-keys/%s" // SessionResult contains the information about a created session type SessionResult struct { @@ -91,3 +95,35 @@ func GetSession(httpClient requests.HttpClient, sessionID string, clientSdkId, a return share, err } + +// GetSession get session info using the supplied sessionID +func CreateShareQrCode(httpClient requests.HttpClient, sessionID string, clientSdkId, apiUrl string, key *rsa.PrivateKey) (qrCode CreateShareQrCodeResult, err error) { + endpoint := identitySessionQrCodeCreation + headers := requests.AuthHeader(clientSdkId) + request, err := requests.SignedRequest{ + Key: key, + HTTPMethod: http.MethodPost, + BaseURL: apiUrl, + Endpoint: endpoint, + Headers: headers, + Body: nil, + }.Request() + if err != nil { + return qrCode, err + } + + response, err := requests.Execute(httpClient, request, ShareURLHTTPErrorMessages, yotierror.DefaultHTTPErrorMessages) + if err != nil { + return qrCode, err + } + defer response.Body.Close() + + responseBytes, err := io.ReadAll(response.Body) + if err != nil { + return qrCode, err + } + + err = json.Unmarshal(responseBytes, &qrCode) + + return qrCode, err +}