-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9e6b805
Showing
13 changed files
with
653 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Laurence de Jong | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,3 @@ | ||
# go-acapy-client | ||
|
||
A library for interacting with ACA-py in Go. |
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,69 @@ | ||
package acapy | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type Client struct { | ||
LedgerURL string | ||
AcapyURL string | ||
|
||
HTTPClient http.Client | ||
} | ||
|
||
func NewClient(ledgerURL string, acapyURL string) *Client { | ||
return &Client{ | ||
LedgerURL: ledgerURL, | ||
AcapyURL: acapyURL, | ||
HTTPClient: http.Client{}, | ||
} | ||
} | ||
|
||
func (c *Client) post(url string, queryParam map[string]string, body interface{}, response interface{}) error { | ||
return c.request(http.MethodPost, url, queryParam, body, response) | ||
} | ||
|
||
func (c *Client) get(url string, queryParams map[string]string, response interface{}) error { | ||
return c.request(http.MethodGet, url, queryParams, nil, response) | ||
} | ||
|
||
func (c *Client) request(method string, url string, queryParams map[string]string, body interface{}, response interface{}) error { | ||
var input io.Reader | ||
var err error | ||
|
||
if body != nil { | ||
jsonInput, err := json.Marshal(body) | ||
if err != nil { | ||
return err | ||
} | ||
input = bytes.NewReader(jsonInput) | ||
} | ||
|
||
r, err := http.NewRequest(method, url, input) | ||
if err != nil { | ||
return err | ||
} | ||
r.Header.Add("Content-Type", "application/json") | ||
|
||
q := r.URL.Query() | ||
for k, v := range queryParams { | ||
if k != "" && v != "" { | ||
q.Add(k, v) | ||
} | ||
} | ||
r.URL.RawQuery = q.Encode() | ||
|
||
result, err := c.HTTPClient.Do(r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = json.NewDecoder(result.Body).Decode(response) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
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,16 @@ | ||
package acapy | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func (c *Client) SendBasicMessage(connectionID string, message string) error { | ||
type BasicMessage struct { | ||
Content string `json:"content"` | ||
} | ||
var basicMessage = BasicMessage{ | ||
Content: message, | ||
} | ||
|
||
return c.post(fmt.Sprintf("%s/connections/%s/send-message", c.AcapyURL, connectionID), nil, basicMessage, nil) | ||
} |
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,175 @@ | ||
package acapy | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
type CreateInvitationResponse struct { | ||
InvitationURL string `json:"invitation_url,omitempty"` | ||
ConnectionID string `json:"connection_id,omitempty"` | ||
Invitation struct { | ||
ImageURL string `json:"imageUrl,omitempty"` | ||
Label string `json:"label,omitempty"` | ||
ServiceEndpoint string `json:"serviceEndpoint,omitempty"` | ||
RecipientKeys []string `json:"recipientKeys,omitempty"` | ||
RoutingKeys []string `json:"routingKeys,omitempty"` | ||
ID string `json:"@id,omitempty"` | ||
DID string `json:"did,omitempty"` | ||
Type string `json:"@type,omitempty"` | ||
} `json:"invitation,omitempty"` | ||
} | ||
|
||
func (c *Client) CreateInvitation(alias string, autoAccept bool, multiUse bool, public bool) (CreateInvitationResponse, error) { | ||
var createInvitationResponse CreateInvitationResponse | ||
err := c.post(c.AcapyURL+"/connections/create-invitation", map[string]string{ | ||
"alias": alias, | ||
"auto_accept": strconv.FormatBool(autoAccept), | ||
"multi_use": strconv.FormatBool(multiUse), | ||
"public": strconv.FormatBool(public), | ||
}, nil, &createInvitationResponse) | ||
return createInvitationResponse, err | ||
} | ||
|
||
type ReceiveInvitationResponse struct { | ||
InboundConnectionID string `json:"inbound_connection_id,omitempty"` | ||
InvitationKey string `json:"invitation_key,omitempty"` | ||
MyDid string `json:"my_did,omitempty"` | ||
TheirDid string `json:"their_did,omitempty"` | ||
TheirRole string `json:"their_role,omitempty"` | ||
RequestID string `json:"request_id,omitempty"` | ||
State string `json:"state,omitempty"` | ||
ConnectionID string `json:"connection_id,omitempty"` | ||
Alias string `json:"alias,omitempty"` | ||
InvitationMode string `json:"invitation_mode,omitempty"` | ||
CreatedAt string `json:"created_at,omitempty"` | ||
Accept string `json:"accept,omitempty"` | ||
Initiator string `json:"initiator,omitempty"` | ||
ErrorMsg string `json:"error_msg,omitempty"` | ||
TheirLabel string `json:"their_label,omitempty"` | ||
RoutingState string `json:"routing_state,omitempty"` | ||
UpdatedAt string `json:"updated_at,omitempty"` | ||
} | ||
|
||
func (c *Client) ReceiveInvitation(invitation Invitation) (ReceiveInvitationResponse, error) { | ||
var receiveInvitationResponse ReceiveInvitationResponse | ||
|
||
err := c.post(c.AcapyURL+"/connections/receive-invitation", map[string]string{ | ||
"alias": invitation.Label, | ||
"auto_accept": strconv.FormatBool(false), | ||
}, invitation, &receiveInvitationResponse) | ||
return receiveInvitationResponse, err | ||
} | ||
|
||
type Invitation struct { | ||
ImageURL string `json:"imageUrl,omitempty"` | ||
Label string `json:"label,omitempty"` | ||
ServiceEndpoint string `json:"serviceEndpoint,omitempty"` | ||
RecipientKeys []string `json:"recipientKeys,omitempty"` | ||
RoutingKeys []string `json:"routingKeys,omitempty"` | ||
ID string `json:"@id,omitempty"` | ||
DID string `json:"did,omitempty"` | ||
} | ||
|
||
func (c *Client) AcceptInvitation(connectionID string) (Connection, error) { | ||
var connection Connection | ||
err := c.post(fmt.Sprintf("%s/connections/%s/accept-invitation", c.AcapyURL, connectionID), nil, nil, &connection) | ||
return connection, err | ||
} | ||
|
||
func (c *Client) AcceptRequest(connectionID string) (Connection, error) { | ||
var connection Connection | ||
err := c.post(fmt.Sprintf("%s/connections/%s/accept-request", c.AcapyURL, connectionID), nil, nil, &connection) | ||
return connection, err | ||
} | ||
|
||
type Connection struct { | ||
Accept string `json:"accept"` | ||
Alias string `json:"alias"` | ||
ConnectionID string `json:"connection_id"` | ||
CreatedAt string `json:"created_at"` | ||
ErrorMsg string `json:"error_msg"` | ||
InboundConnectionID string `json:"inbound_connection_id"` | ||
Initiator string `json:"initiator"` | ||
InvitationKey string `json:"invitation_key"` | ||
InvitationMode string `json:"invitation_mode"` | ||
MyDid string `json:"my_did"` | ||
RequestID string `json:"request_id"` | ||
RoutingState string `json:"routing_state"` | ||
State string `json:"state"` | ||
TheirDid string `json:"their_did"` | ||
TheirLabel string `json:"their_label"` | ||
TheirRole string `json:"their_role"` | ||
UpdatedAt string `json:"updated_at"` | ||
} | ||
|
||
// QueryConnectionsParams model | ||
// | ||
// Parameters for querying connections | ||
// | ||
type QueryConnectionsParams struct { | ||
|
||
// Alias of connection invitation | ||
Alias string `json:"alias,omitempty"` | ||
|
||
// Initiator is Connection invitation initiator | ||
Initiator string `json:"initiator,omitempty"` | ||
|
||
// Invitation key | ||
InvitationKey string `json:"invitation_key,omitempty"` | ||
|
||
// MyDID is DID of the agent | ||
MyDID string `json:"my_did,omitempty"` | ||
|
||
// State of the connection invitation | ||
State string `json:"state"` | ||
|
||
// TheirDID is other party's DID | ||
TheirDID string `json:"their_did,omitempty"` | ||
|
||
// TheirRole is other party's role | ||
TheirRole string `json:"their_role,omitempty"` | ||
} | ||
|
||
func (c *Client) QueryConnections(params QueryConnectionsParams) ([]Connection, error) { | ||
var connections = struct { | ||
Result []Connection `json:"results"` | ||
}{} | ||
|
||
var queryParams = map[string]string{ | ||
"alias": params.Alias, | ||
"initiator": params.Initiator, | ||
"invitation_key": params.InvitationKey, | ||
"my_did": params.MyDID, | ||
"connection_state": params.State, | ||
"their_did": params.TheirDID, | ||
"their_role": params.TheirRole, | ||
} | ||
err := c.get(c.AcapyURL+"/connections", queryParams, &connections) | ||
return connections.Result, err | ||
} | ||
|
||
func (c *Client) GetConnection(connectionID string) (Connection, error) { | ||
var connection Connection | ||
err := c.get(fmt.Sprintf("%s/connections/%s", c.AcapyURL, connectionID), nil, &connection) | ||
return connection, err | ||
} | ||
|
||
func (c *Client) RemoveConnection(connectionID string) error { | ||
return c.post(fmt.Sprintf("%s/connections/%s", c.AcapyURL, connectionID), nil, nil, nil) | ||
} | ||
|
||
type Thread struct { | ||
ThreadID string `json:"thread_id"` | ||
} | ||
|
||
func (c *Client) SendPing(connectionID string) (Thread, error) { | ||
ping := struct { | ||
Comment string `json:"comment"` | ||
}{ | ||
Comment: "ping", | ||
} | ||
var thread Thread | ||
err := c.post(fmt.Sprintf("%s/connections/%s/send-ping", c.AcapyURL, connectionID), nil, ping, &thread) | ||
return thread, err | ||
} |
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,8 @@ | ||
module github.com/ldej/go-acapy-client/example | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/gorilla/mux v1.8.0 | ||
github.com/ldej/go-acapy-client v0.1 | ||
) |
Oops, something went wrong.