Skip to main content
Version: Current

Practical examples and use cases

This section demonstrates practical examples of using the Genesis HTTP Client in various Genesis components and common use cases.

Example 1: Request Server with external API call

This example shows how to use the Genesis HTTP Client in a Request Server to fetch data from an external API:

requestReply<TradeRequest, TradeDetails>("FETCH_TRADE_DETAILS") {
val client = GenesisHttpClient()

replySingle { request: TradeRequest ->
try {
val response = client.get<TradeApiResponse> {
url = "/trades/${request.tradeId}"
query("currency", request.currency)
}.data

TradeDetails(
id = response.data.id,
amount = response.data.amount,
currency = response.data.currency,
status = response.data.status
)
} catch (e: UnexpectedResponseException) {
when (e.statusCode) {
HttpStatusCode.NotFound -> throw NotFoundException("Trade not found")
else -> throw Exception("Error fetching trade: ${e.message}")
}
}
}
}

Example 2: Event Handler with external POST

This example demonstrates how to use the Genesis HTTP Client in an Event Handler to send a post to an external service:

eventHandler<TradeEvent> {
val client = GenesisHttpClient()

data class TradeNotification(
val tradeId: Long,
val status: String,
val timestamp: Long
)

onCommit { event ->
val notification = TradeNotification(
tradeId = event.tradeId,
status = event.status,
timestamp = event.timestamp
)

try {
client.post<Unit>(
path = "https://my-external-api/endpoint",
request = notification,
builder = {
header("Authorization ", "Bearer 123")
}
)
ack()
} catch (e: Exception) {
nack("Failed to send notification")
}
}
}

Example 3: Request Server with OpenAPI client

This example demonstrates how to use the OpenAPI-generated client service in a Request Server to get an account by id:


val tradeApiService = TradeApiService("http://localhost:8080")
tradeApiService.registerApiToken("Authorization", "Basic ABC123=")

requestReply<Int, Account>("GET_ACCOUNT_BY_ID") {
replySingle {
accountsApi.getAccountById(it.toLong())
}
}

Example 4 - Request Server with OpenAPI client inside an Event Handler

This example demonstrates how to use the OpenAPI-generated client service in an Event Handler to update a trade:


val tradeApiService = TradeApiService("http://localhost:8080")

eventHandler<TradeUpdateEvent> {

onCommit { event ->
val updatedTrade = NewTrade(
symbol = event.symbol,
quantity = event.quantity,
price = event.price
)

try {
tradeApiService.updateTrade(event.tradeId, updatedTrade)
} catch (e: Exception) {
logger.error("Failed to update trade in external system: ${e.message}")
}
}
}

Example 5 - Paginated request

This example demonstrates how to use the OpenAPI-generated client service in an Event Handler using a paginated requestReply:


val accountsApi = AccountControllerApi("http://localhost:8080")
accountsApi.registerApiToken("Authorization", "Basic ABC123=")

requestReply<FindAllUsingGETRequest, AccountWithRecordId>("ACCOUNTS_API") {
maxRetries = 5
replyList { request ->
try {
val response = accountsApi.findAllUsingGET(request)
val accounts = response.accounts.map { account ->
AccountWithRecordId(
account.accountNumber,
account.balance,
account.brokerId,
account.customerId,
account.owner
)
}

accounts
} catch (e: Exception) {
LOG.warn("Error sending request for accounts to external API", e)
emptyList()
}
}
}