Skip to content

Commit 576dfab

Browse files
committed
Simplify credential exchange function signatures. Auto fill trace parameter.
1 parent 2fad541 commit 576dfab

File tree

9 files changed

+251
-142
lines changed

9 files changed

+251
-142
lines changed

client.go

+34-11
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,60 @@ import (
1111
)
1212

1313
type Client struct {
14-
ACApyURL string
15-
APIKey string
16-
HTTPClient http.Client
14+
acapyURL string
15+
apiKey string
16+
tracing bool
17+
preserveExchangeRecords bool
18+
autoRespondCredentialOffer bool
19+
HTTPClient http.Client
1720
}
1821

1922
func NewClient(acapyURL string) *Client {
2023
return &Client{
21-
ACApyURL: strings.TrimRight(acapyURL, "/"),
24+
acapyURL: strings.TrimRight(acapyURL, "/"),
2225
HTTPClient: http.Client{},
2326
}
2427
}
2528

2629
func (c *Client) SetAPIKey(apiKey string) *Client {
27-
c.APIKey = apiKey
30+
c.apiKey = apiKey
31+
return c
32+
}
33+
34+
func (c *Client) EnableTracing() *Client {
35+
c.tracing = true
36+
return c
37+
}
38+
39+
func (c *Client) DisableTracing() *Client {
40+
c.tracing = false
41+
return c
42+
}
43+
44+
func (c *Client) PreserveExchangeRecords() *Client {
45+
c.preserveExchangeRecords = true
46+
return c
47+
}
48+
49+
func (c *Client) AutoRespondCredentialOffer() *Client {
50+
c.autoRespondCredentialOffer = true
2851
return c
2952
}
3053

3154
func (c *Client) post(path string, queryParam map[string]string, body interface{}, response interface{}) error {
32-
return c.request(http.MethodPost, c.ACApyURL+path, queryParam, body, response)
55+
return c.request(http.MethodPost, c.acapyURL+path, queryParam, body, response)
3356
}
3457

3558
func (c *Client) get(path string, queryParams map[string]string, response interface{}) error {
36-
return c.request(http.MethodGet, c.ACApyURL+path, queryParams, nil, response)
59+
return c.request(http.MethodGet, c.acapyURL+path, queryParams, nil, response)
3760
}
3861

3962
func (c *Client) patch(path string, queryParams map[string]string, body interface{}, response interface{}) error {
40-
return c.request(http.MethodPatch, c.ACApyURL+path, queryParams, body, response)
63+
return c.request(http.MethodPatch, c.acapyURL+path, queryParams, body, response)
4164
}
4265

4366
func (c *Client) put(path string) error {
44-
return c.request(http.MethodPut, c.ACApyURL+path, nil, nil, nil)
67+
return c.request(http.MethodPut, c.acapyURL+path, nil, nil, nil)
4568
}
4669

