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 . Module ;
5
+ import com .strategyobject .substrateclient .pallet . PalletFactory ;
6
+ import com .strategyobject .substrateclient .rpc .RpcSectionFactory ;
7
7
import com .strategyobject .substrateclient .transport .ProviderInterface ;
8
8
import lombok .NonNull ;
9
- import lombok .val ;
9
+ import lombok .RequiredArgsConstructor ;
10
10
11
11
import java .util .Map ;
12
12
import java .util .concurrent .ConcurrentHashMap ;
13
+ import java .util .function .Supplier ;
13
14
14
15
/**
15
16
* Provides the ability to query a node and interact with the Polkadot or Substrate chains.
16
17
* It allows interacting with blockchain in various ways: using RPC's queries directly or
17
18
* accessing Pallets and its APIs, such as storages, transactions, etc.
18
19
*/
20
+ @ RequiredArgsConstructor
19
21
public class Api implements AutoCloseable {
20
- private final @ NonNull ProviderInterface providerInterface ;
21
- private final @ NonNull PalletResolver palletResolver ;
22
+ private final @ NonNull RpcSectionFactory rpcSectionFactory ;
23
+ private final @ NonNull PalletFactory palletFactory ;
22
24
private final Map <Class <?>, Object > resolvedCache = new ConcurrentHashMap <>();
23
25
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
26
31
27
/**
32
28
* Resolves the instance of a rpc by its definition.
@@ -35,9 +31,8 @@ private Api(@NonNull ProviderInterface providerInterface) {
35
31
* @param <T> the type of the rpc
36
32
* @return appropriate instance of the rpc
37
33
*/
38
- public <T > T rpc (Class <T > clazz ) {
39
- return clazz .cast (resolvedCache
40
- .computeIfAbsent (clazz , x -> RpcGeneratedSectionFactory .create (x , providerInterface )));
34
+ public <T > T rpc (@ NonNull Class <T > clazz ) {
35
+ return clazz .cast (resolvedCache .computeIfAbsent (clazz , rpcSectionFactory ::create ));
41
36
}
42
37
43
38
/**
@@ -48,18 +43,21 @@ public <T> T rpc(Class<T> clazz) {
48
43
* @return appropriate instance of the pallet
49
44
*/
50
45
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 );
46
+ return clazz .cast (resolvedCache .computeIfAbsent (clazz , palletFactory ::create ));
57
47
}
58
48
59
49
@ Override
60
50
public void close () throws Exception {
61
- if (providerInterface instanceof AutoCloseable ) {
62
- ((AutoCloseable ) providerInterface ).close ();
51
+ if (rpcSectionFactory instanceof AutoCloseable ) {
52
+ ((AutoCloseable ) rpcSectionFactory ).close ();
63
53
}
64
54
}
55
+
56
+ public static ApiBuilder with (@ NonNull Supplier <ProviderInterface > providerInterface ) {
57
+ return with (new DefaultModule (providerInterface .get ()));
58
+ }
59
+
60
+ public static ApiBuilder with (@ NonNull Module module ) {
61
+ return new ApiBuilder (Guice .createInjector (new RequireModule (), module ));
62
+ }
65
63
}
0 commit comments