Skip to content
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

SDK-2254:go-create-qr-code added #287

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions digital_identity_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
38 changes: 38 additions & 0 deletions digitalidentity/create_qr_code_builder.go
Original file line number Diff line number Diff line change
@@ -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,
})
}
73 changes: 73 additions & 0 deletions digitalidentity/create_qr_code_builder_test.go
Original file line number Diff line number Diff line change
@@ -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"}
}
36 changes: 36 additions & 0 deletions digitalidentity/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Loading