Skip to content

Commit

Permalink
Make client closable (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
chehsunliu authored Apr 30, 2022
1 parent 402b58c commit 1a1b963
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
# fugle-kt
# fugle-kt

[![Build](https://github.com/chehsunliu/fugle-kt/actions/workflows/build.yml/badge.svg)](https://github.com/chehsunliu/fugle-kt/actions/workflows/build.yml)
[![Publish](https://github.com/chehsunliu/fugle-kt/actions/workflows/publish.yml/badge.svg)](https://github.com/chehsunliu/fugle-kt/actions/workflows/publish.yml)
[![Maven](https://img.shields.io/maven-central/v/io.github.chehsunliu.fuglekt/fugle-kt-core)](https://search.maven.org/artifact/io.github.chehsunliu.fuglekt/fugle-kt-core)

fugle-kt is a client for Fugle Realtime API.

## Installation

Fetch the dependency via Gradle:

```kotlin
implementation("io.github.chehsunliu.fuglekt:fugle-kt-core:0.2.0-beta.1")

implementation(platform("org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.6.1"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
```

## Usage

Get the metadata of TSMC:

```kotlin
import io.github.chehsunliu.fuglekt.core.FugleAsyncClient
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
FugleAsyncClient.create(token = "xxx").use { client ->
val response = client.getMeta(symbolId = "2330")
println(response)
}
}

// GetMetaResponse(apiVersion=0.3.0, data=Data(info=StockInfo(date=2022-04-29, type=EQUITY, exchange=TWSE, market=TSE, symbolId=2330, countryCode=TW, timeZone=Asia/Taipei, lastUpdatedAt=2022-04-29T13:30+08:00), meta=Meta(market=TSE, nameZhTw=台積電, industryZhTw=半導體業, priceReference=531.0, priceHighLimit=584.0, priceLowLimit=478.0, canDayBuySell=true, canDaySellBuy=true, canShortMargin=true, canShortLend=true, tradingUnit=1000, currency=TWD, isTerminated=false, isSuspended=false, typeZhTw=一般股票, abnormal=正常, isUnusuallyRecommended=false, isNewlyCompiled=null)))
```

Here is the `FugleAsyncClient` interface:

```kotlin
interface FugleAsyncClient : Closeable {
suspend fun getMeta(symbolId: String, oddLot: Boolean? = null): GetMetaResponse
suspend fun getQuote(symbolId: String, oddLot: Boolean? = null): GetQuoteResponse
suspend fun getChart(symbolId: String, oddLot: Boolean? = null): GetChartResponse
suspend fun getDealts(symbolId: String, limit: Int? = null, offset: Int? = null, oddLot: Boolean? = null): GetDealtsResponse
suspend fun getVolumes(symbolId: String, oddLot: Boolean? = null): GetVolumesResponse
suspend fun getCandles(symbolId: String, startDate: LocalDate, endDate: LocalDate): GetCandlesResponse
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ internal class DefaultFugleAsyncClient(private val baseUrl: HttpUrl, private val
return execute<GetCandlesResponse>(request).await()
}

override fun close() {
client.dispatcher.executorService.shutdown()
}

private fun HttpUrl.Builder.configureUrlBuilder(): HttpUrl.Builder =
this.scheme(baseUrl.scheme)
.host(baseUrl.host)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import io.github.chehsunliu.fuglekt.core.model.GetDealtsResponse
import io.github.chehsunliu.fuglekt.core.model.GetMetaResponse
import io.github.chehsunliu.fuglekt.core.model.GetQuoteResponse
import io.github.chehsunliu.fuglekt.core.model.GetVolumesResponse
import java.io.Closeable
import java.time.LocalDate
import okhttp3.HttpUrl.Companion.toHttpUrl

interface FugleAsyncClient {
interface FugleAsyncClient : Closeable {
suspend fun getMeta(symbolId: String, oddLot: Boolean? = null): GetMetaResponse

suspend fun getQuote(symbolId: String, oddLot: Boolean? = null): GetQuoteResponse
Expand Down
18 changes: 18 additions & 0 deletions fugle-kt-example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
kotlin("jvm") version "1.6.21"
id("com.github.johnrengelman.shadow") version "7.1.2"
}

repositories {
mavenCentral()
}

dependencies {
implementation(project(":fugle-kt-core"))

implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

implementation(platform("org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.6.1"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.chehsunliu.fuglekt.example

import io.github.chehsunliu.fuglekt.core.FugleAsyncClient
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
FugleAsyncClient.create(token = System.getenv("FUGLE_TOKEN")).use { client ->
val response = client.getMeta(symbolId = "2330")
println(response)
}
}

0 comments on commit 1a1b963

Please sign in to comment.