Skip to content
This repository has been archived by the owner on Jan 29, 2019. It is now read-only.

Commit

Permalink
#196 add connector info endpoint, handle unknown exchange name
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Stavetski committed May 14, 2018
1 parent 4238c51 commit 591bf91
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,43 @@ class ConnectorService {
}

fun getTokensPairsByExchange(exchange: String): Set<TokensPair>? {
var pairs: Array<TokensPair>? = null
val apiUrl = connectorsMap[exchange]
val requestUri = apiUrl + EXCHANGE_TOKENS_PAIRS_PATH

val parameters = mutableMapOf<String, String>().apply {
put("exchangeName", exchange)
}
if (apiUrl != null) {
val requestUri = apiUrl + EXCHANGE_TOKENS_PAIRS_PATH
val parameters = mutableMapOf<String, String>().apply {
put("exchangeName", exchange)
}

var pairs: Array<TokensPair>? = null
try {
pairs = restTemplate.getForObject<Array<TokensPair>>(requestUri, Array<TokensPair>::class.java, parameters)
} catch (e: HttpClientErrorException) {
log.error("Cannot get tokens pairs for $exchange exchange. Status code: {}", e.rawStatusCode)
try {
pairs = restTemplate.getForObject<Array<TokensPair>>(requestUri, Array<TokensPair>::class.java, parameters)
} catch (e: HttpClientErrorException) {
log.error("Cannot get tokens pairs for $exchange exchange. Status code: {}", e.rawStatusCode)
}
}

return pairs?.toSet()

}

fun getOrderBook(exchange: String, pair: TokensPair): OrderBook? {
var orderBook: OrderBook? = null
val apiUrl = connectorsMap[exchange]
val requestUri = apiUrl + ORDERBOOK_PATH
val pairString = pair.base + "_" + pair.quote

val builder = UriComponentsBuilder.fromUriString(requestUri)
.queryParam("exchange", exchange)
.queryParam("pair", pairString)
if (apiUrl != null) {
val requestUri = apiUrl + ORDERBOOK_PATH
val pairString = pair.base + "_" + pair.quote

var orderBook: OrderBook? = null
try {
orderBook = restTemplate.getForObject<OrderBook>(builder.toUriString(), OrderBook::class.java)
} catch (e: HttpClientErrorException) {
log.error("Cannot get order book from $exchange and pair: $pair. Response status code: {}", e.rawStatusCode)
val builder = UriComponentsBuilder.fromUriString(requestUri)
.queryParam("exchange", exchange)
.queryParam("pair", pairString)

try {
orderBook = restTemplate.getForObject<OrderBook>(builder.toUriString(), OrderBook::class.java)
} catch (e: HttpClientErrorException) {
log.error("Cannot get order book from $exchange and pair: $pair. Response status code: {}", e.rawStatusCode)
}
}

return orderBook
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package fund.cyber.markets.api.rest.controller

import fund.cyber.markets.common.model.TokensPair
import fund.cyber.markets.common.rest.service.ConnectorService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.ResponseEntity
import org.springframework.http.ResponseEntity.notFound
import org.springframework.http.ResponseEntity.ok
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Mono
import reactor.core.publisher.toMono


@RestController
class ConnectorInfoController {

@Autowired
private lateinit var connectorService: ConnectorService

@GetMapping("/exchanges")
fun getExchanges(): Mono<ResponseEntity<Set<String>>> {

val exchanges = connectorService.getExchanges()

return if (exchanges != null) {
ok().body(exchanges).toMono()
} else {
notFound().build<Set<String>>().toMono()
}
}

@GetMapping("/exchange/{exchangeName}/pairs")
fun getTrades(
@PathVariable exchangeName: String
): Mono<ResponseEntity<Set<TokensPair>>> {
val pairs = connectorService.getTokensPairsByExchange(exchangeName)

return if (pairs != null && pairs.isNotEmpty()) {
ok().body(pairs).toMono()
} else {
notFound().build<Set<TokensPair>>().toMono()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,20 @@ import fund.cyber.markets.cassandra.repository.TradeRepository
import fund.cyber.markets.common.MILLIS_TO_HOURS
import fund.cyber.markets.common.closestSmallerMultiply
import fund.cyber.markets.common.convert
import fund.cyber.markets.common.model.OrderBook
import fund.cyber.markets.common.model.TokensPair
import fund.cyber.markets.common.rest.service.ConnectorService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.http.ResponseEntity.notFound
import org.springframework.http.ResponseEntity.ok
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.client.HttpClientErrorException
import org.springframework.web.client.RestTemplate
import org.springframework.web.util.UriComponentsBuilder
import reactor.core.publisher.Mono
import reactor.core.publisher.toMono


const val ORDERBOOK_PATH = "/orderbook"
const val ORDERBOOK_SNAPSHOT_PERIOD: Long = 10 * 60 * 1000

@RestController
Expand Down Expand Up @@ -56,19 +51,10 @@ class RawDataController {
val epochHour = nearestTs convert MILLIS_TO_HOURS
orderBook = orderBookRepository.getNearlest(exchange.toUpperCase(), CqlTokensPair(pair), epochHour, nearestTs)
} else {
val requestUri = connectorService.connectorsMap[exchange.toUpperCase()] + ORDERBOOK_PATH

val builder = UriComponentsBuilder.fromUriString(requestUri)
.queryParam("exchange", exchange.toUpperCase())
.queryParam("pair", pair)

try {
val orderBookRest = restTemplate.getForObject<OrderBook>(builder.toUriString(), OrderBook::class.java)
if (orderBookRest != null) {
orderBook = CqlOrderBook(exchange.toUpperCase(), TokensPair(pair), orderBookRest)
}
} catch (e: HttpClientErrorException) {
return Mono.just(ResponseEntity(HttpStatus.valueOf(e.rawStatusCode)))
val tokensPair = TokensPair(pair)
val currentOrderBook = connectorService.getOrderBook(exchange, tokensPair)
if (currentOrderBook != null) {
orderBook = CqlOrderBook(exchange, tokensPair, currentOrderBook)
}
}

Expand Down

0 comments on commit 591bf91

Please sign in to comment.