This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Framework Init - base template with Outbound transport config
- Add New(opts) function which takes in the options to configuration - Support outbound transport configuration Signed-off-by: Rolson Quadras <[email protected]>
- Loading branch information
1 parent
a8eff94
commit 2265127
Showing
6 changed files
with
203 additions
and
10 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
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
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,22 @@ | ||
/* | ||
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" | ||
|
||
// Context provides handles to various configs to be used by the clients | ||
type Context struct { | ||
outboundTransport transport.OutboundTransport | ||
} | ||
|
||
// ContextOption configure the context. | ||
type ContextOption func(opts *Context) error | ||
|
||
// OutboundTransport provides handle to the outbound transport | ||
func (ctx *Context) OutboundTransport() transport.OutboundTransport { | ||
return ctx.outboundTransport | ||
} |
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,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 options | ||
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 | ||
} |
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,66 @@ | ||
/* | ||
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" | ||
) | ||
|
||
// Aries provides access to clients being managed by the framework. | ||
type Aries struct { | ||
outboundTransport transport.OutboundTransport | ||
} | ||
|
||
// Option configures the framework. | ||
type Option func(opts *Aries) error | ||
|
||
// New initializes the Aries framework based on the set of options provided. | ||
func New(opts ...Option) (*Aries, error) { | ||
// get the default framework options | ||
defOpts, err := defFrameworkOpts() | ||
if err != nil { | ||
return nil, errors.Errorf("Default option init failed : %w", err) | ||
} | ||
|
||
frameworkOpts := &Aries{} | ||
|
||
// 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) | ||
} | ||
} | ||
|
||
return frameworkOpts, nil | ||
} | ||
|
||
// WithOutboundTransport injects a OutboundTransport interface to the Aries | ||
func WithOutboundTransport(ot transport.OutboundTransport) Option { | ||
return func(opts *Aries) error { | ||
opts.outboundTransport = ot | ||
return nil | ||
} | ||
} | ||
|
||
// Context provides handle to framework context | ||
func (a *Aries) Context(opts ...ContextOption) (*Context, error) { | ||
ctx := &Context{ | ||
outboundTransport: a.outboundTransport, | ||
} | ||
|
||
// generate context from options | ||
for _, option := range append(opts) { | ||
err := option(ctx) | ||
if err != nil { | ||
return nil, errors.Errorf("Framework context creation failed: %w", err) | ||
} | ||
} | ||
|
||
return ctx, 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,52 @@ | ||
/* | ||
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" | ||
errors "golang.org/x/xerrors" | ||
) | ||
|
||
func TestFramework(t *testing.T) { | ||
// framework new - error | ||
_, err := New(func(opts *Aries) error { | ||
return errors.New("error creating the framework option") | ||
}) | ||
require.Error(t, err) | ||
|
||
// framework new - success | ||
ot := mocktransport.NewOutboundTransport("success") | ||
aries, err := New(WithOutboundTransport(ot)) | ||
require.NoError(t, err) | ||
|
||
req := &exchange.Request{ | ||
ID: "5678876542345", | ||
Label: "Bob", | ||
} | ||
|
||
// context - success | ||
ctx, err := aries.Context() | ||
require.NoError(t, err) | ||
require.NoError(t, exchange.SendExchangeRequest(ctx, req, "http://example/didexchange")) | ||
|
||
ctx, err = aries.Context(func(opts *Context) error { | ||
opts.outboundTransport = ot | ||
return nil | ||
}) | ||
require.NoError(t, err) | ||
require.Error(t, exchange.SendExchangeRequest(ctx, req, "")) | ||
|
||
// context - error | ||
_, err = aries.Context(func(opts *Context) error { | ||
return errors.New("error creating the context option") | ||
}) | ||
require.Error(t, err) | ||
} |