-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
191 changed files
with
1,498 additions
and
985 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
50 changes: 50 additions & 0 deletions
50
api/src/main/java/com/strategyobject/substrateclient/api/ApiBuilder.java
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,50 @@ | ||
package com.strategyobject.substrateclient.api; | ||
|
||
import com.google.inject.Injector; | ||
import com.strategyobject.substrateclient.common.types.AutoRegistry; | ||
import com.strategyobject.substrateclient.rpc.registries.RpcDecoderRegistry; | ||
import com.strategyobject.substrateclient.rpc.registries.RpcEncoderRegistry; | ||
import com.strategyobject.substrateclient.scale.registries.ScaleReaderRegistry; | ||
import com.strategyobject.substrateclient.scale.registries.ScaleWriterRegistry; | ||
import com.strategyobject.substrateclient.transport.ProviderInterface; | ||
import lombok.val; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Consumer; | ||
|
||
public class ApiBuilder { | ||
private static final String ROOT_PREFIX = "com.strategyobject.substrateclient"; | ||
|
||
private final Injector injector; | ||
|
||
public ApiBuilder(Injector injector) { | ||
this.injector = injector; | ||
|
||
autoRegister( | ||
ScaleReaderRegistry.class, | ||
ScaleWriterRegistry.class, | ||
RpcDecoderRegistry.class, | ||
RpcEncoderRegistry.class); | ||
} | ||
|
||
public <T> ApiBuilder configure(Class<T> clazz, Consumer<T> consumer) { | ||
consumer.accept(injector.getInstance(clazz)); | ||
return this; | ||
} | ||
|
||
public CompletableFuture<Api> build() { | ||
val provider = injector.getInstance(ProviderInterface.class); | ||
val result = provider.isConnected() ? | ||
CompletableFuture.<Void>completedFuture(null) : | ||
provider.connect(); | ||
|
||
return result.thenApply(ignored -> injector.getInstance(Api.class)); | ||
} | ||
|
||
@SafeVarargs | ||
private final void autoRegister(Class<? extends AutoRegistry>... registries) { | ||
for (val registry : registries) { | ||
injector.getInstance(registry).registerAnnotatedFrom(ROOT_PREFIX); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
api/src/main/java/com/strategyobject/substrateclient/api/DefaultModule.java
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,68 @@ | ||
package com.strategyobject.substrateclient.api; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.google.inject.Provides; | ||
import com.google.inject.Singleton; | ||
import com.strategyobject.substrateclient.pallet.GeneratedPalletFactory; | ||
import com.strategyobject.substrateclient.pallet.PalletFactory; | ||
import com.strategyobject.substrateclient.rpc.GeneratedRpcSectionFactory; | ||
import com.strategyobject.substrateclient.rpc.RpcSectionFactory; | ||
import com.strategyobject.substrateclient.rpc.api.section.State; | ||
import com.strategyobject.substrateclient.rpc.registries.RpcDecoderRegistry; | ||
import com.strategyobject.substrateclient.rpc.registries.RpcEncoderRegistry; | ||
import com.strategyobject.substrateclient.scale.registries.ScaleReaderRegistry; | ||
import com.strategyobject.substrateclient.scale.registries.ScaleWriterRegistry; | ||
import com.strategyobject.substrateclient.transport.ProviderInterface; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class DefaultModule extends AbstractModule { | ||
private final @NonNull ProviderInterface providerInterface; | ||
|
||
@Override | ||
protected void configure() { | ||
try { | ||
bind(ProviderInterface.class).toInstance(providerInterface); | ||
bind(ScaleReaderRegistry.class).asEagerSingleton(); | ||
bind(ScaleWriterRegistry.class).asEagerSingleton(); | ||
bind(RpcDecoderRegistry.class) | ||
.toConstructor(RpcDecoderRegistry.class.getConstructor(ScaleReaderRegistry.class)) | ||
.asEagerSingleton(); | ||
bind(RpcEncoderRegistry.class) | ||
.toConstructor(RpcEncoderRegistry.class.getConstructor(ScaleWriterRegistry.class)) | ||
.asEagerSingleton(); | ||
bind(RpcSectionFactory.class) | ||
.toConstructor( | ||
GeneratedRpcSectionFactory.class.getConstructor( | ||
ProviderInterface.class, | ||
RpcEncoderRegistry.class, | ||
ScaleWriterRegistry.class, | ||
RpcDecoderRegistry.class, | ||
ScaleReaderRegistry.class)) | ||
.asEagerSingleton(); | ||
bind(PalletFactory.class) | ||
.toConstructor( | ||
GeneratedPalletFactory.class.getConstructor( | ||
ScaleReaderRegistry.class, | ||
ScaleWriterRegistry.class, | ||
State.class | ||
)) | ||
.asEagerSingleton(); | ||
bind(Api.class) | ||
.toConstructor( | ||
Api.class.getConstructor( | ||
RpcSectionFactory.class, | ||
PalletFactory.class)) | ||
.asEagerSingleton(); | ||
} catch (NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
public State provideState(RpcSectionFactory rpcSectionFactory) { | ||
return rpcSectionFactory.create(State.class); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
api/src/main/java/com/strategyobject/substrateclient/api/RequireModule.java
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,24 @@ | ||
package com.strategyobject.substrateclient.api; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.strategyobject.substrateclient.pallet.PalletFactory; | ||
import com.strategyobject.substrateclient.rpc.RpcSectionFactory; | ||
import com.strategyobject.substrateclient.rpc.registries.RpcDecoderRegistry; | ||
import com.strategyobject.substrateclient.rpc.registries.RpcEncoderRegistry; | ||
import com.strategyobject.substrateclient.scale.registries.ScaleReaderRegistry; | ||
import com.strategyobject.substrateclient.scale.registries.ScaleWriterRegistry; | ||
import com.strategyobject.substrateclient.transport.ProviderInterface; | ||
|
||
public class RequireModule extends AbstractModule { | ||
@Override | ||
protected void configure() { | ||
requireBinding(ProviderInterface.class); | ||
requireBinding(ScaleReaderRegistry.class); | ||
requireBinding(ScaleWriterRegistry.class); | ||
requireBinding(RpcDecoderRegistry.class); | ||
requireBinding(RpcEncoderRegistry.class); | ||
requireBinding(RpcSectionFactory.class); | ||
requireBinding(PalletFactory.class); | ||
requireBinding(Api.class); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
api/src/test/java/com/strategyobject/substrateclient/api/OverrideModule.java
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,11 @@ | ||
package com.strategyobject.substrateclient.api; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.strategyobject.substrateclient.pallet.PalletFactory; | ||
|
||
public class OverrideModule extends AbstractModule { | ||
@Override | ||
protected void configure() { | ||
bind(PalletFactory.class).to(ThrowingPalletFactory.class); | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
api/src/test/java/com/strategyobject/substrateclient/api/ThrowingPalletFactory.java
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,15 @@ | ||
package com.strategyobject.substrateclient.api; | ||
|
||
import com.strategyobject.substrateclient.pallet.PalletFactory; | ||
import lombok.NonNull; | ||
import lombok.Setter; | ||
|
||
public class ThrowingPalletFactory implements PalletFactory { | ||
@Setter | ||
private String message; | ||
|
||
@Override | ||
public <T> T create(@NonNull Class<T> interfaceClass) { | ||
throw new RuntimeException(message); | ||
} | ||
} |
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
Oops, something went wrong.