4770
func (c *Client) delete(url string) error {
@@ -64,8 +87,8 @@ func (c *Client) request(method string, url string, queryParams map[string]strin
6487
if err != nil {
6588
return err
6689
}
67-
if c.APIKey != "" {
68-
r.Header.Add("X-API-KEY", c.APIKey)
90+
if c.apiKey != "" {
91+
r.Header.Add("X-API-KEY", c.apiKey)
6992
}
7093
r.Header.Add("Content-Type", "application/json")
7194

credential_exchange.go

+137-50
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
)
66

7-
type CredentialExchange struct {
7+
type CredentialExchangeRecord struct {
88
CredentialExchangeID string `json:"credential_exchange_id"`
99
CredentialDefinitionID string `json:"credential_definition_id"`
1010
ConnectionID string `json:"connection_id"`
@@ -119,44 +119,90 @@ type CredentialPreviewAttribute struct {
119119
Value string `json:"value"`
120120
}
121121

122-
type CredentialOfferRequest struct {
123-
CredentialDefinitionID string `json:"cred_def_id"`
124-
ConnectionID string `json:"connection_id"`
125-
CredentialPreview CredentialPreview `json:"credential_preview"`
126-
Comment string `json:"comment"`
127-
Trace bool `json:"trace"`
128-
AutoRemove bool `json:"auto_remove"`
129-
AutoIssue bool `json:"auto_issue"`
130-
}
131-
132-
type CredentialProposalRequest struct {
122+
type credentialProposalRequest struct {
133123
CredentialDefinitionID string `json:"cred_def_id"`
134124
ConnectionID string `json:"connection_id"`
135125
IssuerDID string `json:"issuer_did"`
136126
Comment string `json:"comment"`
137127
CredentialPreview CredentialPreview `json:"credential_proposal"`
138-
SchemaName string `json:"schema_name"`
139-
SchemaVersion string `json:"schema_version"`
140128
SchemaID string `json:"schema_id"`
141-
SchemaIssuerDID string `json:"schema_issuer_did"`
142-
Trace bool `json:"trace"`
143-
AutoRemove bool `json:"auto_remove"`
129+
130+
// filled automatically
131+
SchemaName string `json:"schema_name"`
132+
SchemaVersion string `json:"schema_version"`
133+
SchemaIssuerDID string `json:"schema_issuer_did"`
134+
Trace bool `json:"trace"`
135+
AutoRemove bool `json:"auto_remove"`
144136
}
145137

146-
func (c *Client) SendCredentialProposal(proposal CredentialProposalRequest) (CredentialExchange, error) {
147-
var credentialExchange CredentialExchange
148-
err := c.post("/issue-credential/send-proposal", nil, proposal, &credentialExchange)
138+
func (c *Client) SendCredentialProposal(
139+
credentialDefinitionID string,
140+
connectionID string,
141+
issuerDID string,
142+
comment string,
143+
credentialPreview CredentialPreview,
144+
schemaID string,
145+
) (CredentialExchangeRecord, error) {
146+
147+
var response CredentialExchangeRecord
148+
var schemaIssuerDID, _, schemaName, schemaVersion, err = SchemaIDToParts(schemaID)
149149
if err != nil {
150-
return CredentialExchange{}, err
150+
return CredentialExchangeRecord{}, err
151151
}
152-
return credentialExchange, nil
152+
153+
var request = credentialProposalRequest{
154+
CredentialDefinitionID: credentialDefinitionID,
155+
ConnectionID: connectionID,
156+
IssuerDID: issuerDID,
157+
Comment: comment,
158+
CredentialPreview: credentialPreview,
159+
SchemaID: schemaID,
160+
161+
SchemaName: schemaName,
162+
SchemaVersion: schemaVersion,
163+
SchemaIssuerDID: schemaIssuerDID,
164+
Trace: c.tracing,
165+
AutoRemove: !c.preserveExchangeRecords,
166+
}
167+
168+
err = c.post("/issue-credential/send-proposal", nil, request, &response)
169+
if err != nil {
170+
return CredentialExchangeRecord{}, err
171+
}
172+
return response, nil
153173
}
154174

155-
func (c *Client) SendCredentialOffer(offer CredentialOfferRequest) (CredentialExchange, error) {
156-
var credentialExchangeRecord CredentialExchange
175+
type credentialOfferRequest struct {
176+
CredentialDefinitionID string `json:"cred_def_id"`
177+
ConnectionID string `json:"connection_id"`
178+
CredentialPreview CredentialPreview `json:"credential_preview"`
179+
Comment string `json:"comment"`
180+
181+
// filled automatically
182+
Trace bool `json:"trace"`
183+
AutoRemove bool `json:"auto_remove"`
184+
AutoIssue bool `json:"auto_issue"`
185+
}
186+
187+
func (c *Client) SendCredentialOffer(
188+
credentialDefinitionID string,
189+
connectionID string,
190+
credentialPreview CredentialPreview,
191+
comment string,
192+
) (CredentialExchangeRecord, error) {
193+
var offer = credentialOfferRequest{
194+
CredentialDefinitionID: credentialDefinitionID,
195+
ConnectionID: connectionID,
196+
CredentialPreview: credentialPreview,
197+
Comment: comment,
198+
Trace: c.tracing,
199+
AutoRemove: !c.preserveExchangeRecords,
200+
AutoIssue: c.autoRespondCredentialOffer,
201+
}
202+
var credentialExchangeRecord CredentialExchangeRecord
157203
err := c.post("/issue-credential/send-offer", nil, offer, &credentialExchangeRecord)
158204
if err != nil {
159-
return CredentialExchange{}, err
205+
return CredentialExchangeRecord{}, err
160206
}
161207
return credentialExchangeRecord, nil
162208
}
@@ -168,9 +214,9 @@ type QueryCredentialExchangeParams struct {
168214
ThreadID string `json:"thread_id"`
169215
}
170216

171-
func (c *Client) QueryCredentialExchange(params QueryCredentialExchangeParams) ([]CredentialExchange, error) {
217+
func (c *Client) QueryCredentialExchange(params QueryCredentialExchangeParams) ([]CredentialExchangeRecord, error) {
172218
var result = struct {
173-
Results []CredentialExchange `json:"result"`
219+
Results []CredentialExchangeRecord `json:"result"`
174220
}{}
175221
var queryParams = map[string]string{
176222
"connection_id": params.ConnectionID,
@@ -185,11 +231,11 @@ func (c *Client) QueryCredentialExchange(params QueryCredentialExchangeParams) (
185231
return result.Results, nil
186232
}
187233

188-
func (c *Client) GetCredentialExchange(credentialExchangeID string) (CredentialExchange, error) {
189-
var credentialExchange CredentialExchange
234+
func (c *Client) GetCredentialExchange(credentialExchangeID string) (CredentialExchangeRecord, error) {
235+
var credentialExchange CredentialExchangeRecord
190236
err := c.get(fmt.Sprintf("/issue-credential/records/%s", credentialExchangeID), nil, &credentialExchange)
191237
if err != nil {
192-
return CredentialExchange{}, err
238+
return CredentialExchangeRecord{}, err
193239
}
194240
return credentialExchange, nil
195241
}
@@ -207,69 +253,110 @@ type CredentialCreateRequest struct {
207253
AutoRemove bool `json:"auto_remove"`
208254
}
209255

210-
func (c *Client) CreateCredentialExchange(request CredentialCreateRequest) (CredentialExchange, error) {
211-
var credentialExchange CredentialExchange
256+
func (c *Client) CreateCredentialExchange(request CredentialCreateRequest) (CredentialExchangeRecord, error) {
257+
var credentialExchange CredentialExchangeRecord
212258
err := c.post("/issue-credential/create", nil, request, &credentialExchange)
213259
if err != nil {
214-
return CredentialExchange{}, err
260+
return CredentialExchangeRecord{}, err
215261
}
216262
return credentialExchange, nil
217263
}
218264

219-
type CredentialSendRequest CredentialProposalRequest
265+
type credentialSendRequest struct {
266+
CredentialDefinitionID string `json:"cred_def_id"`
267+
ConnectionID string `json:"connection_id"`
268+
IssuerDID string `json:"issuer_did"`
269+
Comment string `json:"comment"`
270+
CredentialPreview CredentialPreview `json:"credential_proposal"`
271+
SchemaID string `json:"schema_id"`
272+
273+
// filled automatically
274+
SchemaName string `json:"schema_name"`
275+
SchemaVersion string `json:"schema_version"`
276+
SchemaIssuerDID string `json:"schema_issuer_did"`
277+
Trace bool `json:"trace"`
278+
AutoRemove bool `json:"auto_remove"`
279+
}
280+
281+
func (c *Client) SendCredential(credentialDefinitionID string,
282+
connectionID string,
283+
issuerDID string,
284+
comment string,
285+
credentialPreview CredentialPreview,
286+
schemaID string,
287+
) (CredentialExchangeRecord, error) {
288+
289+
var credentialExchange CredentialExchangeRecord
290+
var schemaIssuerDID, _, schemaName, schemaVersion, err = SchemaIDToParts(schemaID)
291+
if err != nil {
292+
return CredentialExchangeRecord{}, err
293+
}
294+
295+
var request = credentialSendRequest{
296+
CredentialDefinitionID: credentialDefinitionID,
297+
ConnectionID: connectionID,
298+
IssuerDID: issuerDID,
299+
Comment: comment,
300+
CredentialPreview: credentialPreview,
301+
SchemaID: schemaID,
302+
303+
SchemaName: schemaName,
304+
SchemaVersion: schemaVersion,
305+
SchemaIssuerDID: schemaIssuerDID,
306+
Trace: c.tracing,
307+
AutoRemove: !c.preserveExchangeRecords,
308+
}
220309

221-
func (c *Client) SendCredential(request CredentialSendRequest) (CredentialExchange, error) {
222-
var credentialExchange CredentialExchange
223-
err := c.post("/issue-credential/send", nil, request, &credentialExchange)
310+
err = c.post("/issue-credential/send", nil, request, &credentialExchange)
224311
if err != nil {
225-
return CredentialExchange{}, err
312+
return CredentialExchangeRecord{}, err
226313
}
227314
return credentialExchange, nil
228315
}
229316

230-
func (c *Client) SendCredentialOfferByID(credentialExchangeID string) (CredentialExchange, error) {
231-
var credentialExchange CredentialExchange
317+
func (c *Client) SendCredentialOfferByID(credentialExchangeID string) (CredentialExchangeRecord, error) {
318+
var credentialExchange CredentialExchangeRecord
232319
err := c.post(fmt.Sprintf("/issue-credential/records/%s/send-offer", credentialExchangeID), nil, nil, &credentialExchange)
233320
if err != nil {
234-
return CredentialExchange{}, err
321+
return CredentialExchangeRecord{}, err
235322
}
236323
return credentialExchange, nil
237324
}
238325

239-
func (c *Client) SendCredentialRequestByID(credentialExchangeID string) (CredentialExchange, error) {
240-
var credentialExchange CredentialExchange
326+
func (c *Client) SendCredentialRequestByID(credentialExchangeID string) (CredentialExchangeRecord, error) {
327+
var credentialExchange CredentialExchangeRecord
241328
err := c.post(fmt.Sprintf("/issue-credential/records/%s/send-request", credentialExchangeID), nil, nil, &credentialExchange)
242329
if err != nil {
243-
return CredentialExchange{}, err
330+
return CredentialExchangeRecord{}, err
244331
}
245332
return credentialExchange, nil
246333
}
247334

248-
func (c *Client) IssueCredentialByID(credentialExchangeID string, comment string) (CredentialExchange, error) {
249-
var credentialExchange CredentialExchange
335+
func (c *Client) IssueCredentialByID(credentialExchangeID string, comment string) (CredentialExchangeRecord, error) {
336+
var credentialExchange CredentialExchangeRecord
250337
var body = struct {
251338
Comment string `json:"comment"`
252339
}{
253340
Comment: comment,
254341
}
255342
err := c.post(fmt.Sprintf("/issue-credential/records/%s/issue", credentialExchangeID), nil, body, &credentialExchange)
256343
if err != nil {
257-
return CredentialExchange{}, err
344+
return CredentialExchangeRecord{}, err
258345
}
259346
return credentialExchange, nil
260347
}
261348

262349
// credentialID is optional: https://github.com/hyperledger/aries-cloudagent-python/issues/594#issuecomment-656113125
263-
func (c *Client) StoreCredentialByID(credentialExchangeID string, credentialID string) (CredentialExchange, error) {
264-
var credentialExchange CredentialExchange
350+
func (c *Client) StoreCredentialByID(credentialExchangeID string, credentialID string) (CredentialExchangeRecord, error) {
351+
var credentialExchange CredentialExchangeRecord
265352
var body = struct {
266353
CredentialID string `json:"credential_id"`
267354
}{
268355
CredentialID: credentialID,
269356
}
270357
err := c.post(fmt.Sprintf("/issue-credential/records/%s/store", credentialExchangeID), nil, body, &credentialExchange)
271358
if err != nil {
272-
return CredentialExchange{}, err
359+
return CredentialExchangeRecord{}, err
273360
}
274361
return credentialExchange, nil
275362
}

0 commit comments

Comments
 (0)