From 94efeb32066ee1f587c67c8642c5e2ff588c5efa Mon Sep 17 00:00:00 2001 From: Rolson Quadras Date: Mon, 12 Aug 2019 10:51:12 -0400 Subject: [PATCH] feat: Framework Init - base template with Outbound transport config - Add New(opts) function which takes in the options to configuration - Support outbound transport configuration Closes #78 Signed-off-by: Rolson Quadras --- pkg/framework/aries/default.go | 40 +++++++++++++++++ pkg/framework/aries/framework.go | 63 +++++++++++++++++++++++++++ pkg/framework/aries/framework_test.go | 29 ++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 pkg/framework/aries/default.go create mode 100644 pkg/framework/aries/framework.go create mode 100644 pkg/framework/aries/framework_test.go diff --git a/pkg/framework/aries/default.go b/pkg/framework/aries/default.go new file mode 100644 index 0000000000..57dbcdbb02 --- /dev/null +++ b/pkg/framework/aries/default.go @@ -0,0 +1,40 @@ +/* +Copyright SecureKey Technologies Inc. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package aries + +import ( + "github.com/hyperledger/aries-framework-go/pkg/didcomm/transport" + errors "golang.org/x/xerrors" +) + +// defFramework provides default framework configs +type defFramework struct{} + +// outboundTransport provides default OutboundTransport +func (d defFramework) outboundTransport() (transport.OutboundTransport, error) { + // TODO - https://github.com/hyperledger/aries-framework-go/issues/83 + return nil, nil +} + +// defFrameworkOpts provides default framework optios +func defFrameworkOpts() ([]Option, error) { + // get the default framework configs + def := defFramework{} + + // outbound transport + ot, err := def.outboundTransport() + if err != nil { + return nil, errors.Errorf("Default outbound transport init failed : %w", err) + } + + var opts []Option + + opt := WithOutboundTransport(ot) + opts = append(opts, opt) + + return opts, nil +} diff --git a/pkg/framework/aries/framework.go b/pkg/framework/aries/framework.go new file mode 100644 index 0000000000..7d2adf442c --- /dev/null +++ b/pkg/framework/aries/framework.go @@ -0,0 +1,63 @@ +/* +Copyright SecureKey Technologies Inc. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package aries + +import ( + "github.com/hyperledger/aries-framework-go/pkg/didcomm/transport" + errors "golang.org/x/xerrors" +) + +// Framework provides access to clients being managed by the Framework. +type Framework struct { + opts options +} + +type options struct { + OutboundTransport transport.OutboundTransport +} + +// Option configures the framework. +type Option func(opts *options) error + +// New initializes the Framework based on the set of options provided. +func New(opts ...Option) (*Framework, error) { + // get the default framework options + defOpts, err := defFrameworkOpts() + if err != nil { + return nil, errors.Errorf("Default option init failed : %w", err) + } + + frameworkOpts := options{} + + // generate framework configs from options + for _, option := range append(defOpts, opts...) { + err := option(&frameworkOpts) + if err != nil { + return nil, errors.Errorf("Error in option passed to New: %w", err) + } + } + + // create framework with default configs + framework := &Framework{ + opts: frameworkOpts, + } + + return framework, nil +} + +// WithOutboundTransport injects a OutboundTransport interface to the Framework +func WithOutboundTransport(ot transport.OutboundTransport) Option { + return func(opts *options) error { + opts.OutboundTransport = ot + return nil + } +} + +// OutboundTransport provides configured OutboundTransport +func (f *Framework) OutboundTransport() transport.OutboundTransport { + return f.opts.OutboundTransport +} diff --git a/pkg/framework/aries/framework_test.go b/pkg/framework/aries/framework_test.go new file mode 100644 index 0000000000..d49eb94507 --- /dev/null +++ b/pkg/framework/aries/framework_test.go @@ -0,0 +1,29 @@ +/* +Copyright SecureKey Technologies Inc. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package aries + +import ( + "testing" + + "github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/exchange" + mocktransport "github.com/hyperledger/aries-framework-go/pkg/internal/didcomm/transport/mock" + "github.com/stretchr/testify/require" +) + +func TestFramework(t *testing.T) { + ot := mocktransport.NewOutboundTransport("success") + + f, err := New(WithOutboundTransport(ot)) + require.NoError(t, err) + + req := &exchange.Request{ + ID: "5678876542345", + Label: "Bob", + } + + require.NoError(t, exchange.SendExchangeRequest(req, "http://example/didexchange", f.OutboundTransport())) +}