Skip to content

Commit

Permalink
updated category and berlin with optional parameters, berlin with new…
Browse files Browse the repository at this point in the history
… models
  • Loading branch information
KamenDimitrov97 committed Feb 26, 2024
1 parent 377605c commit bfe6895
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 20 deletions.
5 changes: 2 additions & 3 deletions nlp/berlin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Responsible for identifying geospatial data for more information read [this READ
import "github.com/ONSdigital/dp-api-clients-go/v2/nlp/berlin"

// Initialize the Berlin API client
client := berlin.New("https://berlinURL.com")
client := berlin.New("http://berlinURL.com")
```

#### Functionality
Expand All @@ -28,11 +28,10 @@ Once initialized you can make a request to Berlin like so:
```go
// Create an Options struct and set a query parameter 'q'
// you can also use url.Values directly into the Options
options := berlin.Options{}
options := berlin.OptInit()
options.Q("your_query_here")

// Add custom headers to the options
options.Headers = http.Header{}
options.Headers.Set(authHeader, "")
options.Headers.Set(someOtherHeader, "")

Expand Down
38 changes: 22 additions & 16 deletions nlp/berlin/berlin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,29 @@ var (
berlinResults = models.Berlin{
Matches: []models.Matches{
{
Codes: []string{
"testCode_1",
Loc: models.Locations{
Codes: []string{
"testCode_1",
},
Encoding: "encodingTest_1",
Names: []string{
"nameTest_1",
},
ID: "idTest_1",
Key: "keyTest_1",
State: []string{
"stateTest_1",
},
Subdivision: []string{
"subdiv1",
},
Words: []string{
"wordTest_1",
},
},
Encoding: "encodingTest_1",
Names: []string{
"nameTest_1",
},
ID: "idTest_1",
Key: "keyTest_1",
State: []string{
"stateTest_1",
},
Subdivision: []string{
"subdiv1",
},
Words: []string{
"wordTest_1",
Scores: models.Scores{
Offset: []int{0, 0},
Score: 1000,
},
},
},
Expand Down
11 changes: 11 additions & 0 deletions nlp/berlin/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package models

type Berlin struct {
Matches []Matches `json:"matches,omitempty"`
Query string `json:"query,omitempty"`
}

type Matches struct {
Loc Locations `json:"loc,omitempty"`
Scores Scores `json:"scores,omitempty"`
}

type Locations struct {
Codes []string `json:"codes,omitempty"`
Encoding string `json:"encoding,omitempty"`
Names []string `json:"names,omitempty"`
Expand All @@ -14,3 +20,8 @@ type Matches struct {
Subdivision []string `json:"subdiv,omitempty"`
Words []string `json:"words,omitempty"`
}

type Scores struct {
Offset []int `json:"offset,omitempty"`
Score int `json:"score,omitempty"`
}
37 changes: 37 additions & 0 deletions nlp/berlin/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,49 @@ type Options struct {
Query url.Values
}

// empty Options
func OptInit() Options {
return Options{
Query: url.Values{},
Headers: http.Header{},
}
}

// Q sets the 'q' Query parameter to the request
// Required
func (o *Options) Q(val string) *Options {
o.Query.Set("q", val)
return o
}

// State sets the 'state' Query parameter to the request
// Optional default is 'gb'
func (o *Options) State(val string) *Options {
o.Query.Set("state", val)
return o
}

// LevDist sets the 'lev_distance' Query parameter to the request
// Optional default is '2'
func (o *Options) LevDist(val string) *Options {
o.Query.Set("lev_distance", val)
return o
}

// Limit sets the 'limit' Query parameter to the request
// Optional default is '10'
func (o *Options) Limit(val string) *Options {
o.Query.Set("limit", val)
return o
}

// Limit sets the 'limit' Query parameter to the request
// Optional default is 0(false)
func (o *Options) WithScores(val string) *Options {
o.Query.Set("with_scores", val)
return o
}

func setHeaders(req *http.Request, headers http.Header) {
for name, values := range headers {
for _, value := range values {
Expand Down
2 changes: 1 addition & 1 deletion nlp/category/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Once initialized you can make a request to Category like so:
```go
// Create an Options struct and set a query parameter 'query'
// you can also use url.Values directly into the Options
options := category.Options{}
options := category.OptInit()
options.Q("your_query_here")

// Add custom headers to the options
Expand Down
14 changes: 14 additions & 0 deletions nlp/category/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,26 @@ type Options struct {
Query url.Values
}

// empty Options
func OptInit() Options {
return Options{
Query: url.Values{},
Headers: http.Header{},
}
}

// Q sets the 'q' Query parameter to the request
func (o *Options) Q(val string) *Options {
o.Query.Set("query", val)
return o
}

// Snr sets the 'snr' Query parameter to the request
func (o *Options) Snr(val string) *Options {
o.Query.Set("snr", val)
return o
}

func setHeaders(req *http.Request, headers http.Header) {
for name, values := range headers {
for _, value := range values {
Expand Down

0 comments on commit bfe6895

Please sign in to comment.