22
33import com .scalekit .Environment ;
44import com .scalekit .api .ConnectionClient ;
5- import com .scalekit .exceptions .APIException ;
65import com .scalekit .grpc .scalekit .v1 .connections .*;
6+ import com .scalekit .internal .RetryExecuter ;
77import com .scalekit .internal .ScalekitCredentials ;
88import io .grpc .Deadline ;
99import io .grpc .ManagedChannel ;
1313public class ScalekitConnectionClient implements ConnectionClient {
1414
1515 private final ConnectionServiceGrpc .ConnectionServiceBlockingStub ConnectionStub ;
16+ private final ScalekitCredentials credentials ;
1617
1718 public ScalekitConnectionClient (ManagedChannel channel , ScalekitCredentials credentials ){
1819 try {
20+ this .credentials = credentials ;
1921 this .ConnectionStub = ConnectionServiceGrpc
2022 .newBlockingStub (channel )
2123 .withDeadline (Deadline .after (Environment .defaultConfig ().timeout , TimeUnit .MILLISECONDS ))
@@ -34,17 +36,15 @@ public ScalekitConnectionClient(ManagedChannel channel, ScalekitCredentials cred
3436 */
3537 @ Override
3638 public Connection getConnectionById (String connectionId , String organizationId ) {
37- GetConnectionRequest request = GetConnectionRequest . newBuilder ()
38- . setOrganizationId ( organizationId )
39- . setId ( connectionId )
40- . build ();
41-
42- try {
43- GetConnectionResponse response = ConnectionStub . getConnection ( request );
39+ return RetryExecuter . executeWithRetry (() -> {
40+ GetConnectionResponse response = this . ConnectionStub . getConnection (
41+ GetConnectionRequest . newBuilder ( )
42+ . setId ( connectionId )
43+ . setOrganizationId ( organizationId )
44+ . build ()
45+ );
4446 return response .getConnection ();
45- } catch (StatusRuntimeException e ) {
46- throw new APIException (e );
47- }
47+ },this .credentials );
4848 }
4949
5050 /**
@@ -54,15 +54,12 @@ public Connection getConnectionById(String connectionId, String organizationId)
5454 */
5555 @ Override
5656 public ListConnectionsResponse listConnectionsByDomain (String domain ) {
57- ListConnectionsRequest request = ListConnectionsRequest .newBuilder ()
58- .setDomain (domain )
59- .setInclude ("all" )
60- .build ();
61- try {
62- return ConnectionStub .listConnections (request );
63- } catch (StatusRuntimeException e ) {
64- throw new APIException (e );
65- }
57+ return RetryExecuter .executeWithRetry (() -> this .ConnectionStub .listConnections (
58+ ListConnectionsRequest .newBuilder ()
59+ .setDomain (domain )
60+ .setInclude ("all" )
61+ .build ()
62+ ),this .credentials );
6663 }
6764
6865 /**
@@ -72,16 +69,13 @@ public ListConnectionsResponse listConnectionsByDomain(String domain) {
7269 */
7370 @ Override
7471 public ListConnectionsResponse listConnectionsByOrganization (String organizationId ) {
75- ListConnectionsRequest request = ListConnectionsRequest .newBuilder ()
76- .setInclude ("all" )
77- .setOrganizationId (organizationId )
78- .build ();
7972
80- try {
81- return ConnectionStub .listConnections (request );
82- } catch (StatusRuntimeException e ) {
83- throw new APIException (e );
84- }
73+ return RetryExecuter .executeWithRetry (() -> this .ConnectionStub .listConnections (
74+ ListConnectionsRequest .newBuilder ()
75+ .setOrganizationId (organizationId )
76+ .setInclude ("all" )
77+ .build ()
78+ ),this .credentials );
8579 }
8680
8781
@@ -93,16 +87,14 @@ public ListConnectionsResponse listConnectionsByOrganization(String organization
9387 */
9488 @ Override
9589 public ToggleConnectionResponse enableConnection (String connectionId , String organizationId ) {
96- ToggleConnectionRequest request = ToggleConnectionRequest .newBuilder ()
97- .setOrganizationId (organizationId )
98- .setId (connectionId )
99- .build ();
10090
101- try {
102- return ConnectionStub .enableConnection (request );
103- } catch (StatusRuntimeException e ) {
104- throw new APIException (e );
105- }
91+ return RetryExecuter .executeWithRetry (() -> {
92+ ToggleConnectionRequest request = ToggleConnectionRequest .newBuilder ()
93+ .setOrganizationId (organizationId )
94+ .setId (connectionId )
95+ .build ();
96+ return this .ConnectionStub .enableConnection (request );
97+ },this .credentials );
10698
10799 }
108100
@@ -114,15 +106,33 @@ public ToggleConnectionResponse enableConnection(String connectionId, String org
114106 */
115107 @ Override
116108 public ToggleConnectionResponse disableConnection (String connectionId , String organizationId ) {
117- ToggleConnectionRequest request = ToggleConnectionRequest .newBuilder ()
118- .setOrganizationId (organizationId )
119- .setId (connectionId )
120- .build ();
121109
122- try {
123- return ConnectionStub .disableConnection (request );
124- } catch (StatusRuntimeException e ) {
125- throw new APIException (e );
126- }
110+ return RetryExecuter .executeWithRetry (() -> {
111+ ToggleConnectionRequest request = ToggleConnectionRequest .newBuilder ()
112+ .setOrganizationId (organizationId )
113+ .setId (connectionId )
114+ .build ();
115+ return this .ConnectionStub .disableConnection (request );
116+ },this .credentials );
117+ }
118+
119+
120+ /**
121+ * createConnection creates a new connection in Scalekit for the organization
122+ * @param organizationId: The organization ID
123+ * @param connection: The connection to create
124+ * @return Connection: The connection created
125+ */
126+ @ Override
127+ public Connection createConnection (String organizationId , CreateConnection connection ) {
128+ return RetryExecuter .executeWithRetry (() -> {
129+ CreateConnectionResponse response = this .ConnectionStub .createConnection (
130+ CreateConnectionRequest .newBuilder ()
131+ .setOrganizationId (organizationId )
132+ .setConnection (connection )
133+ .build ()
134+ );
135+ return response .getConnection ();
136+ },this .credentials );
127137 }
128138}
0 commit comments