|
13 | 13 | import java.util.Map; |
14 | 14 |
|
15 | 15 | /** |
16 | | - * This example demonstrates how to use the Skyflow SDK to invoke connections for different endpoints with different configurations. |
| 16 | + * This example demonstrates how to use the Skyflow SDK to invoke API connections. |
17 | 17 | * It includes: |
18 | | - * 1. Setting up connection configurations. |
19 | | - * 2. Creating a Skyflow client. |
20 | | - * 3. Sending POST and GET requests to connections. |
| 18 | + * 1. Setting up credentials and connection configurations. |
| 19 | + * 2. Creating a Skyflow client with multiple connections. |
| 20 | + * 3. Sending a POST request with request body and headers. |
| 21 | + * 4. Sending a GET request with path and query parameters. |
21 | 22 | */ |
22 | 23 | public class InvokeConnectionExample { |
23 | 24 | public static void main(String[] args) throws SkyflowException { |
24 | | - // Step 1: Set up credentials for the first connection configuration |
| 25 | + // Step 1: Set up credentials for API authentication |
25 | 26 | Credentials credentials = new Credentials(); |
26 | | - credentials.setPath("<YOUR_CREDENTIALS_FILE_PATH_1>"); // Replace with the path to the credentials file |
| 27 | + credentials.setApiKey("<YOUR_API_KEY>"); // Replace with the actual API key |
27 | 28 |
|
28 | | - // Step 2: Configure the first connection (Connection 1) |
29 | | - ConnectionConfig connectionConfig1 = new ConnectionConfig(); |
30 | | - connectionConfig1.setConnectionId("<YOUR_CONNECTION_ID_1>"); // Replace with the ID of the first connection |
31 | | - connectionConfig1.setConnectionUrl("<YOUR_CONNECTION_URL_1>"); // Replace with the URL of the first connection |
32 | | - connectionConfig1.setCredentials(credentials); // Associate credentials for the first connection |
| 29 | + // Step 2: Configure the first connection |
| 30 | + ConnectionConfig primaryConnectionConfig = new ConnectionConfig(); |
| 31 | + primaryConnectionConfig.setConnectionId("<YOUR_CONNECTION_ID_1>"); // Replace with first connection ID |
| 32 | + primaryConnectionConfig.setConnectionUrl("<YOUR_CONNECTION_URL_1>"); // Replace with first connection URL |
| 33 | + primaryConnectionConfig.setCredentials(credentials); // Assign credentials |
33 | 34 |
|
34 | | - // Step 3: Configure the second connection (Connection 2) |
35 | | - ConnectionConfig connectionConfig2 = new ConnectionConfig(); |
36 | | - connectionConfig2.setConnectionId("<YOUR_CONNECTION_ID_2>"); // Replace with the ID of the second connection |
37 | | - connectionConfig2.setConnectionUrl("<YOUR_CONNECTION_URL_2>"); // Replace with the URL of the second connection |
| 35 | + // Step 3: Configure the second connection |
| 36 | + ConnectionConfig secondaryConnectionConfig = new ConnectionConfig(); |
| 37 | + secondaryConnectionConfig.setConnectionId("<YOUR_CONNECTION_ID_2>"); // Replace with second connection ID |
| 38 | + secondaryConnectionConfig.setConnectionUrl("<YOUR_CONNECTION_URL_2>"); // Replace with second connection URL |
38 | 39 |
|
39 | 40 | // Step 4: Set up credentials for the Skyflow client |
40 | 41 | Credentials skyflowCredentials = new Credentials(); |
41 | | - skyflowCredentials.setPath("<YOUR_CREDENTIALS_FILE_PATH_2>"); // Replace with the path to another credentials file |
| 42 | + skyflowCredentials.setCredentialsString("<YOUR_CREDENTIALS_STRING>"); // Replace with the credentials string |
42 | 43 |
|
43 | | - // Step 5: Create a Skyflow client and add connection configurations |
| 44 | + // Step 5: Create a Skyflow client with connection configurations |
44 | 45 | Skyflow skyflowClient = Skyflow.builder() |
45 | | - .setLogLevel(LogLevel.DEBUG) // Enable debugging for detailed logs |
46 | | - .addConnectionConfig(connectionConfig1) // Add the first connection configuration |
47 | | - .addConnectionConfig(connectionConfig2) // Add the second connection configuration |
48 | | - .addSkyflowCredentials(skyflowCredentials) // Add general Skyflow credentials |
| 46 | + .setLogLevel(LogLevel.ERROR) // Set log level to ERROR |
| 47 | + .addConnectionConfig(primaryConnectionConfig) // Add the first connection |
| 48 | + .addConnectionConfig(secondaryConnectionConfig) // Add the second connection |
| 49 | + .addSkyflowCredentials(skyflowCredentials) // Provide Skyflow credentials |
49 | 50 | .build(); |
50 | 51 |
|
51 | 52 | // Example 1: Sending a POST request to the first connection |
52 | 53 | try { |
53 | | - // Set up the request body and headers for the POST request |
| 54 | + // Set up request body and headers |
54 | 55 | Map<String, String> requestBody = new HashMap<>(); |
55 | | - requestBody.put("<COLUMN_NAME_1>", "<COLUMN_VALUE_1>"); // Replace with the actual column name and value |
| 56 | + requestBody.put("<COLUMN_NAME_1>", "<COLUMN_VALUE_1>"); // Replace with actual column name and value |
56 | 57 | requestBody.put("<COLUMN_NAME_2>", "<COLUMN_VALUE_2>"); // Replace with another column name and value |
57 | 58 |
|
58 | 59 | Map<String, String> requestHeaders = new HashMap<>(); |
59 | | - requestHeaders.put("<HEADER_NAME_1>", "<HEADER_VALUE_1>"); // Replace with header name and value |
| 60 | + requestHeaders.put("<HEADER_NAME_1>", "<HEADER_VALUE_1>"); // Replace with actual header name and value |
60 | 61 | requestHeaders.put("<HEADER_NAME_2>", "<HEADER_VALUE_2>"); // Replace with another header name and value |
61 | 62 |
|
62 | | - // Build the POST request to invoke the connection |
| 63 | + // Build and send the POST request |
63 | 64 | InvokeConnectionRequest invokeConnectionRequest1 = InvokeConnectionRequest.builder() |
64 | | - .methodName(RequestMethod.POST) // Set the HTTP method to POST |
65 | | - .requestBody(requestBody) // Set the request body |
66 | | - .requestHeaders(requestHeaders) // Set the request headers |
| 65 | + .method(RequestMethod.POST) // HTTP method set to POST |
| 66 | + .requestBody(requestBody) // Include request body |
| 67 | + .requestHeaders(requestHeaders) // Include request headers |
67 | 68 | .build(); |
68 | 69 |
|
69 | | - // Execute the POST request to the first connection |
70 | 70 | InvokeConnectionResponse invokeConnectionResponse1 = skyflowClient.connection().invoke(invokeConnectionRequest1); |
71 | | - System.out.println("Invoke Connection Response (POST): " + invokeConnectionResponse1); // Print the response |
| 71 | + System.out.println("Invoke Connection Response (POST): " + invokeConnectionResponse1); |
72 | 72 | } catch (SkyflowException e) { |
73 | | - System.out.println("Error while invoking connection (POST):"); |
74 | | - e.printStackTrace(); |
| 73 | + System.out.println("Error while invoking connection (POST):" + e); |
75 | 74 | } |
76 | 75 |
|
77 | 76 | // Example 2: Sending a GET request to the second connection |
78 | 77 | try { |
79 | | - // Set up path parameters and query parameters for the GET request |
| 78 | + // Set up path and query parameters |
80 | 79 | Map<String, String> pathParams = new HashMap<>(); |
81 | | - pathParams.put("<YOUR_PATH_PARAM_KEY_1>", "<YOUR_PATH_PARAM_VALUE_1>"); // Replace with path parameters |
| 80 | + pathParams.put("<YOUR_PATH_PARAM_KEY_1>", "<YOUR_PATH_PARAM_VALUE_1>"); // Replace with actual path parameter |
82 | 81 | pathParams.put("<YOUR_PATH_PARAM_KEY_2>", "<YOUR_PATH_PARAM_VALUE_2>"); // Replace with another path parameter |
83 | 82 |
|
84 | 83 | Map<String, String> queryParams = new HashMap<>(); |
85 | | - queryParams.put("<YOUR_QUERY_PARAM_KEY_1>", "<YOUR_QUERY_PARAM_VALUE_1>"); // Replace with query parameters |
| 84 | + queryParams.put("<YOUR_QUERY_PARAM_KEY_1>", "<YOUR_QUERY_PARAM_VALUE_1>"); // Replace with actual query parameter |
86 | 85 | queryParams.put("<YOUR_QUERY_PARAM_KEY_2>", "<YOUR_QUERY_PARAM_VALUE_2>"); // Replace with another query parameter |
87 | 86 |
|
88 | | - // Build the GET request to invoke the connection |
| 87 | + // Build and send the GET request |
89 | 88 | InvokeConnectionRequest invokeConnectionRequest2 = InvokeConnectionRequest.builder() |
90 | | - .methodName(RequestMethod.GET) // Set the HTTP method to GET |
91 | | - .pathParams(pathParams) // Set the path parameters |
92 | | - .queryParams(queryParams) // Set the query parameters |
| 89 | + .method(RequestMethod.GET) // HTTP method set to GET |
| 90 | + .pathParams(pathParams) // Include path parameters |
| 91 | + .queryParams(queryParams) // Include query parameters |
93 | 92 | .build(); |
94 | 93 |
|
95 | | - // Execute the GET request to the second connection |
96 | 94 | InvokeConnectionResponse invokeConnectionResponse2 = skyflowClient |
97 | | - .connection("<YOUR_CONNECTION_ID_2>").invoke(invokeConnectionRequest2); // Invoke connection with ID 2 |
98 | | - System.out.println("Invoke Connection Response (GET): " + invokeConnectionResponse2); // Print the response |
| 95 | + .connection("<YOUR_CONNECTION_ID_2>").invoke(invokeConnectionRequest2); |
| 96 | + System.out.println("Invoke Connection Response (GET): " + invokeConnectionResponse2); |
99 | 97 | } catch (SkyflowException e) { |
100 | | - System.out.println("Error while invoking connection (GET):"); |
101 | | - e.printStackTrace(); |
| 98 | + System.out.println("Error while invoking connection (GET):" + e); |
102 | 99 | } |
103 | 100 | } |
104 | 101 | } |
0 commit comments