Skip to content

Commit

Permalink
Merge pull request #74 from geniusyield/openapi3-support
Browse files Browse the repository at this point in the history
Move to OpenApi 3.0
  • Loading branch information
brunjlar authored Jun 10, 2024
2 parents c30b532 + b501621 commit 0d2b055
Show file tree
Hide file tree
Showing 12 changed files with 1,373 additions and 1,345 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This repository houses on-chain smart contracts, Haskell off-chain interaction l
- [Setting up the API Server with Docker (Kupo)](#setting-up-the-API-Server-with-docker-kupo)
- [Building locally from source using Docker](#building-locally-from-source-using-docker)
- [Building locally from source using the Haskell Toolchain](#building-locally-from-source-using-the-haskell-toolchain)
- [Swagger API documentation](#swagger-api-documentation)
- [OpenApi documentation](#openapi-documentation)
- [Trading Strategy Executor (Python SDK)](#trading-strategy-executor-python-sdk)
- [Trading Bot API Video Tutorial](#trading-bot-api-video-tutorial)
- [Contributing](#contributing)
Expand Down Expand Up @@ -224,9 +224,9 @@ Alternatively you could also test using `make test`. This is sending a test GET
Further test requests are available in the Makefile of the strategy executor:
- [Make targets sending test HTTP requests to the Bot API](https://github.com/geniusyield/strategy-executor/blob/6b5a1b1d9f117831e409989335bd48875eef4189/Makefile#L32-L57)

### Swagger API documentation
### OpenApi documentation

Endpoints made available by server are specified [here](./web/swagger/api.yaml).
Endpoints made available by server are specified [here](./web/openapi/api.yaml).

### Trading Strategy Executor (Python SDK)

Expand Down
20 changes: 2 additions & 18 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ package strict-containers
source-repository-package
type: git
location: https://github.com/geniusyield/atlas
tag: 1c20f2a65de8e087b495d1f3ad524d6e659167ad
--sha256: sha256-mvzqgLwKHhIJdeHfUoIdapN0t2K85DoehgSkfm/ddIw=
tag: 7532a439525db20346f4b205fea49f970efe0a98
--sha256: sha256-UyLTCsbZVShxdhAu6fjBrILnyka3vOgBiOeNwW36xkk=

source-repository-package
type: git
Expand Down Expand Up @@ -117,22 +117,6 @@ source-repository-package
package cryptonite
flags: -support_rdrand

-- Using a fork until our patches can be merged upstream

-- TODO: ADP-1713
source-repository-package
type: git
location: https://github.com/biocad/servant-openapi3
tag: 4165b837d3a71debd1059c3735460075840000b5
--sha256: 1dngrr353kjhmwhn0b289jzqz5rf32llwcv79zcyq15ldpqpbib9

-- TODO: ADP-1713
source-repository-package
type: git
location: https://github.com/paolino/openapi3
tag: f22c31611c295637a3e72b341cd1c56d1d87b993
--sha256: 10l7wlaz9rcr3fysi1vwg7qqa826bb7nidkpx9jy1q7ja7ddw47i

source-repository-package
type: git
location: https://github.com/input-output-hk/cardano-addresses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ partialOrderInfoToIn
PartialOrderInfo
PartialOrderAction
GYTxIn 'PlutusV2
partialOrderInfoToIn a pocVersion pors oi@PartialOrderInfo {..} oa =
partialOrderInfoToIn a pocVersion pors PartialOrderInfo {..} oa =
let SomePORef PORef {..} = selectPor pors pocVersion
in GYTxIn
{ gyTxInTxOutRef = poiRef,
Expand Down
4 changes: 4 additions & 0 deletions geniusyield-server-lib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Revision history for geniusyield-server-lib

## 0.6.0 -- 2024-06-10

* Makes a move to OpenApi 3.0 specification from earlier 2.0.

## 0.5.1 -- 2024-06-06

* Incorporates 0.3.1.0 of `geniusyield-dex-api`.
Expand Down
5 changes: 3 additions & 2 deletions geniusyield-server-lib/geniusyield-server-lib.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.6
name: geniusyield-server-lib
version: 0.5.1
version: 0.6.0
synopsis: GeniusYield server library
description: Library for GeniusYield server.
license: Apache-2.0
Expand Down Expand Up @@ -97,14 +97,15 @@ library
, http-types
, insert-ordered-containers
, lens
, openapi3
, optparse-applicative
, rio
, servant
, servant-client
, servant-client-core
, servant-openapi3
, servant-foreign
, servant-server
, servant-swagger
, strict
, swagger2
, time
Expand Down
13 changes: 7 additions & 6 deletions geniusyield-server-lib/src/GeniusYield/Server/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module GeniusYield.Server.Api (
MainAPI,
mainAPI,
mainServer,
geniusYieldAPISwagger,
geniusYieldAPIOpenApi,
) where

import Control.Lens ((?~))
Expand All @@ -15,9 +15,9 @@ import Data.Aeson qualified as Aeson
import Data.Aeson.Key qualified as K
import Data.Kind (Type)
import Data.List (sortBy)
import Data.OpenApi
import Data.Strict qualified as Strict
import Data.Strict.Tuple
import Data.Swagger
import Data.Swagger qualified as Swagger
import Data.Swagger.Internal.Schema qualified as Swagger
import Data.Version (showVersion)
Expand All @@ -42,14 +42,15 @@ import GeniusYield.Server.Tx (TxAPI, handleTxApi)
import GeniusYield.Server.Utils
import GeniusYield.TxBuilder (GYTxQueryMonad (utxosAtAddress))
import GeniusYield.Types
import GeniusYield.Types.OpenApi ()
import PackageInfo_geniusyield_server_lib qualified as PackageInfo
import RIO hiding (asks, logDebug, logInfo)
import RIO.Char (toLower)
import RIO.List (isPrefixOf)
import RIO.Map qualified as Map
import RIO.Text qualified as T
import Servant
import Servant.Swagger
import Servant.OpenApi

{- $setup
Expand Down Expand Up @@ -213,9 +214,9 @@ infixr 4 +>
type family (+>) (api1 k) (api2 Type) where
(+>) api1 api2 = APIKeyAuthProtect :> V0 :> api1 :> api2

geniusYieldAPISwagger Swagger
geniusYieldAPISwagger =
toSwagger geniusYieldAPI
geniusYieldAPIOpenApi OpenApi
geniusYieldAPIOpenApi =
toOpenApi geniusYieldAPI
& info
. title
.~ "GeniusYield DEX Server API"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import GeniusYield.Server.Ctx
import GeniusYield.Server.Utils
import GeniusYield.Types
import Maestro.Client.V1 (pricesFromDex)
import Maestro.Types.V1 (Dex, OHLCCandleInfo (..), Order, Resolution, TaggedText (TaggedText))
import Maestro.Types.V1 (Dex (..), OHLCCandleInfo (..), Order (..), Resolution (..), TaggedText (TaggedText))
import RIO hiding (logDebug, logInfo)
import RIO.Text (unpack)
import RIO.Time (Day)
Expand Down Expand Up @@ -94,20 +94,50 @@ newtype MaestroOrder = MaestroOrder {unMaestroOrder ∷ Order}
instance Swagger.ToParamSchema MaestroOrder where
toParamSchema = commonEnumParamSchemaRecipe

instance Swagger.ToSchema MaestroOrder where
declareNamedSchema p =
pure $
Swagger.NamedSchema (Just "MaestroOrder") $
Swagger.paramSchemaToSchema p
& Swagger.example
?~ toJSON (MaestroOrder Ascending)
& Swagger.description
?~ "Order of the results"

newtype MaestroResolution = MaestroResolution {unMaestroResolution Resolution}
deriving stock (Show)
deriving newtype (ToHttpApiData, FromHttpApiData, FromJSON, ToJSON, Enum, Bounded)

instance Swagger.ToParamSchema MaestroResolution where
toParamSchema = commonEnumParamSchemaRecipe

instance Swagger.ToSchema MaestroResolution where
declareNamedSchema p =
pure $
Swagger.NamedSchema (Just "MaestroResolution") $
Swagger.paramSchemaToSchema p
& Swagger.example
?~ toJSON (MaestroResolution Res1m)
& Swagger.description
?~ "Resolution of the data"

newtype MaestroDex = MaestroDex {unMaestroDex Dex}
deriving stock (Show)
deriving newtype (ToHttpApiData, FromHttpApiData, FromJSON, ToJSON, Enum, Bounded)

instance Swagger.ToParamSchema MaestroDex where
toParamSchema = commonEnumParamSchemaRecipe

instance Swagger.ToSchema MaestroDex where
declareNamedSchema p =
pure $
Swagger.NamedSchema (Just "MaestroDex") $
Swagger.paramSchemaToSchema p
& Swagger.example
?~ toJSON (MaestroDex GeniusYield)
& Swagger.description
?~ "DEX to fetch data from"

type MaestroPriceHistoryAPI =
Summary "Get price history using Maestro."
:> Description ("This endpoint internally calls Maestro's \"DEX And Pair OHLC\" endpoint. " `AppendSymbol` CommonMaestroKeyRequirementText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module GeniusYield.Server.Dex.HistoricalPrices.TapTools.Client (
handleTapToolsError,
) where

import Control.Lens ((?~))
import Data.Aeson (ToJSON (..))
import Data.Swagger qualified as Swagger
import Data.Time.Clock.POSIX
Expand Down Expand Up @@ -77,6 +78,16 @@ instance FromHttpApiData TapToolsInterval where
instance Swagger.ToParamSchema TapToolsInterval where
toParamSchema = commonEnumParamSchemaRecipe

instance Swagger.ToSchema TapToolsInterval where
declareNamedSchema p =
pure $
Swagger.NamedSchema (Just "TapToolsInterval") $
Swagger.paramSchemaToSchema p
& Swagger.example
?~ toJSON TTI1M
& Swagger.description
?~ "The time interval"

type TapToolsOHLCVPrefix Symbol
type TapToolsOHLCVPrefix = "tapToolsOHLCV"

Expand Down
12 changes: 6 additions & 6 deletions geniusyield-server-lib/src/GeniusYield/Server/Orphans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ module GeniusYield.Server.Orphans () where

import Control.Lens (at, (?~))
import Data.HashMap.Strict.InsOrd qualified as IOHM
import Data.Swagger
import Data.OpenApi
import GeniusYield.Server.Auth (APIKeyAuthProtect, apiKeyHeaderText)
import RIO
import Servant
import Servant.Foreign
import Servant.Swagger
import Servant.OpenApi

instance HasSwagger api HasSwagger (APIKeyAuthProtect :> api) where
toSwagger _ =
toSwagger (Proxy Proxy api)
& securityDefinitions
instance HasOpenApi api HasOpenApi (APIKeyAuthProtect :> api) where
toOpenApi _ =
toOpenApi (Proxy Proxy api)
& (components . securitySchemes)
.~ SecurityDefinitions (IOHM.fromList [(apiKeyHeaderText, apiKeySecurityScheme)])
& allOperations
. security
Expand Down
2 changes: 1 addition & 1 deletion geniusyield-server-lib/src/GeniusYield/Server/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ runServer mfp = do
logErrorS = gyLogError providers mempty
logInfoS $ "GeniusYield server version: " +| showVersion PackageInfo.version |+ "\nCommit used: " +| gitHash |+ "\nOptional collateral configuration: " +|| scCollateral serverConfig ||+ "\nAddress of optional wallet: " +|| fmap Strict.snd optionalSigningKey ||+ "\nOptional stake address: " +|| scStakeAddress serverConfig ||+ ""
-- BL.writeFile "web/swagger/api.json" (encodePretty geniusYieldAPISwagger)
B.writeFile "web/swagger/api.yaml" (Yaml.encodePretty Yaml.defConfig geniusYieldAPISwagger)
B.writeFile "web/openapi/api.yaml" (Yaml.encodePretty Yaml.defConfig geniusYieldAPIOpenApi)
reqLoggerMiddleware gcpReqLogger
let
-- These are only meant to catch fatal exceptions, application thrown exceptions should be caught beforehand.
Expand Down
Loading

0 comments on commit 0d2b055

Please sign in to comment.