Query params must be implementation specific

main
Nikita Tokarchuk 2 years ago
parent 6311bf1dd7
commit 8e92ba0f9e
Signed by: mainnika
GPG Key ID: A595FB7E3E56911C
  1. 41
      frontend/ghost/client.go
  2. 6
      frontend/ghost/ghost.go
  3. 59
      frontend/ghost/params.go

@ -2,6 +2,7 @@ package ghost
import ( import (
"fmt" "fmt"
"strconv"
"sync" "sync"
"time" "time"
@ -45,7 +46,7 @@ func (g *HTTPClient) setupClient() {
} }
// doQuery does the method and unmarshals the result into the easyjson Unmarshaler // doQuery does the method and unmarshals the result into the easyjson Unmarshaler
func (g *HTTPClient) doQuery(method string, v easyjson.Unmarshaler, params ...QueryParam) (err error) { func (g *HTTPClient) doQuery(method string, v easyjson.Unmarshaler, params Params) (err error) {
g.setupClientOnce.Do(g.setupClient) g.setupClientOnce.Do(g.setupClient)
@ -57,9 +58,7 @@ func (g *HTTPClient) doQuery(method string, v easyjson.Unmarshaler, params ...Qu
}() }()
g.setupRequest(method, req) g.setupRequest(method, req)
for _, param := range params { g.applyParams(params, req)
param.Apply(&req.Header, uri.QueryArgs())
}
err = g.client.DoTimeout(req, res, g.QueryTimeout) err = g.client.DoTimeout(req, res, g.QueryTimeout)
if err != nil { if err != nil {
@ -103,13 +102,32 @@ func (g *HTTPClient) setupRequest(path string, req *fasthttp.Request) {
} }
} }
// applyParams function additionally configure the http request using params
func (g *HTTPClient) applyParams(p Params, req *fasthttp.Request) (err error) {
uri := req.URI()
limit := p.Limit
if limit > 0 {
uri.QueryArgs().Add("limit", strconv.Itoa(limit))
}
page := p.Page
if page > 1 {
uri.QueryArgs().Add("page", strconv.Itoa(page))
}
return
}
// GetPageBySlug returns the only one page using slug filter // GetPageBySlug returns the only one page using slug filter
func (g *HTTPClient) GetPageBySlug(slug string) (pages *Pages, err error) { func (g *HTTPClient) GetPageBySlug(slug string) (pages *Pages, err error) {
pages = &Pages{} pages = &Pages{}
defaultParams := Params{}
method := fmt.Sprintf(ghostAPIGetPageBySlug, slug) method := fmt.Sprintf(ghostAPIGetPageBySlug, slug)
err = g.doQuery(method, pages) err = g.doQuery(method, pages, defaultParams)
if err != nil { if err != nil {
pages = nil pages = nil
} }
@ -118,10 +136,13 @@ func (g *HTTPClient) GetPageBySlug(slug string) (pages *Pages, err error) {
} }
// GetPosts returns posts // GetPosts returns posts
func (g *HTTPClient) GetPosts(params ...QueryParam) (posts *Posts, err error) { func (g *HTTPClient) GetPosts(queryModifiers ...Modifier) (posts *Posts, err error) {
posts = &Posts{} posts = &Posts{}
err = g.doQuery(ghostAPIGetPosts, posts, params...) defaultParams := Params{}
combinedParams := Modifiers(queryModifiers).Apply(defaultParams)
err = g.doQuery(ghostAPIGetPosts, posts, combinedParams)
if err != nil { if err != nil {
posts = nil posts = nil
} }
@ -130,12 +151,14 @@ func (g *HTTPClient) GetPosts(params ...QueryParam) (posts *Posts, err error) {
} }
// GetPostBySlug returns the only one post using slug filter // GetPostBySlug returns the only one post using slug filter
func (g *HTTPClient) GetPostBySlug(slug string, params ...QueryParam) (posts *Posts, err error) { func (g *HTTPClient) GetPostBySlug(slug string, queryModifiers ...Modifier) (posts *Posts, err error) {
posts = &Posts{} posts = &Posts{}
defaultParams := Params{}
combinedParams := Modifiers(queryModifiers).Apply(defaultParams)
method := fmt.Sprintf(ghostAPIGetPostBySlug, slug) method := fmt.Sprintf(ghostAPIGetPostBySlug, slug)
err = g.doQuery(method, posts, params...) err = g.doQuery(method, posts, combinedParams)
if err != nil { if err != nil {
posts = nil posts = nil
} }

@ -4,7 +4,7 @@ package ghost
// Client is the ghost backend client // Client is the ghost backend client
type Client interface { type Client interface {
GetPosts(params ...QueryParam) (posts *Posts, err error) GetPosts(queryParams ...Modifier) (posts *Posts, err error)
GetPostBySlug(slug string, params ...QueryParam) (posts *Posts, err error) GetPostBySlug(slug string, queryParams ...Modifier) (posts *Posts, err error)
GetPageBySlug(slug string) (pages *Pages, err error) GetPageBySlug(slug string, queryParams ...Modifier) (pages *Pages, err error)
} }

@ -1,48 +1,39 @@
package ghost package ghost
import ( // Params are generics query argument
"strconv" type Params struct {
Limit int
"github.com/valyala/fasthttp" Page int
)
// QueryParam is the generic query param applier
type QueryParam interface {
Apply(headers *fasthttp.RequestHeader, args *fasthttp.Args)
} }
// QueryLimit returns limit param query // Modifier function takes params and makes some changes
func QueryLimit(limit int) queryLimit { type Modifier func(params Params) Params
return queryLimit{limit: limit}
}
// QueryPage returns page param query
func QueryPage(page int) queryPage {
return queryPage{page: page}
}
// queryLimit implements the limit param query applier // Modifiers is a list of modifier
type queryLimit struct{ limit int } type Modifiers []Modifier
// Apply applies the limit argument to the query // Apply function modifies params
func (ql queryLimit) Apply(headers *fasthttp.RequestHeader, args *fasthttp.Args) { func (ms Modifiers) Apply(params Params) Params {
if ql.limit == 0 { for _, m := range ms {
return params = m(params)
} }
args.Add("limit", strconv.Itoa(ql.limit)) return params
} }
// queryPage implements the page param query applier // WithLimit modifier setups the limit
type queryPage struct{ page int } func WithLimit(limit int) Modifier {
return func(params Params) Params {
// Apply applies the page argument to the query params.Limit = limit
func (qp queryPage) Apply(headers *fasthttp.RequestHeader, args *fasthttp.Args) { return params
if qp.page < 2 {
return
} }
}
args.Add("page", strconv.Itoa(qp.page)) // WithPage modifier setups the page
func WithPage(page int) Modifier {
return func(params Params) Params {
params.Page = page
return params
}
} }

Loading…
Cancel
Save