github.com/hyperledger/aries-framework-go@v0.3.2/pkg/client/mediator/client.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package mediator
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  	"time"
    13  
    14  	"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
    15  	"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/mediator"
    16  )
    17  
    18  // provider contains dependencies for the route protocol and is typically created by using aries.Context().
    19  type provider interface {
    20  	Service(id string) (interface{}, error)
    21  }
    22  
    23  // Client enable access to route api.
    24  type Client struct {
    25  	service.Event
    26  	routeSvc protocolService
    27  	options  []mediator.ClientOption
    28  }
    29  
    30  // protocolService defines DID Exchange service.
    31  type protocolService interface {
    32  	// DIDComm service
    33  	service.DIDComm
    34  
    35  	// Register registers the agent with the router
    36  	Register(connectionID string, options ...mediator.ClientOption) error
    37  
    38  	// Unregister unregisters the agent with the router
    39  	Unregister(connID string) error
    40  
    41  	// GetConnections returns router`s connections.
    42  	GetConnections(...mediator.ConnectionOption) ([]string, error)
    43  
    44  	// Config returns the router's configuration.
    45  	Config(connID string) (*mediator.Config, error)
    46  }
    47  
    48  // WithTimeout option is for definition timeout value waiting for responses received from the router.
    49  func WithTimeout(t time.Duration) mediator.ClientOption {
    50  	return func(opts *mediator.ClientOptions) {
    51  		opts.Timeout = t
    52  	}
    53  }
    54  
    55  // New return new instance of route client.
    56  func New(ctx provider, options ...mediator.ClientOption) (*Client, error) {
    57  	svc, err := ctx.Service(mediator.Coordination)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	routeSvc, ok := svc.(protocolService)
    63  	if !ok {
    64  		return nil, errors.New("cast service to route service failed")
    65  	}
    66  
    67  	return &Client{
    68  		Event:    routeSvc,
    69  		routeSvc: routeSvc,
    70  		options:  options,
    71  	}, nil
    72  }
    73  
    74  // Register the agent with the router(passed in connectionID). This function asks router's
    75  // permission to publish it's endpoint and routing keys.
    76  func (c *Client) Register(connectionID string) error {
    77  	if err := c.routeSvc.Register(connectionID, c.options...); err != nil {
    78  		return fmt.Errorf("router registration : %w", err)
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  // Unregister unregisters the agent with the router.
    85  func (c *Client) Unregister(connID string) error {
    86  	if err := c.routeSvc.Unregister(connID); err != nil {
    87  		return fmt.Errorf("router unregister : %w", err)
    88  	}
    89  
    90  	return nil
    91  }
    92  
    93  // GetConnections returns router`s connections.
    94  func (c *Client) GetConnections(options ...ConnectionOption) ([]string, error) {
    95  	connections, err := c.routeSvc.GetConnections(options...)
    96  	if err != nil {
    97  		return nil, fmt.Errorf("get router connections: %w", err)
    98  	}
    99  
   100  	return connections, nil
   101  }
   102  
   103  // GetConfig returns the router's configuration.
   104  func (c *Client) GetConfig(connID string) (*mediator.Config, error) {
   105  	conf, err := c.routeSvc.Config(connID)
   106  	if err != nil {
   107  		return nil, fmt.Errorf("failed to fetch routing configuration : %w", err)
   108  	}
   109  
   110  	return conf, nil
   111  }