Skip to content

Commit

Permalink
Merge pull request #17 from raul782/issue-16-add-support-scala3
Browse files Browse the repository at this point in the history
issue-16 add support for scala 3
  • Loading branch information
raul782 authored Aug 29, 2021
2 parents 5ba579c + 00b8cdf commit 9ca0b62
Show file tree
Hide file tree
Showing 42 changed files with 192 additions and 180 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: scala
scala:
- 2.13.6
- 2.12.14
- 3.0.1
script:
- sbt ++$TRAVIS_SCALA_VERSION compile test publishLocal
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
CHANGELOG
=========

3.0.0 - Add support for scala 3

Scala 2
-------
0.2.0 - Upgrade sttp client 3
0.1.7 - Coingecko scala client
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ any backend that sttp supports.
## Setup with sbt

Add the following dependency:
Scala 3
```scala
"com.besselstudio.coingecko" %% "client" % "3.0.0"
```

Scala 2, we will backport updates in the branch 2.x
```scala
"com.besselstudio.coingecko" %% "client" % "0.2.0"
```
Expand All @@ -34,7 +39,7 @@ import scala.util.{Failure, Success, Try}

object CoingeckoApp extends App {
println(s"Coingecko App Start")
implicit lazy val backend: SttpBackend[Identity, Any] = HttpURLConnectionBackend()
given backend: SttpBackend[Identity, Any] = HttpURLConnectionBackend()
lazy val api = new CoingeckoApi()

lazy val client = new CoingeckoClientImpl(api)
Expand Down
6 changes: 4 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ReleaseTransformations._

ThisBuild / scalaVersion := "2.13.6"
ThisBuild / scalaVersion := "3.0.1"
ThisBuild / organization := "com.besselstudio"
ThisBuild / organizationName := "Bessel Studio"
ThisBuild / homepage := Some(url("https://github.com/besselstudio/coingecko"))
Expand All @@ -20,7 +20,9 @@ lazy val root = (project in file("."))
"scm:git:[email protected]:besselstudio/coingecko.git"
)
),
crossScalaVersions := Seq(scalaVersion.value, "2.12.14"),
scalacOptions ++= Seq(
"-Xmax-inlines", "64" //Solves: Maximal number of successive inlines (32) exceeded
),
resolvers ++= Seq(
Resolver.sonatypeRepo("releases"),
Resolver.sonatypeRepo("snapshots")
Expand Down
11 changes: 4 additions & 7 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import sbt._

object Version {
val sttpVersion = "3.3.6"
val sttpVersion = "3.3.13"
val scalaTestVersion = "3.2.9"
val playJsonVersion = "2.9.2"
val playJsonxVersion = "0.42.0"
val playJsonVersion = "2.10.0-RC5"
}

object Dependencies {
import Version._
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.2.2" % Test
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.2.9" % Test
lazy val playJson = "com.typesafe.play" %% "play-json" % playJsonVersion
lazy val sttpClient = "com.softwaremill.sttp.client3" %% "core" % sttpVersion
lazy val playJsonx = "ai.x" %% "play-json-extensions" % playJsonxVersion

lazy val common = Seq(
scalaTest,
playJson,
sttpClient,
playJsonx
sttpClient
)
}
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.5.2
sbt.version=1.5.5
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.besselstudio.coingecko
import sttp.client3.{Identity, Response, SttpBackend, UriContext, basicRequest}
import sttp.model.QueryParams

class CoingeckoApi(implicit val backend: SttpBackend[Identity, Any]) {
class CoingeckoApi(using val backend: SttpBackend[Identity, Any]) {
def get(endpoint: String, params: QueryParams): Identity[Response[Either[String, String]]] = {
val apiUrl = s"${CoingeckoApi.baseUrl}/$endpoint"
println(uri"${apiUrl}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.besselstudio.coingecko

import com.besselstudio.coingecko.model.response.BaseResponse
import play.api.libs.json.{Format, Json}

case class CoingeckoApiError(
code: Int,
error: String
)

object CoingeckoApiError {
implicit lazy val format: Format[CoingeckoApiError] = Json.format[CoingeckoApiError]
object CoingeckoApiError extends BaseResponse {
given Format[CoingeckoApiError] = Json.format[CoingeckoApiError]

def internalApiError(message: Option[String] = None): CoingeckoApiError =
CoingeckoApiError(500, s"${message.getOrElse("Coingecko Api Error")}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
package com.besselstudio.coingecko.client

import play.api.libs.json.{Format, JsError, Json, JsSuccess}
import com.besselstudio.coingecko.model.coins.CoinPrice.CoinWithCurrencies
import com.besselstudio.coingecko.{
CoingeckoApi,
CoingeckoApiError,
CoingeckoClient
}
import com.besselstudio.coingecko.{CoingeckoApi, CoingeckoApiError, CoingeckoClient}
import com.besselstudio.coingecko.model.coins.status.ProjectUpdate
import com.besselstudio.coingecko.model.coins.{
BaseCoin,
Coin,
CoinHistory,
CoinMarket,
CoinTicker,
MarketChart
}
import com.besselstudio.coingecko.model.events.{
EventCountries,
EventTypes,
Events
}
import com.besselstudio.coingecko.model.exchanges.{
BaseExchange,
Exchange,
ExchangeRates,
ExchangeTickers
}
import com.besselstudio.coingecko.model.coins.{BaseCoin, Coin, CoinHistory, CoinMarket, CoinTicker, MarketChart}
import com.besselstudio.coingecko.model.events.{EventCountries, EventTypes, Events}
import com.besselstudio.coingecko.model.exchanges.{BaseExchange, Exchange, ExchangeRates, ExchangeTickers}
import com.besselstudio.coingecko.model.global.Global
import com.besselstudio.coingecko.model.response.PingResponse
import play.api.libs.json.{JsError, JsSuccess, Json, Reads}
import sttp.model.QueryParams

class CoingeckoClientImpl(
api: CoingeckoApi
api: CoingeckoApi
) extends CoingeckoClient {

override def ping: Either[CoingeckoApiError, PingResponse] =
Expand Down Expand Up @@ -237,6 +217,7 @@ class CoingeckoClientImpl(
): Either[CoingeckoApiError, CoinHistory] = {
def buildQuery =
Map(
"date" -> Some(date),
"localization" -> localization.map(_.toString)
).filter(kv => kv._2.nonEmpty)
.map(kv => kv._1 -> kv._2.getOrElse(""))
Expand Down Expand Up @@ -474,8 +455,8 @@ class CoingeckoClientImpl(
override def getGlobal: Either[CoingeckoApiError, Global] =
get[Global](endpoint = "global", QueryParams())

def get[T](endpoint: String, queryParams: QueryParams)(
implicit reads: Reads[T]
protected def get[T](endpoint: String, queryParams: QueryParams)(
using Format[T]
): Either[CoingeckoApiError, T] =
api.get(endpoint, queryParams).body match {
case Left(json) =>
Expand All @@ -487,7 +468,8 @@ class CoingeckoClientImpl(

case Right(json) =>
Json.parse(json).validate[T] match {
case JsSuccess(value, _) => Right(value)
case JsSuccess(value, _) =>
Right(value)
case JsError(errors) =>
Left(
CoingeckoApiError
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.besselstudio.coingecko.model.coins

import com.besselstudio.coingecko.model.response.BaseResponse
import play.api.libs.json.{Json, Reads}
import play.api.libs.json.{Format, Json}

case class BaseCoin(
id: String,
symbol: String,
name: String
)

object BaseCoin extends BaseResponse{
implicit val baseCoinReads: Reads[BaseCoin] = Json.reads[BaseCoin]
object BaseCoin extends BaseResponse {
given Format[BaseCoin] = Json.format[BaseCoin]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ case class Coin(
)

object Coin extends BaseResponse {
implicit val format: Format[Coin] = Json.format[Coin]
given Format[Coin] = Json.format[Coin]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ case class CoinHistory(
name: String,
localization: Map[String, String],
image: Image,
marketData: MarketData,
communityData: CommunityData,
developerData: DeveloperData,
publicInterestStats: PublicInterestStats
marketData: Option[MarketData],
communityData: Option[CommunityData],
developerData: Option[DeveloperData],
publicInterestStats: Option[PublicInterestStats]
)

object CoinHistory extends BaseResponse {
implicit val format: Format[CoinHistory] = Json.format[CoinHistory]
given Format[CoinHistory] = Json.format[CoinHistory]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.besselstudio.coingecko.model.coins

import com.besselstudio.coingecko.model.coins.data.Roi
import com.besselstudio.coingecko.model.response.BaseResponse
import play.api.libs.json.{Json, Reads}
import play.api.libs.json.{Format, Json}

case class CoinMarket(
id: String,
Expand Down Expand Up @@ -30,5 +30,5 @@ case class CoinMarket(
)

object CoinMarket extends BaseResponse {
implicit val coinMarketReads: Reads[CoinMarket] = Json.reads[CoinMarket]
given Format[CoinMarket] = Json.format[CoinMarket]
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.besselstudio.coingecko.model.coins

import com.besselstudio.coingecko.model.coins.common.Ticker
import play.api.libs.json.{Json, Reads}
import com.besselstudio.coingecko.model.response.BaseResponse
import play.api.libs.json.{Format, Json}

case class CoinTicker(
name: String,
tickers: List[Ticker]
)

object CoinTicker {
implicit val reads: Reads[CoinTicker] = Json.reads[CoinTicker]
object CoinTicker extends BaseResponse {
given Format[CoinTicker] = Json.format[CoinTicker]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.besselstudio.coingecko.model.coins

import com.besselstudio.coingecko.model.response.BaseResponse
import play.api.libs.json.{Json, Reads}
import play.api.libs.json.{Format, Json}

case class MarketChart(
prices: List[List[String]],
Expand All @@ -10,5 +10,5 @@ case class MarketChart(
)

object MarketChart extends BaseResponse {
implicit val reads: Reads[MarketChart] = Json.reads[MarketChart]
given Format[MarketChart] = Json.format[MarketChart]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import com.besselstudio.coingecko.model.response.BaseResponse
import play.api.libs.json.{Format, Json}

case class Image(
thumb: String,
small: String,
large: String
thumb: Option[String],
small: Option[String],
large: Option[String]
)

object Image extends BaseResponse {
implicit val format: Format[Image] = Json.format[Image]
given Format[Image] = Json.format[Image]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.besselstudio.coingecko.model.coins.common

import com.besselstudio.coingecko.model.response.BaseResponse
import play.api.libs.json.{Json, Format}
import play.api.libs.json.{Format, Json}

case class Market(
name: String,
Expand All @@ -10,5 +10,5 @@ case class Market(
)

object Market extends BaseResponse {
implicit val format: Format[Market] = Json.format[Market]
given Format[Market] = Json.format[Market]
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ case class Ticker(
)

object Ticker extends BaseResponse {
implicit val format: Format[Ticker] = Json.format[Ticker]
given Format[Ticker] = Json.format[Ticker]
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ case class CodeAdditionsDeletions4Weeks(
)

object CodeAdditionsDeletions4Weeks extends BaseResponse {
implicit val format: Format[CodeAdditionsDeletions4Weeks] = Json.format[CodeAdditionsDeletions4Weeks]
given Format[CodeAdditionsDeletions4Weeks] = Json.format[CodeAdditionsDeletions4Weeks]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import com.besselstudio.coingecko.model.response.BaseResponse
import play.api.libs.json.{Format, Json}

case class CommunityData(
facebookLikes: Double,
twitterFollowers: Double,
redditAveragePosts48h: Double,
redditAverageComments48h: Double,
redditSubscribers: Double,
redditAccountsActive48h: Double,
telegramChannelUserCount: Double
facebookLikes: Option[Double],
twitterFollowers: Option[Double],
redditAveragePosts48h: Option[Double],
redditAverageComments48h: Option[Double],
redditSubscribers: Option[Double],
redditAccountsActive48h: Option[Double],
telegramChannelUserCount: Option[Double]
)

object CommunityData extends BaseResponse {
implicit val format: Format[CommunityData] = Json.format[CommunityData]
given Format[CommunityData] = Json.format[CommunityData]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import play.api.libs.json.{Format, Json}
case class DeveloperData(
forks: Long,
stars: Long,
subscribers: Long,
totalIssues: Long,
closedIssues: Long,
pullRequestsMerged: Long,
pullRequestContributors: Long,
codeAdditionsDeletions4Weeks: CodeAdditionsDeletions4Weeks,
commitCount4Weeks: Long,
last4WeeksCommitActivitySeries: List[Long]
subscribers: Option[Long],
totalIssues: Option[Long],
closedIssues: Option[Long],
pullRequestsMerged: Option[Long],
pullRequestContributors: Option[Long],
codeAdditionsDeletions4Weeks: Option[CodeAdditionsDeletions4Weeks],
commitCount4Weeks: Option[Long],
last4WeeksCommitActivitySeries: Option[List[Long]]
)

object DeveloperData extends BaseResponse {
implicit val format: Format[DeveloperData] = Json.format[DeveloperData]
given Format[DeveloperData] = Json.format[DeveloperData]
}
Loading

0 comments on commit 9ca0b62

Please sign in to comment.