From 59a55c2ac5eba16f95d6531aeffd6591d3b053d1 Mon Sep 17 00:00:00 2001 From: ac4ch Date: Wed, 8 May 2024 07:55:25 +0200 Subject: [PATCH] adding admin contacts methods --- admin_contacts.go | 29 +++++++++++++++++++++++++ transports/http_admin.go | 47 ++++++++++++++++++++++++++++++++++++++++ transports/interface.go | 5 +++++ 3 files changed, 81 insertions(+) create mode 100644 admin_contacts.go diff --git a/admin_contacts.go b/admin_contacts.go new file mode 100644 index 0000000..7dae43e --- /dev/null +++ b/admin_contacts.go @@ -0,0 +1,29 @@ +package walletclient + +import ( + "context" + + "github.com/bitcoin-sv/spv-wallet/models" + + "github.com/bitcoin-sv/spv-wallet-go-client/transports" +) + +func (wc *WalletClient) AdminGetContacts(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *transports.QueryParams) ([]*models.Contact, transports.ResponseError) { + return wc.transport.AdminGetContacts(ctx, conditions, metadata, queryParams) +} + +func (wc *WalletClient) AdminUpdateContact(ctx context.Context, id, fullName string, metadata *models.Metadata) (*models.Contact, transports.ResponseError) { + return wc.transport.AdminUpdateContact(ctx, id, fullName, metadata) +} + +func (wc *WalletClient) AdminDeleteContact(ctx context.Context, id string) transports.ResponseError { + return wc.transport.AdminDeleteContact(ctx, id) +} + +func (wc *WalletClient) AdminAcceptContact(ctx context.Context, id string) (*models.Contact, transports.ResponseError) { + return wc.transport.AdminAcceptContact(ctx, id) +} + +func (wc *WalletClient) AdminRejectContact(ctx context.Context, id string) (*models.Contact, transports.ResponseError) { + return wc.transport.AdminRejectContact(ctx, id) +} diff --git a/transports/http_admin.go b/transports/http_admin.go index 3c9548f..7ae2d7e 100644 --- a/transports/http_admin.go +++ b/transports/http_admin.go @@ -3,6 +3,7 @@ package transports import ( "context" "encoding/json" + "fmt" "net/http" "github.com/bitcoin-sv/spv-wallet/models" @@ -316,3 +317,49 @@ func (h *TransportHTTP) AdminGetSharedConfig(ctx context.Context) (*models.Share return model, nil } + +func (h *TransportHTTP) AdminGetContacts(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *QueryParams) ([]*models.Contact, ResponseError) { + jsonStr, err := json.Marshal(map[string]interface{}{ + FieldConditions: conditions, + FieldMetadata: processMetadata(metadata), + FieldQueryParams: queryParams, + }) + if err != nil { + return nil, WrapError(err) + } + + var contacts []*models.Contact + err = h.doHTTPRequest(ctx, http.MethodPost, "admin/contact/search", jsonStr, h.adminXPriv, h.IsSignRequest(), contacts) + return contacts, WrapError(err) +} + +func (h *TransportHTTP) AdminUpdateContact(ctx context.Context, id, fullName string, metadata *models.Metadata) (*models.Contact, ResponseError) { + jsonStr, err := json.Marshal(map[string]interface{}{ + "fullName": fullName, + FieldMetadata: processMetadata(metadata), + }) + if err != nil { + return nil, WrapError(err) + } + var contact *models.Contact + err = h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("admin/contact/%s", id), jsonStr, h.adminXPriv, h.IsSignRequest(), contact) + return contact, WrapError(err) +} + +func (h *TransportHTTP) AdminDeleteContact(ctx context.Context, id string) ResponseError { + var contact *models.Contact + err := h.doHTTPRequest(ctx, http.MethodDelete, "", nil, h.adminXPriv, h.IsSignRequest(), contact) + return WrapError(err) +} + +func (h *TransportHTTP) AdminAcceptContact(ctx context.Context, id string) (*models.Contact, ResponseError) { + var contact *models.Contact + err := h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("admin/contact/accepted/%s", id), nil, h.adminXPriv, h.IsSignRequest(), contact) + return contact, WrapError(err) +} + +func (h *TransportHTTP) AdminRejectContact(ctx context.Context, id string) (*models.Contact, ResponseError) { + var contact *models.Contact + err := h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("admin/contact/rejected/%s", id), nil, h.adminXPriv, h.IsSignRequest(), contact) + return contact, WrapError(err) +} diff --git a/transports/interface.go b/transports/interface.go index 3ae040b..a7ceeea 100644 --- a/transports/interface.go +++ b/transports/interface.go @@ -82,6 +82,11 @@ type AdminService interface { AdminGetXPubsCount(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata) (int64, ResponseError) AdminRecordTransaction(ctx context.Context, hex string) (*models.Transaction, ResponseError) AdminGetSharedConfig(ctx context.Context) (*models.SharedConfig, ResponseError) + AdminGetContacts(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *QueryParams) ([]*models.Contact, ResponseError) + AdminUpdateContact(ctx context.Context, id, fullName string, metadata *models.Metadata) (*models.Contact, ResponseError) + AdminDeleteContact(ctx context.Context, id string) ResponseError + AdminAcceptContact(ctx context.Context, id string) (*models.Contact, ResponseError) + AdminRejectContact(ctx context.Context, id string) (*models.Contact, ResponseError) } // TransportService the transport service interface