Skip to content

Commit

Permalink
Include support to headers in URL Resource (#282)
Browse files Browse the repository at this point in the history
* feat: include support to headers in url resource

* fix lint
  • Loading branch information
ralphg6 authored Feb 18, 2022
1 parent 19ca461 commit 7934a3b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 10 deletions.
12 changes: 12 additions & 0 deletions docs/cn/Tutorial_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ if err != nil {
}
```

#### 帶標題

```go
headers := make(http.Header)
headers.Set("Authorization", "Basic YWxhZGRpbjpvcGVuc2VzYW1l")
urlRes := pkg.NewURLResourceWithHeaders("http://host.com/path/to/rule.grl", headers)
err := ruleBuilder.BuildRuleFromResource("TutorialRules", "0.0.1", urlRes)
if err != nil {
panic(err)
}
```

### 从Git获取

```go
Expand Down
12 changes: 12 additions & 0 deletions docs/de/Tutorial_de.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,18 @@ if err != nil {
}
```

#### With Headers

```go
headers := make(http.Header)
headers.Set("Authorization", "Basic YWxhZGRpbjpvcGVuc2VzYW1l")
urlRes := pkg.NewURLResourceWithHeaders("http://host.com/path/to/rule.grl", headers)
err := ruleBuilder.BuildRuleFromResource("TutorialRules", "0.0.1", urlRes)
if err != nil {
panic(err)
}
```

### From GIT

```go
Expand Down
12 changes: 12 additions & 0 deletions docs/en/Tutorial_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,18 @@ if err != nil {
}
```

#### With Headers

```go
headers := make(http.Header)
headers.Set("Authorization", "Basic YWxhZGRpbjpvcGVuc2VzYW1l")
urlRes := pkg.NewURLResourceWithHeaders("http://host.com/path/to/rule.grl", headers)
err := ruleBuilder.BuildRuleFromResource("TutorialRules", "0.0.1", urlRes)
if err != nil {
panic(err)
}
```

### From GIT

```go
Expand Down
12 changes: 12 additions & 0 deletions docs/id/Tutorial_id.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,18 @@ if err != nil {
}
```

#### With Headers

```go
headers := make(http.Header)
headers.Set("Authorization", "Basic YWxhZGRpbjpvcGVuc2VzYW1l")
urlRes := pkg.NewURLResourceWithHeaders("http://host.com/path/to/rule.grl", headers)
err := ruleBuilder.BuildRuleFromResource("TutorialRules", "0.0.1", urlRes)
if err != nil {
panic(err)
}
```

### From GIT

```go
Expand Down
10 changes: 5 additions & 5 deletions examples/EvaluateMissingDataContext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package examples

import (
"fmt"
"testing"

"github.com/hyperjumptech/grule-rule-engine/ast"
"github.com/hyperjumptech/grule-rule-engine/builder"
"github.com/hyperjumptech/grule-rule-engine/engine"
"github.com/hyperjumptech/grule-rule-engine/pkg"
"testing"
)

const (
input_rule = `
inputRule = `
rule TestRule "" {
when
R.Result == 'NoResult' &&
Expand All @@ -22,7 +23,6 @@ const (
`
)


func TestDataContextMissingFact(t *testing.T) {

oresult := &ObjectResult{
Expand All @@ -32,7 +32,7 @@ func TestDataContextMissingFact(t *testing.T) {
// build rules
lib := ast.NewKnowledgeLibrary()
rb := builder.NewRuleBuilder(lib)
err := rb.BuildRuleFromResource("Test", "0.0.1", pkg.NewBytesResource([]byte(input_rule)))
err := rb.BuildRuleFromResource("Test", "0.0.1", pkg.NewBytesResource([]byte(inputRule)))

// add JSON fact
json := []byte(`{"blabla":"bla","name":{"first":"john","last":"doe"}}`)
Expand All @@ -48,4 +48,4 @@ func TestDataContextMissingFact(t *testing.T) {
// results in panic
engine.NewGruleEngine().Execute(dcx, kb)

}
}
29 changes: 24 additions & 5 deletions pkg/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ package pkg

import (
"fmt"
"github.com/hyperjumptech/grule-rule-engine/logger"
"io"
"io/ioutil"
"net/http"
"path/filepath"

"github.com/hyperjumptech/grule-rule-engine/logger"

"github.com/bmatcuk/doublestar"
"gopkg.in/src-d/go-billy.v4"
)
Expand Down Expand Up @@ -201,14 +202,24 @@ func (res *BytesResource) String() string {
// NewURLResource will create a new Resource using a resource as located in the url
func NewURLResource(url string) Resource {
return &URLResource{
URL: url,
URL: url,
Header: make(http.Header),
}
}

// NewURLResourceWithHeaders will create a new Resource using a resource as located in the url with headers
func NewURLResourceWithHeaders(url string, Header http.Header) Resource {
return &URLResource{
URL: url,
Header: Header,
}
}

// URLResource is a struct that will hold the byte array data and URL source
type URLResource struct {
URL string
Bytes []byte
URL string
Header http.Header
Bytes []byte
}

// String will state the resource url.
Expand All @@ -224,7 +235,15 @@ func (res *URLResource) Load() ([]byte, error) {
if res.Bytes != nil {
return res.Bytes, nil
}
resp, err := http.Get(res.URL)
client := &http.Client{}
req, err := http.NewRequest("GET", res.URL, nil)
if len(res.Header) > 0 {
req.Header = res.Header
}
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7934a3b

Please sign in to comment.