1
1
package com .strategyobject .substrateclient .api ;
2
2
3
- import com .strategyobject .substrateclient .pallet .GeneratedPalletResolver ;
4
- import com .strategyobject .substrateclient .pallet .PalletResolver ;
5
- import com .strategyobject .substrateclient .rpc .RpcGeneratedSectionFactory ;
6
- import com .strategyobject .substrateclient .rpc .api .section .State ;
3
+ import com .google .inject .Guice ;
4
+ import com .google .inject .Injector ;
5
+ import com .google .inject .Module ;
6
+ import com .google .inject .util .Modules ;
7
+ import com .strategyobject .substrateclient .pallet .PalletFactory ;
8
+ import com .strategyobject .substrateclient .rpc .RpcSectionFactory ;
7
9
import com .strategyobject .substrateclient .transport .ProviderInterface ;
8
10
import lombok .NonNull ;
11
+ import lombok .RequiredArgsConstructor ;
9
12
import lombok .val ;
10
13
11
14
import java .util .Map ;
16
19
* It allows interacting with blockchain in various ways: using RPC's queries directly or
17
20
* accessing Pallets and its APIs, such as storages, transactions, etc.
18
21
*/
22
+ @ RequiredArgsConstructor
19
23
public class Api implements AutoCloseable {
20
- private final @ NonNull ProviderInterface providerInterface ;
21
- private final @ NonNull PalletResolver palletResolver ;
24
+ private final @ NonNull Injector injector ;
25
+ private final @ NonNull RpcSectionFactory rpcSectionFactory ;
26
+ private final @ NonNull PalletFactory palletFactory ;
22
27
private final Map <Class <?>, Object > resolvedCache = new ConcurrentHashMap <>();
23
28
24
- private Api (@ NonNull ProviderInterface providerInterface ) {
25
- this .providerInterface = providerInterface ;
26
-
27
- val state = RpcGeneratedSectionFactory .create (State .class , providerInterface );
28
- this .palletResolver = GeneratedPalletResolver .with (state );
29
- }
30
29
31
30
/**
32
31
* Resolves the instance of a rpc by its definition.
@@ -35,9 +34,8 @@ private Api(@NonNull ProviderInterface providerInterface) {
35
34
* @param <T> the type of the rpc
36
35
* @return appropriate instance of the rpc
37
36
*/
38
- public <T > T rpc (Class <T > clazz ) {
39
- return clazz .cast (resolvedCache
40
- .computeIfAbsent (clazz , x -> RpcGeneratedSectionFactory .create (x , providerInterface )));
37
+ public <T > T rpc (@ NonNull Class <T > clazz ) {
38
+ return clazz .cast (resolvedCache .computeIfAbsent (clazz , rpcSectionFactory ::create ));
41
39
}
42
40
43
41
/**
@@ -48,18 +46,26 @@ public <T> T rpc(Class<T> clazz) {
48
46
* @return appropriate instance of the pallet
49
47
*/
50
48
public <T > T pallet (@ NonNull Class <T > clazz ) {
51
- return clazz .cast (resolvedCache
52
- .computeIfAbsent (clazz , palletResolver ::resolve ));
53
- }
54
-
55
- public static Api with (ProviderInterface providerInterface ) {
56
- return new Api (providerInterface );
49
+ return clazz .cast (resolvedCache .computeIfAbsent (clazz , palletFactory ::create ));
57
50
}
58
51
59
52
@ Override
60
53
public void close () throws Exception {
54
+ val providerInterface = injector .getInstance (ProviderInterface .class );
61
55
if (providerInterface instanceof AutoCloseable ) {
62
56
((AutoCloseable ) providerInterface ).close ();
63
57
}
64
58
}
59
+
60
+ public static ApiBuilder with (@ NonNull ProviderInterface providerInterface ) {
61
+ return new ApiBuilder (Guice .createInjector (new DefaultModule (providerInterface )));
62
+ }
63
+
64
+ public static ApiBuilder with (@ NonNull ProviderInterface providerInterface , @ NonNull Module overrideModule ) {
65
+ val module = Modules
66
+ .override (new DefaultModule (providerInterface ))
67
+ .with (overrideModule );
68
+
69
+ return new ApiBuilder (Guice .createInjector (module ));
70
+ }
65
71
}
0 commit comments