@@ -18,10 +18,7 @@ import (
1818
1919// Client represents the fabric transaction clients
2020type Client struct {
21- opts * clientOptions
22- identity apifabclient.User
23- providers apisdk.SDK
24- clientFactory apisdk.SessionClientFactory
21+ provider clientProvider
2522}
2623
2724// ClientOption configures the clients created by the SDK.
@@ -33,6 +30,15 @@ type clientOptions struct {
3330 targetFilter resmgmt.TargetFilter
3431}
3532
33+ type clientProvider func () (* clientContext , error )
34+
35+ type clientContext struct {
36+ opts * clientOptions
37+ identity apifabclient.User
38+ providers apisdk.SDK
39+ clientFactory apisdk.SessionClientFactory
40+ }
41+
3642// WithOrg uses the configuration and users from the named organization.
3743func WithOrg (name string ) ClientOption {
3844 return func (opts * clientOptions ) error {
@@ -59,24 +65,32 @@ func withConfig(configProvider apiconfig.Config) ClientOption {
5965}
6066
6167// NewClient allows creation of transactions using the supplied identity as the credential.
62- func (sdk * FabricSDK ) NewClient (identityOpt IdentityOption , opts ... ClientOption ) (* Client , error ) {
63- o , err := newClientOptions (sdk .ConfigProvider (), opts )
64- if err != nil {
65- return nil , errors .WithMessage (err , "unable to retrieve configuration from SDK" )
66- }
68+ func (sdk * FabricSDK ) NewClient (identityOpt IdentityOption , opts ... ClientOption ) * Client {
69+ // delay execution of the following logic to avoid error return from this function.
70+ // this is done to allow a cleaner API - i.e., client, err := sdk.NewClient(args).<Desired Interface>(extra args)
71+ provider := func () (* clientContext , error ) {
72+ o , err := newClientOptions (sdk .ConfigProvider (), opts )
73+ if err != nil {
74+ return nil , errors .WithMessage (err , "unable to retrieve configuration from SDK" )
75+ }
6776
68- identity , err := sdk .newIdentity (o .orgID , identityOpt )
69- if err != nil {
70- return nil , errors .WithMessage (err , "unable to create client context" )
71- }
77+ identity , err := sdk .newIdentity (o .orgID , identityOpt )
78+ if err != nil {
79+ return nil , errors .WithMessage (err , "unable to create client context" )
80+ }
7281
82+ cc := clientContext {
83+ opts : o ,
84+ identity : identity ,
85+ providers : sdk ,
86+ clientFactory : sdk .opts .Session ,
87+ }
88+ return & cc , nil
89+ }
7390 client := Client {
74- opts : o ,
75- identity : identity ,
76- providers : sdk ,
77- clientFactory : sdk .opts .Session ,
91+ provider : provider ,
7892 }
79- return & client , nil
93+ return & client
8094}
8195
8296func newClientOptions (config apiconfig.Config , options []ClientOption ) (* clientOptions , error ) {
@@ -107,8 +121,13 @@ func newClientOptions(config apiconfig.Config, options []ClientOption) (*clientO
107121
108122// ChannelMgmt returns a client API for managing channels
109123func (c * Client ) ChannelMgmt () (chmgmt.ChannelMgmtClient , error ) {
110- session := newSession (c .identity )
111- client , err := c .clientFactory .NewChannelMgmtClient (c .providers , session , c .opts .configProvider )
124+ p , err := c .provider ()
125+ if err != nil {
126+ return nil , errors .WithMessage (err , "unable to get client provider context" )
127+ }
128+
129+ session := newSession (p .identity )
130+ client , err := p .clientFactory .NewChannelMgmtClient (p .providers , session , p .opts .configProvider )
112131 if err != nil {
113132 return nil , errors .WithMessage (err , "failed to create new channel management client" )
114133 }
@@ -118,52 +137,32 @@ func (c *Client) ChannelMgmt() (chmgmt.ChannelMgmtClient, error) {
118137
119138// ResourceMgmt returns a client API for managing system resources
120139func (c * Client ) ResourceMgmt () (resmgmt.ResourceMgmtClient , error ) {
121- session := newSession (c .identity )
122- client , err := c .clientFactory .NewResourceMgmtClient (c .providers , session , c .opts .configProvider , c .opts .targetFilter )
140+ p , err := c .provider ()
123141 if err != nil {
124- return nil , errors .WithMessage (err , "failed to created new resource management client " )
142+ return nil , errors .WithMessage (err , "unable to get client provider context " )
125143 }
126144
127- return client , nil
128- }
129-
130- // Channel returns a client API for transacting on a channel
131- func (c * Client ) Channel (id string ) (apitxn.ChannelClient , error ) {
132- session := newSession (c .identity )
133- client , err := c .clientFactory .NewChannelClient (c .providers , session , c .opts .configProvider , id )
145+ session := newSession (p .identity )
146+ client , err := p .clientFactory .NewResourceMgmtClient (p .providers , session , p .opts .configProvider , p .opts .targetFilter )
134147 if err != nil {
135148 return nil , errors .WithMessage (err , "failed to created new resource management client" )
136149 }
137150
138151 return client , nil
139152}
140153
141- // NewClientChannelMgmt returns a new client for managing channels
142- func (sdk * FabricSDK ) NewClientChannelMgmt (identity IdentityOption , opts ... ClientOption ) (chmgmt.ChannelMgmtClient , error ) {
143- c , err := sdk .NewClient (identity , opts ... )
144- if err != nil {
145- return nil , errors .WithMessage (err , "error creating client from SDK" )
146- }
147-
148- return c .ChannelMgmt ()
149- }
150-
151- // NewClientResourceMgmt returns a new client for managing system resources
152- func (sdk * FabricSDK ) NewClientResourceMgmt (identity IdentityOption , opts ... ClientOption ) (resmgmt.ResourceMgmtClient , error ) {
153- c , err := sdk .NewClient (identity , opts ... )
154+ // Channel returns a client API for transacting on a channel
155+ func (c * Client ) Channel (id string ) (apitxn.ChannelClient , error ) {
156+ p , err := c .provider ()
154157 if err != nil {
155- return nil , errors .WithMessage (err , "error creating client from SDK " )
158+ return nil , errors .WithMessage (err , "unable to get client provider context " )
156159 }
157160
158- return c .ResourceMgmt ()
159- }
160-
161- // NewClientChannel returns a new client for a channel
162- func (sdk * FabricSDK ) NewClientChannel (identity IdentityOption , channelID string , opts ... ClientOption ) (apitxn.ChannelClient , error ) {
163- c , err := sdk .NewClient (identity , opts ... )
161+ session := newSession (p .identity )
162+ client , err := p .clientFactory .NewChannelClient (p .providers , session , p .opts .configProvider , id )
164163 if err != nil {
165- return nil , errors .WithMessage (err , "error creating client from SDK " )
164+ return nil , errors .WithMessage (err , "failed to created new resource management client " )
166165 }
167166
168- return c . Channel ( channelID )
167+ return client , nil
169168}
0 commit comments