From c27fd1a8c6263799bc0ec4d99ea1fc1437bdeb2c Mon Sep 17 00:00:00 2001 From: Mikhail Gerasimchuk Date: Sun, 19 Nov 2023 15:42:31 +0700 Subject: [PATCH] Refactor repositories adapters from the native http go to the resty+gjson --- cmd/api/main.go | 4 +- cmd/verifier/main.go | 4 +- deployments/docker-compose/.env | 4 +- go.mod | 4 + go.sum | 34 ++++++ internal/adapter/repository/api/landpad.go | 61 ++++------ internal/adapter/repository/api/launchpad.go | 113 +++++++------------ internal/infrastructure/config/config.go | 2 +- 8 files changed, 107 insertions(+), 119 deletions(-) diff --git a/cmd/api/main.go b/cmd/api/main.go index 2191d39..9fa946b 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -43,8 +43,8 @@ func main() { } bookingRepo := pg.NewBookingRepository(db) - launchpadRepo := api.NewLaunchpadRepository(cfg.Launchpad.ApiBaseUri) - landpadRepo := api.NewLandpadRepository(cfg.Landpad.ApiBaseUri) + launchpadRepo := api.NewLaunchpadRepository(cfg.Launchpad.APIBaseURL) + landpadRepo := api.NewLandpadRepository(cfg.Landpad.APIBaseURL) bookingService := service.NewBookingVerifierService() bookingUsecase := usecase.NewBookingUsecase(bookingService, bookingRepo, launchpadRepo, landpadRepo) bookingController := controller.NewBookingController(bookingUsecase, logger) diff --git a/cmd/verifier/main.go b/cmd/verifier/main.go index ea926dd..8ea29de 100644 --- a/cmd/verifier/main.go +++ b/cmd/verifier/main.go @@ -41,8 +41,8 @@ func main() { } bookingRepo := pg.NewBookingRepository(db) - launchpadRepo := api.NewLaunchpadRepository(cfg.Launchpad.ApiBaseUri) - landpadRepo := api.NewLandpadRepository(cfg.Landpad.ApiBaseUri) + launchpadRepo := api.NewLaunchpadRepository(cfg.Launchpad.APIBaseURL) + landpadRepo := api.NewLandpadRepository(cfg.Landpad.APIBaseURL) bookingService := service.NewBookingVerifierService() bookingUsecase := usecase.NewBookingUsecase(bookingService, bookingRepo, launchpadRepo, landpadRepo) diff --git a/deployments/docker-compose/.env b/deployments/docker-compose/.env index aa2b6c7..270f257 100644 --- a/deployments/docker-compose/.env +++ b/deployments/docker-compose/.env @@ -14,9 +14,9 @@ DB_NAME=postgres DB_USER=postgres DB_PASSWORD=postgres -LAUNCHPAD_API_BASE_URI="https://api.spacexdata.com" +LAUNCHPAD_API_BASE_URL="https://api.spacexdata.com" -LANDPAD_API_BASE_URI="https://api.spacexdata.com" +LANDPAD_API_BASE_URL="https://api.spacexdata.com" HTTP_SERVER_PORT=8080 # see=vendor/github.com/gin-gonic/gin/mode.go diff --git a/go.mod b/go.mod index a2e5adb..b18f64e 100644 --- a/go.mod +++ b/go.mod @@ -6,12 +6,14 @@ require ( github.com/gavv/httpexpect/v2 v2.16.0 github.com/gin-gonic/gin v1.9.1 github.com/go-faker/faker/v4 v4.2.0 + github.com/go-resty/resty/v2 v2.10.0 github.com/google/uuid v1.4.0 github.com/jinzhu/gorm v1.9.16 github.com/kelseyhightower/envconfig v1.4.0 github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.8.4 + github.com/tidwall/gjson v1.17.0 github.com/toorop/gin-logrus v0.0.0-20210225092905-2c785434f26f go.uber.org/mock v0.3.0 golang.org/x/sync v0.5.0 @@ -54,6 +56,8 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sanity-io/litter v1.5.5 // indirect github.com/sergi/go-diff v1.0.0 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect diff --git a/go.sum b/go.sum index dae8c9f..6493f5e 100644 --- a/go.sum +++ b/go.sum @@ -46,6 +46,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE= github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-resty/resty/v2 v2.10.0 h1:Qla4W/+TMmv0fOeeRqzEpXPLfTUnR5HZ1+lGs+CkiCo= +github.com/go-resty/resty/v2 v2.10.0/go.mod h1:iiP/OpA0CkcL3IGt1O0+/SIItFUbkkyw5BGXiVdTu+A= github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= @@ -140,6 +142,12 @@ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/tailscale/depaware v0.0.0-20210622194025-720c4b409502/go.mod h1:p9lPsd+cx33L3H9nNoecRRxPssFKUwwI50I3pZ0yT+8= +github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM= +github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/toorop/gin-logrus v0.0.0-20210225092905-2c785434f26f h1:oqdnd6OGlOUu1InG37hWcCB3a+Jy3fwjylyVboaNMwY= github.com/toorop/gin-logrus v0.0.0-20210225092905-2c785434f26f/go.mod h1:X3Dd1SB8Gt1V968NTzpKFjMM6O8ccta2NPC6MprOxZQ= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= @@ -166,6 +174,7 @@ github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 h1:BHyfKlQyqbsFN5p3Ifn github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDfVJdfcVVdX+jpBxNmX4rDAzaS45IcYoM= github.com/yudai/pp v2.0.1+incompatible h1:Q4//iY4pNF6yPLZIigmvcl7k/bPgrcTPIFIcmawg5bI= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo= go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= @@ -176,23 +185,34 @@ golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA= golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -204,23 +224,37 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= +golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20201211185031-d93e913c1a58/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/internal/adapter/repository/api/landpad.go b/internal/adapter/repository/api/landpad.go index 41daf26..f309811 100644 --- a/internal/adapter/repository/api/landpad.go +++ b/internal/adapter/repository/api/landpad.go @@ -1,69 +1,50 @@ package api import ( - "encoding/json" "fmt" - "io" - "log" + "github.com/go-resty/resty/v2" + "github.com/tidwall/gjson" "net/http" ) type LandpadRepository struct { - apiBaseUri string + client *resty.Client } const landpadActiveStatus = "active" -func NewLandpadRepository(apiBaseUri string) *LandpadRepository { +func NewLandpadRepository(apiBaseURL string) *LandpadRepository { return &LandpadRepository{ - apiBaseUri: apiBaseUri, + client: resty.New(). + SetBaseURL(apiBaseURL). + SetHeader("Content-Type", "application/json"), } } func (r *LandpadRepository) IsExists(id string) (bool, error) { - _, statusCode, err := r.getLandpad(id) + resp, err := r.client.R(). + SetPathParam("landpadId", id). + Get("/v4/landpads/{landpadId}") if err != nil { return false, err } - - return statusCode == http.StatusOK, nil + if resp.StatusCode() != http.StatusOK && resp.StatusCode() != http.StatusNotFound { + return false, fmt.Errorf("got unexpected http status code: %d", resp.StatusCode()) + } + return resp.StatusCode() == http.StatusOK, nil } func (r *LandpadRepository) IsActive(id string) (bool, error) { - lp, _, err := r.getLandpad(id) + resp, err := r.client.R(). + SetPathParam("landpadId", id). + Get("/v4/landpads/{landpadId}") if err != nil { return false, err } - - return lp.Status == landpadActiveStatus, nil -} - -func (r *LandpadRepository) getLandpad(id string) (lp *landpad, statusCode int, err error) { - resp, err := http.Get(fmt.Sprintf("%s/v4/landpads/%s", r.apiBaseUri, id)) - - if err != nil { - return nil, 0, err - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound { - return nil, resp.StatusCode, fmt.Errorf("got unexpected http status code: %d", resp.StatusCode) + if resp.StatusCode() != http.StatusOK && resp.StatusCode() != http.StatusNotFound { + return false, fmt.Errorf("got unexpected http status code: %d", resp.StatusCode()) } - body, err := io.ReadAll(resp.Body) - if err != nil { - log.Fatalln(err) - } - - lp = new(landpad) - err = json.Unmarshal(body, lp) - if err != nil { - return lp, resp.StatusCode, err - } - - return lp, resp.StatusCode, nil -} - -type landpad struct { - Status string + respData := gjson.ParseBytes(resp.Body()) + return respData.Get("Status").String() == landpadActiveStatus, nil } diff --git a/internal/adapter/repository/api/launchpad.go b/internal/adapter/repository/api/launchpad.go index 8bf7e7d..8382da8 100644 --- a/internal/adapter/repository/api/launchpad.go +++ b/internal/adapter/repository/api/launchpad.go @@ -1,107 +1,76 @@ package api import ( - "bytes" - "encoding/json" "fmt" - "io" - "log" + "github.com/go-resty/resty/v2" + "github.com/tidwall/gjson" "net/http" "time" ) type LaunchpadRepository struct { - apiBaseUri string + client *resty.Client } const launchpadActiveStatus = "active" -func NewLaunchpadRepository(apiBaseUri string) *LaunchpadRepository { - return &LaunchpadRepository{apiBaseUri: apiBaseUri} +func NewLaunchpadRepository(apiBaseURL string) *LaunchpadRepository { + return &LaunchpadRepository{ + client: resty.New(). + SetBaseURL(apiBaseURL). + SetHeader("Content-Type", "application/json"), + } } func (r *LaunchpadRepository) IsExists(id string) (bool, error) { - _, statusCode, err := r.getLaunchpad(id) + resp, err := r.client.R(). + SetPathParam("launchpadId", id). + Get("/v4/launchpads/{launchpadId}") if err != nil { return false, err } - - return statusCode == http.StatusOK, nil + if resp.StatusCode() != http.StatusOK && resp.StatusCode() != http.StatusNotFound { + return false, fmt.Errorf("got unexpected http status code: %d", resp.StatusCode()) + } + return resp.StatusCode() == http.StatusOK, nil } func (r *LaunchpadRepository) IsActive(id string) (bool, error) { - lp, _, err := r.getLaunchpad(id) + resp, err := r.client.R(). + SetPathParam("launchpadId", id). + Get("/v4/launchpads/{launchpadId}") if err != nil { return false, err } + if resp.StatusCode() != http.StatusOK && resp.StatusCode() != http.StatusNotFound { + return false, fmt.Errorf("got unexpected http status code: %d", resp.StatusCode()) + } - return lp.Status == launchpadActiveStatus, nil + respData := gjson.ParseBytes(resp.Body()) + return respData.Get("Status").String() == launchpadActiveStatus, nil } func (r *LaunchpadRepository) IsDateAvailableForLaunch(launchpadID string, launchDate time.Time) (bool, error) { - requestBody := fmt.Sprintf(` -{ - "query": { - "launchpad": "%s", - "$and": [{"date_utc": {"$gte": "%s"}}, {"date_utc": {"$lt": "%s"}}] - } -} -`, launchpadID, launchDate.Format(time.RFC3339), launchDate.Add(24*time.Hour).Format(time.RFC3339)) - - resp, err := http.Post(fmt.Sprintf("%s/v4/launches/query", r.apiBaseUri), "application/json", bytes.NewBuffer([]byte(requestBody))) - if err != nil { - return false, err - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - return false, fmt.Errorf("got unexpected http status code: %d", resp.StatusCode) - } - - body, err := io.ReadAll(resp.Body) - if err != nil { - log.Fatalln(err) - } - - lqr := launchesQueryResult{} - err = json.Unmarshal(body, &lqr) + resp, err := r.client.R().SetBody(map[string]any{ + "query": map[string]any{ + "launchpad": launchpadID, + "$and": []map[string]any{ + { + "date_utc": map[string]any{"$gte": launchDate.Format(time.RFC3339)}, + }, + { + "date_utc": map[string]any{"$lt": launchDate.Add(24 * time.Hour).Format(time.RFC3339)}, + }, + }, + }, + }).Post("/v4/launches/query") if err != nil { return false, err } - - return lqr.TotalDocs == 0, nil -} - -func (r *LaunchpadRepository) getLaunchpad(id string) (lp *launchpad, statusCode int, err error) { - resp, err := http.Get(fmt.Sprintf("%s/v4/launchpads/%s", r.apiBaseUri, id)) - - if err != nil { - return nil, 0, err - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound { - return nil, resp.StatusCode, fmt.Errorf("got unexpected http status code: %d", resp.StatusCode) - } - - body, err := io.ReadAll(resp.Body) - if err != nil { - log.Fatalln(err) - } - - lp = new(launchpad) - err = json.Unmarshal(body, lp) - if err != nil { - return lp, resp.StatusCode, err + if resp.StatusCode() != http.StatusOK { + return false, fmt.Errorf("got unexpected http status code: %d", resp.StatusCode()) } - return lp, resp.StatusCode, nil -} - -type launchpad struct { - Status string -} - -type launchesQueryResult struct { - TotalDocs int + respData := gjson.ParseBytes(resp.Body()) + return respData.Get("TotalDocs").Int() == 0, nil } diff --git a/internal/infrastructure/config/config.go b/internal/infrastructure/config/config.go index 297ab02..26cb65f 100644 --- a/internal/infrastructure/config/config.go +++ b/internal/infrastructure/config/config.go @@ -16,7 +16,7 @@ type ( Password string `envconfig:"PASSWORD" required:"true"` } APIClientConfig struct { - ApiBaseUri string `envconfig:"API_BASE_URI" required:"true"` + APIBaseURL string `envconfig:"API_BASE_URL" required:"true"` } HTTPServerConfig struct { Port int `envconfig:"PORT" required:"true"`