@@ -6,20 +6,25 @@ module GeniusYield.Server.Dex.HistoricalPrices.TapTools.Client (
6
6
TapToolsOHLCVAPI ,
7
7
tapToolsClientEnv ,
8
8
tapToolsOHLCV ,
9
+ tapToolsPrices ,
10
+ PricesResponse ,
9
11
TapToolsException ,
10
12
handleTapToolsError ,
11
13
) where
12
14
13
15
import Control.Lens ((?~) )
14
16
import Data.Aeson (ToJSON (.. ))
17
+ import Data.Aeson qualified as Aeson
18
+ import Data.Aeson.Types qualified as Aeson
19
+ import Data.Map.Strict qualified as Map
15
20
import Data.Swagger qualified as Swagger
16
21
import Data.Time.Clock.POSIX
17
22
import Deriving.Aeson
18
23
import GHC.TypeLits (Symbol , symbolVal )
19
24
import GeniusYield.Server.Ctx (TapToolsApiKey , TapToolsEnv (tteApiKey , tteClientEnv ))
20
25
import GeniusYield.Server.Utils (commonEnumParamSchemaRecipe , hideServantClientErrorHeader )
21
26
import GeniusYield.Swagger.Utils
22
- import GeniusYield.Types (GYAssetClass )
27
+ import GeniusYield.Types (GYAssetClass , makeAssetClass )
23
28
import Maestro.Types.Common (LowerFirst )
24
29
import Network.HTTP.Client (newManager )
25
30
import Network.HTTP.Client.TLS (tlsManagerSettings )
@@ -47,6 +52,25 @@ instance ToHttpApiData TapToolsUnit where
47
52
where
48
53
removeDot = Text. filter (/= ' .' )
49
54
55
+ instance Aeson. ToJSON TapToolsUnit where
56
+ toJSON = Aeson. toJSON . toUrlPiece
57
+
58
+ instance Aeson. ToJSONKey TapToolsUnit where
59
+ toJSONKey = Aeson. toJSONKeyText toUrlPiece
60
+
61
+ instance FromHttpApiData TapToolsUnit where
62
+ parseUrlPiece t =
63
+ let (pid, tn) = Text. splitAt 56 t
64
+ in bimap Text. pack TapToolsUnit $ makeAssetClass pid tn
65
+
66
+ instance Aeson. FromJSON TapToolsUnit where
67
+ parseJSON = Aeson. withText " TapToolsUnit" $ \ t → case parseUrlPiece t of
68
+ Left e → fail $ show e
69
+ Right ttu → pure ttu
70
+
71
+ instance Aeson. FromJSONKey TapToolsUnit where
72
+ fromJSONKey = Aeson. FromJSONKeyTextParser (either (fail . show ) pure . parseUrlPiece)
73
+
50
74
data TapToolsInterval = TTI3m | TTI5m | TTI15m | TTI30m | TTI1h | TTI2h | TTI4h | TTI12h | TTI1d | TTI3d | TTI1w | TTI1M
51
75
deriving stock (Eq , Ord , Enum , Bounded , Data , Typeable , Generic )
52
76
deriving (FromJSON , ToJSON ) via CustomJSON '[ConstructorTagModifier '[StripPrefix " TTI" ]] TapToolsInterval
@@ -111,22 +135,34 @@ instance Swagger.ToSchema TapToolsOHLCV where
111
135
& addSwaggerDescription " Get a specific token's trended (open, high, low, close, volume) price data."
112
136
& addSwaggerExample (toJSON $ TapToolsOHLCV {tapToolsOHLCVTime = 1_715_007_300 , tapToolsOHLCVOpen = open, tapToolsOHLCVHigh = open, tapToolsOHLCVLow = open, tapToolsOHLCVClose = open, tapToolsOHLCVVolume = 120 })
113
137
138
+ type PricesResponse = Map. Map TapToolsUnit Double
139
+
114
140
type TapToolsApiKeyHeaderName ∷ Symbol
115
141
type TapToolsApiKeyHeaderName = " x-api-key"
116
142
117
143
type TapToolsAPI =
118
- Header' '[Required ] TapToolsApiKeyHeaderName TapToolsApiKey :> TapToolsOHLCVAPI
144
+ Header' '[Required ] TapToolsApiKeyHeaderName TapToolsApiKey
145
+ :> " token"
146
+ :> (TapToolsOHLCVAPI :<|> TapToolsPricesAPI )
119
147
120
148
type TapToolsOHLCVAPI =
121
- " token"
122
- :> " ohlcv"
149
+ " ohlcv"
123
150
:> QueryParam " unit" TapToolsUnit
124
151
:> QueryParam' '[Required , Strict ] " interval" TapToolsInterval
125
152
:> QueryParam " numIntervals" Natural
126
153
:> Get '[JSON ] [TapToolsOHLCV ]
127
154
128
- _tapToolsOHLCV ∷ TapToolsApiKey → Maybe TapToolsUnit → TapToolsInterval → Maybe Natural → ClientM [TapToolsOHLCV ]
129
- _tapToolsOHLCV = client (Proxy @ TapToolsAPI )
155
+ type TapToolsPricesAPI = " prices" :> ReqBody '[JSON ] [TapToolsUnit ] :> Post '[JSON ] PricesResponse
156
+
157
+ data TapToolsClient = TapToolsClient
158
+ { tapToolsOHLCVClient ∷ Maybe TapToolsUnit → TapToolsInterval → Maybe Natural → ClientM [TapToolsOHLCV ],
159
+ tapToolsPricesClient ∷ [TapToolsUnit ] → ClientM PricesResponse
160
+ }
161
+
162
+ mkTapToolsClient ∷ TapToolsApiKey → TapToolsClient
163
+ mkTapToolsClient apiKey =
164
+ let tapToolsOHLCVClient :<|> tapToolsPricesClient = client (Proxy @ TapToolsAPI ) apiKey
165
+ in TapToolsClient {.. }
130
166
131
167
tapToolsBaseUrl ∷ String
132
168
tapToolsBaseUrl = " https://openapi.taptools.io/api/v1"
@@ -151,4 +187,7 @@ handleTapToolsError ∷ Text → Either ClientError a → IO a
151
187
handleTapToolsError locationInfo = either (throwIO . TapToolsApiError locationInfo . hideServantClientErrorHeader (fromString $ symbolVal (Proxy @ TapToolsApiKeyHeaderName ))) pure
152
188
153
189
tapToolsOHLCV ∷ TapToolsEnv → Maybe TapToolsUnit → TapToolsInterval → Maybe Natural → IO [TapToolsOHLCV ]
154
- tapToolsOHLCV env@ (tteApiKey → apiKey) ttu tti mttni = _tapToolsOHLCV apiKey ttu tti mttni & runTapToolsClient env >>= handleTapToolsError " tapToolsOHLCV"
190
+ tapToolsOHLCV env@ (tteApiKey → apiKey) ttu tti mttni = mkTapToolsClient apiKey & tapToolsOHLCVClient & (\ f → f ttu tti mttni) & runTapToolsClient env >>= handleTapToolsError " tapToolsOHLCV"
191
+
192
+ tapToolsPrices ∷ TapToolsEnv → [TapToolsUnit ] → IO PricesResponse
193
+ tapToolsPrices env@ (tteApiKey → apiKey) ttus = mkTapToolsClient apiKey & tapToolsPricesClient & (\ f → f ttus) & runTapToolsClient env >>= handleTapToolsError " tapToolsPrices"
0 commit comments