|
1 | 1 | package io.openfuture.api.component.state
|
2 | 2 |
|
3 |
| -import io.openfuture.api.client.BodyConverter |
4 |
| -import io.openfuture.api.client.HttpClientWrapper |
5 |
| -import io.openfuture.api.domain.PageResponse |
6 |
| -import io.openfuture.api.domain.state.* |
7 |
| -import org.springframework.beans.factory.annotation.Value |
8 |
| -import org.springframework.data.domain.PageRequest |
| 3 | +import io.openfuture.api.domain.state.CreateStateWalletRequest |
| 4 | +import io.openfuture.api.domain.state.StateWalletDto |
| 5 | +import io.openfuture.api.entity.state.Blockchain |
9 | 6 | import org.springframework.stereotype.Component
|
| 7 | +import org.springframework.web.client.RestTemplate |
10 | 8 |
|
11 | 9 | @Component
|
12 |
| -class DefaultStateApi( |
13 |
| - private val clientHttp: HttpClientWrapper, |
14 |
| - @Value("\${open.state.url}") private val stateUrl: String |
15 |
| -) : StateApi { |
| 10 | +class DefaultStateApi(private val stateRestTemplate: RestTemplate) : StateApi { |
16 | 11 |
|
17 |
| - override fun saveOpenScaffold(webHook: String, address: String) :OpenScaffoldDto { |
18 |
| - val url = "$stateUrl/open-scaffolds" |
19 |
| - val request = SaveScaffoldRequest(address, webHook) |
20 |
| - val response = clientHttp.post(url, prepareHeader(), request) |
21 |
| - |
22 |
| - return BodyConverter.deserialize(response.entity) |
23 |
| - } |
24 |
| - |
25 |
| - override fun createAccount(webHook: String?, address: String, blockchainId: Int): AccountDto { |
26 |
| - val url = "$stateUrl/accounts" |
27 |
| - val request = CreateAccountRequest(webHook, setOf(CreateIntegrationRequest(address, blockchainId))) |
28 |
| - val response = clientHttp.post(url, prepareHeader(), request) |
29 |
| - |
30 |
| - return BodyConverter.deserialize(response.entity) |
31 |
| - } |
32 |
| - |
33 |
| - override fun getAccount(id: Long): AccountDto { |
34 |
| - val url = "$stateUrl/accounts/$id" |
35 |
| - val response = clientHttp.get(url, prepareHeader()) |
36 |
| - |
37 |
| - return BodyConverter.deserialize(response.entity) |
38 |
| - } |
39 |
| - |
40 |
| - override fun updateWebhook(accountId: Long, webHook: String): AccountDto { |
41 |
| - val url = "$stateUrl/accounts" |
42 |
| - val request = UpdateAccountWebHookRequest(accountId, webHook) |
43 |
| - val response = clientHttp.put(url, prepareHeader(), request) |
44 |
| - |
45 |
| - return BodyConverter.deserialize(response.entity) |
46 |
| - } |
47 |
| - |
48 |
| - override fun deleteAccount(id: Long): AccountDto { |
49 |
| - val url = "$stateUrl/accounts/$id" |
50 |
| - val response = clientHttp.delete(url, prepareHeader()) |
51 |
| - |
52 |
| - return BodyConverter.deserialize(response.entity) |
53 |
| - } |
54 |
| - |
55 |
| - override fun addWallet(accountId: Long, address: String, blockchainId: Int): AccountDto { |
56 |
| - val url = "$stateUrl/accounts/$accountId/wallets" |
57 |
| - val request = AddWalletRequest(setOf(CreateIntegrationRequest(address, blockchainId))) |
58 |
| - val response = clientHttp.post(url, prepareHeader(), request) |
59 |
| - |
60 |
| - return BodyConverter.deserialize(response.entity) |
61 |
| - } |
62 |
| - |
63 |
| - override fun deleteWallet(accountId: Long, address: String, blockchainId: Int): AccountDto { |
64 |
| - val url = "$stateUrl/accounts/$accountId/wallets/delete" |
65 |
| - val request = DeleteWalletRequest(accountId, address, blockchainId) |
66 |
| - val response = clientHttp.post(url, prepareHeader(), request) |
67 |
| - |
68 |
| - return BodyConverter.deserialize(response.entity) |
69 |
| - } |
70 |
| - |
71 |
| - override fun getAllWalletsByAccount(accountId: Long): List<WalletDto> { |
72 |
| - val url = "$stateUrl/accounts/$accountId/wallets" |
73 |
| - val response = clientHttp.get(url, prepareHeader()) |
74 |
| - |
75 |
| - return BodyConverter.deserialize(response.entity) |
76 |
| - } |
77 |
| - |
78 |
| - override fun getWalletByAccount(id: Long, accountId: Long): WalletDto { |
79 |
| - val url = "$stateUrl/accounts/$accountId/wallets/$id" |
80 |
| - val response = clientHttp.get(url, prepareHeader()) |
81 |
| - |
82 |
| - return BodyConverter.deserialize(response.entity) |
83 |
| - } |
84 |
| - |
85 |
| - override fun getTransaction(id: Long, walletId: Long): StateTransactionDto { |
86 |
| - val url = "$stateUrl/wallets/$walletId/transactions/$id" |
87 |
| - val response = clientHttp.get(url, prepareHeader()) |
88 |
| - |
89 |
| - return BodyConverter.deserialize(response.entity) |
90 |
| - } |
91 |
| - |
92 |
| - override fun getAllTransactionsByWalletId(walletId: Long, pageRequest: PageRequest): PageResponse<StateTransactionDto> { |
93 |
| - val url = "$stateUrl/wallets/$walletId/transactions" |
94 |
| - val response = clientHttp.getPageable(url, prepareHeader(), pageRequest) |
95 |
| - |
96 |
| - return BodyConverter.deserialize(response.entity) |
97 |
| - } |
98 |
| - |
99 |
| - private fun prepareHeader(): Map<String, String> { |
100 |
| - return mapOf("Content-Type" to "application/json") |
| 12 | + override fun createWallet(address: String, webHook: String, blockchain: Blockchain): StateWalletDto { |
| 13 | + val request = CreateStateWalletRequest(address, webHook, blockchain) |
| 14 | + val response = stateRestTemplate.postForEntity("/wallets", request, StateWalletDto::class.java) |
| 15 | + return response.body!! |
101 | 16 | }
|
102 | 17 |
|
103 | 18 | }
|
0 commit comments