Skip to content

Commit

Permalink
Remove In.Request field in Ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
jsautret committed Mar 31, 2021
1 parent 8536945 commit 5d65910
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 48 deletions.
4 changes: 2 additions & 2 deletions ansible/templates/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
pipe:
- name: Run only on IFTTT dedicated endpoint
match:
string: =In.Req.URL.Path
string: =In.URL.Path
value: /ifttt
- name: must be HTTP GET
match:
string: =In.Req.Method
string: =In.Method
value: GET
- name: Body must be json
match:
Expand Down
4 changes: 2 additions & 2 deletions app/conf/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ s1: '=V.variable1'
{
name: "Request",
conf: `
s1: '=In.Req.Method'
s1: '=In.Method'
`,
expected: params{
S1: "POST",
Expand Down Expand Up @@ -225,7 +225,7 @@ l1:
t.Run(c.name, func(t *testing.T) {
conf := getConf(t, c.conf)
context := ctx.New()
context.In.Req.Method = "POST"
context.In.Method = "POST"
context.V = ctx.Variables{
"variable1": "value1",
"variable2": []string{"value21", "value22"},
Expand Down
6 changes: 3 additions & 3 deletions cmd/genapid/genapid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ func TestFullConf(t *testing.T) {
- name: "Test IncomingHttpMatching"
pipe:
- match:
string: =In.Req.Method
string: =In.Method
value: POST
- match:
string: =In.URL.Params.param2[0]
string: '=In.URL.Query()|param2[0]'
value: value2
- match:
string: =In.URL.Params.param1[0]
string: '=In.URL.Query()|param1[0]'
regexp: value[1-9]
- body:
mime: application/json
Expand Down
11 changes: 2 additions & 9 deletions cmd/genapid/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,8 @@ func process(w http.ResponseWriter, r *http.Request, c *ctx.Ctx) bool {

log.Trace().Interface("headers", r.Header).Msg("")

// Create & init context structures
url := &ctx.URL{
Params: r.URL.Query(),
}
In := ctx.Request{
Req: r,
URL: url,
}
c.In = In
// init context structures with incoming request
c.In = r

// Process each pipe
var res bool
Expand Down
13 changes: 2 additions & 11 deletions ctx/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// Ctx is the main entry point to the context
type Ctx struct {
// Incoming request
In Request
In *http.Request

// Default predicates values, set by 'default' predicate
Default Default
Expand All @@ -34,7 +34,7 @@ type Ctx struct {
// New returns a empty context
func New() *Ctx {
return &Ctx{
In: Request{Req: &http.Request{}},
In: &http.Request{},
Default: Default{},
R: Registered{},
V: Variables{},
Expand All @@ -46,15 +46,6 @@ type URL struct {
Params url.Values // map[string]string
}

// Request containas info about incoming request
type Request struct {
// Incoming HTTP request
Req *http.Request

// Imcoming URL info
URL *URL
}

// Registered stores results resgistered by a predicate
type Registered map[string]Result

Expand Down
10 changes: 6 additions & 4 deletions examples/github/github.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# See README.md in this directory

- init:
- name: Read tokens
- name: Read secret tokens
readfile:
yaml: "tokens.yml"
register: tokens

- name: "Incoming github request"
- name: Incoming github request
pipe:
- name: github dedicated endpoint
match:
string: =In.Req.URL.Path
string: =In.URL.Path
value: /github

- name: must be HTTP POST
match:
string: =In.Req.Method
string: =In.Method
value: POST

- name: Get body
Expand Down
9 changes: 5 additions & 4 deletions examples/kodi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ process.
The phrase received by Google Assistant is passed in the URL query
parameter we've setup in the Webhook in IFTTT, which is `q`.

In genapid, the URL query parameters are available in `In.URL.Params`.
In genapid, the URL query parameters are available in `In.URL.Query()`. We get the `q` field of the `Query()` result, and take the first one found: `In.URL.Query()|q[0]`
```
We use a [`match`](../../predicates/match/) predicate to match the
phrase against a regexp.
Expand All @@ -35,7 +36,7 @@ documenting and logs readability.
pipe:
- name: Match a phrase asking to mute sound
match:
string: = In.URL.Params.q[0]
string: = In.URL.Query()|q[0]
regexp: ^(mute|unmute)( the sound)?$
```

Expand Down Expand Up @@ -68,7 +69,7 @@ title. We register the results in `movie`.
pipe:
- name: Match a phrase asking to play a movie
match:
string: = In.URL.Params.q[0]
string: = In.URL.Query()|q[0]
regexp: "play (the movie )?(?P<title>.+)"
register: movie
```
Expand Down Expand Up @@ -134,7 +135,7 @@ predicate at the beginning of our main pipe:
- name: Set default values
default:
match: # Phrase from Google Assistant sent by IFTTT
string: = In.URL.Params.q[0]
string: = In.URL.Query()|q[0]
jsonrpc: # Kodi API params
url: http://192.168.0.32:8080/jsonrpc
basic_auth:
Expand Down
17 changes: 11 additions & 6 deletions examples/kodi/kodi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
pipe:
- name: IFTTT dedicated endpoint
match:
string: =In.Req.URL.Path
string: =In.URL.Path
value: /ifttt
- name: Must be HTTP POST
match:
string: =In.Req.Method
string: =In.Method
value: POST
- name: Parse JSON body
body:
Expand All @@ -20,10 +20,15 @@
match:
string: = R.body.payload.token
value: secretToken

- name: Get lang query param
variable:
- lang: '= In.URL.Query()|lang[0]'
- name: Read strings corresponding to lang param
readfile:
yaml: '="examples/kodi/kodi."+In.URL.Params.lang[0]+".yml"'
yaml: '="examples/kodi/kodi."+V.lang+".yml"'
register: strings

- name: Voices for TTS
variable:
- voices: # https://cloud.google.com/text-to-speech/docs/voices
Expand All @@ -37,7 +42,7 @@
- name: Set default values
default:
match: # Phrase from Google Assistant sent by IFTTT
string: = In.URL.Params.phrase[0]
string: '= In.URL.Query()|phrase[0]'
jsonrpc: # Kodi API params
url: http://192.168.0.32:8080/jsonrpc
basic_auth:
Expand All @@ -47,8 +52,8 @@
# Google Home IP addr
addr: 192.168.3.9
# TTS params
language_code: '=V.voices[In.URL.Params.lang[0]][0]'
voice_name: '=V.voices[In.URL.Params.lang[0]][1]'
language_code: '=V.voices[V.lang][0]'
voice_name: '=V.voices[V.lang][1]'
# Credentials to use Google API service for TTS
google_service_account: "~/.credentials/google_service_account.json"

Expand Down
8 changes: 4 additions & 4 deletions predicates/body/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (predicate *Predicate) Call(log zerolog.Logger, c *ctx.Ctx) bool {
log.Debug().Str("Type", p.Type).Msg("")
log.Debug().Str("Mime", p.Mime).Msg("")

switch c.In.Req.Method {
switch c.In.Method {
case "GET":
return false
case "HEAD":
Expand All @@ -47,7 +47,7 @@ func (predicate *Predicate) Call(log zerolog.Logger, c *ctx.Ctx) bool {
}

if p.Mime != "" { // checking if content-type match
contentType := c.In.Req.Header.Get("Content-Type")
contentType := c.In.Header.Get("Content-Type")
m, _, err := mime.ParseMediaType(contentType)
if err != nil {
log.Warn().Err(err).Msg("Cannot parse Content-Type")
Expand All @@ -59,7 +59,7 @@ func (predicate *Predicate) Call(log zerolog.Logger, c *ctx.Ctx) bool {
}
}
if p.Type != "" {
limitedReader := &io.LimitedReader{R: c.In.Req.Body, N: p.Limit}
limitedReader := &io.LimitedReader{R: c.In.Body, N: p.Limit}
// Use a TeeReade to allow to use body predicate several times
var b bytes.Buffer
tee := io.TeeReader(limitedReader, &b)
Expand All @@ -81,7 +81,7 @@ func (predicate *Predicate) Call(log zerolog.Logger, c *ctx.Ctx) bool {
predicate.results = ctx.Result{"payload": result}
}
// Put back the body in case the predicate is used later
c.In.Req.Body = ioutil.NopCloser(&b)
c.In.Body = ioutil.NopCloser(&b)
}
return true
}
Expand Down
2 changes: 1 addition & 1 deletion predicates/body/body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ mime: application/json
Req.Header = h
}
Req.Body = ioutil.NopCloser(strings.NewReader(tc.body))
c.In = ctx.Request{Req: &Req}
c.In = &Req
init := genapid.InitPredicate(log, c, p, cfg)
assert.Equal(t, !tc.invalidParam, init, "initPredicate")
if init {
Expand Down
2 changes: 1 addition & 1 deletion predicates/header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Predicate struct {
func (predicate *Predicate) Call(log zerolog.Logger, c *ctx.Ctx) bool {
p := predicate.params

v := c.In.Req.Header.Get(p.Name)
v := c.In.Header.Get(p.Name)
log.Debug().Str("name", p.Name).Msg("")
log.Debug().Str("value", v).Msg("")
predicate.result = ctx.Result{"value": v}
Expand Down
2 changes: 1 addition & 1 deletion predicates/header/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ name: key
h := http.Header{tc.header[0]: []string{tc.header[1]}}
Req.Header = h
}
c.In = ctx.Request{Req: &Req}
c.In = &Req
init := genapid.InitPredicate(log, c, p, cfg)
assert.Equal(t, !tc.invalidParam, init, "initPredicate")
if init {
Expand Down

0 comments on commit 5d65910

Please sign in to comment.