-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
113 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
"github.com/gorilla/context" | ||
) | ||
|
||
func AddMessage(r *http.Request, message string) { | ||
rawMessages, ok := context.GetOk(r, "messages") | ||
|
||
if ok { | ||
messages := rawMessages.([]string) | ||
messages = append(messages, message) | ||
context.Set(r, "messages", messages) | ||
} else { | ||
context.Set(r, "messages", []string{ message }) | ||
} | ||
} | ||
|
||
func GetMessages(r *http.Request) []string { | ||
rawMessages, ok := context.GetOk(r, "messages") | ||
|
||
if ok { | ||
return rawMessages.([]string) | ||
} else { | ||
return []string{} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package handler | ||
|
||
import ( | ||
"regexp" | ||
"net/http" | ||
"encoding/json" | ||
"strings" | ||
|
@@ -11,13 +12,30 @@ import ( | |
"github.com/untoldone/bloomapi/api" | ||
) | ||
|
||
var validElasticSearchRegexp = regexp.MustCompile(`\A[a-zA-Z0-9\-\_\:\.]+\z`) | ||
|
||
func ItemHandler (w http.ResponseWriter, req *http.Request) { | ||
vars := mux.Vars(req) | ||
source := strings.ToLower(vars["source"]) | ||
id := vars["id"] | ||
|
||
conn := api.Conn().SearchConnection() | ||
|
||
if !validElasticSearchRegexp.MatchString(source) { | ||
api.Render(w, req, http.StatusNotFound, "item not found") | ||
return | ||
} | ||
|
||
if !validElasticSearchRegexp.MatchString(id) { | ||
api.Render(w, req, http.StatusNotFound, "item not found") | ||
return | ||
} | ||
|
||
if source != "usgov.hhs.npi" { | ||
api.AddMessage(req, "Warning: Use the dataset, '" + source + "', without an API key is for development-use only. Use of this API without a key may be rate-limited in the future. For hosted, production access, please email '[email protected]' for an API key.") | ||
api.AddMessage(req, "Warning: The dataset, '" + source + "', is currently in beta and is subject to change. If you'd like to be notified before changes to this dataset, email '[email protected]' and ask for an API key referencing this message.") | ||
} | ||
|
||
result, err := conn.Get("source", source, id, nil) | ||
if err != nil && err.Error() == elastigo.RecordNotFound.Error() { | ||
api.Render(w, req, http.StatusNotFound, "item not found") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
package handler | ||
|
||
import ( | ||
"fmt" | ||
"encoding/json" | ||
"regexp" | ||
"net/http" | ||
"github.com/mattbaird/elastigo/lib" | ||
|
||
"github.com/untoldone/bloomapi/api" | ||
) | ||
|
||
var esTypeExceptionRegex = regexp.MustCompile(`FormatException`) | ||
|
||
func phraseMatches (paramSets []*SearchParamSet) []interface{} { | ||
var experimentalOperationMessage = "Warning: The operation, '%s', is currently in beta and is subject to change. If you'd like to be notified before changes to this functionality, email '[email protected]' and ask for an API key referencing this message." | ||
|
||
func phraseMatches (paramSets []*SearchParamSet, r *http.Request) []interface{} { | ||
elasticPhrases := make([]interface{}, len(paramSets)) | ||
for index, set := range paramSets { | ||
shouldValues := make([]map[string]interface{}, len(set.Values)) | ||
|
@@ -23,12 +27,14 @@ func phraseMatches (paramSets []*SearchParamSet) []interface{} { | |
}, | ||
} | ||
case "fuzzy": | ||
api.AddMessage(r, fmt.Sprintf(experimentalOperationMessage, "fuzzy")) | ||
shouldValues[vIndex] = map[string]interface{} { | ||
"fuzzy": map[string]interface{} { | ||
set.Key: value, | ||
}, | ||
} | ||
case "prefix": | ||
api.AddMessage(r, fmt.Sprintf(experimentalOperationMessage, "prefix")) | ||
shouldValues[vIndex] = map[string]interface{} { | ||
"prefix": map[string]interface{} { | ||
set.Key: value, | ||
|
@@ -79,15 +85,22 @@ func phraseMatches (paramSets []*SearchParamSet) []interface{} { | |
return elasticPhrases | ||
} | ||
|
||
func Search(sourceType string, params *SearchParams) (map[string]interface{}, error) { | ||
func Search(sourceType string, params *SearchParams, r *http.Request) (map[string]interface{}, error) { | ||
conn := api.Conn().SearchConnection() | ||
|
||
if sourceType != "usgov.hhs.npi" { | ||
api.AddMessage(r, "Warning: Use the dataset, '" + sourceType + "', without an API key is for development-use only. Use of this API without a key may be rate-limited in the future. For hosted, production access, please email '[email protected]' for an API key.") | ||
api.AddMessage(r, "Warning: The dataset, '" + sourceType + "', is currently in beta and is subject to change. If you'd like to be notified before changes to this dataset, email '[email protected]' and ask for an API key referencing this message.") | ||
} | ||
|
||
matches := phraseMatches(params.paramSets, r) | ||
|
||
query := map[string]interface{} { | ||
"from": params.Offset, | ||
"size": params.Limit, | ||
"query": map[string]interface{} { | ||
"bool": map[string]interface{} { | ||
"must": phraseMatches(params.paramSets), | ||
"must": matches, | ||
}, | ||
}, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"github.com/gorilla/context" | ||
) | ||
|
||
type ClearContext struct {} | ||
|
||
func NewClearContext() *ClearContext { | ||
return &ClearContext{} | ||
} | ||
|
||
func (s *ClearContext) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) { | ||
context.Clear(r) | ||
next(rw, r) | ||
} |