Full API implementation
This commit is contained in:
@@ -3,43 +3,53 @@ package controllers
|
||||
import (
|
||||
"addrss/pkg/postal"
|
||||
"addrss/pkg/router"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Api struct{}
|
||||
|
||||
type ParseRequest struct {
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
func (a Api) AddRoutes() {
|
||||
router.AddPost("/v1/expand", expandAddress).Anonymous()
|
||||
router.AddPost("/v1/parse", parseAddress).Anonymous()
|
||||
router.AddGet("/suggestions", suggestions).Anonymous()
|
||||
router.AddGet("/parse", parse).Anonymous()
|
||||
}
|
||||
|
||||
func expandAddress(ctx *router.Context) {
|
||||
expansions := postal.ExpandAddress("1080 Brayden Ct. Hebron KY 41048")
|
||||
for i := 0; i < len(expansions); i++ {
|
||||
fmt.Println(expansions[i])
|
||||
}
|
||||
|
||||
ctx.Response.NoContent()
|
||||
}
|
||||
|
||||
func parseAddress(ctx *router.Context) {
|
||||
pr := ParseRequest{}
|
||||
if err := ctx.Request.Bind(&pr); err != nil {
|
||||
func suggestions(ctx *router.Context) {
|
||||
address, err := ctx.Request.Query("address")
|
||||
if err != nil {
|
||||
ctx.Response.BadRequest(err)
|
||||
}
|
||||
|
||||
options := postal.ParserOptions{}
|
||||
var parsedSlice []map[string]any
|
||||
|
||||
pa := postal.ParseAddressOptions(pr.Address, options)
|
||||
expansions := postal.ExpandAddress(address.(string))
|
||||
for i := 0; i < len(expansions); i++ {
|
||||
parsed := parseAddress(expansions[i])
|
||||
parsedSlice = append(parsedSlice, parsed)
|
||||
}
|
||||
|
||||
ctx.Response.OK(parsedSlice)
|
||||
}
|
||||
|
||||
func parse(ctx *router.Context) {
|
||||
address, err := ctx.Request.Query("address")
|
||||
if err != nil {
|
||||
ctx.Response.BadRequest(err)
|
||||
}
|
||||
|
||||
parsed := parseAddress(address.(string))
|
||||
ctx.Response.OK(parsed)
|
||||
}
|
||||
|
||||
func parseAddress(address string) map[string]any {
|
||||
pa := postal.ParseAddress(address)
|
||||
addr := map[string]any{}
|
||||
|
||||
for i := 0; i < len(pa); i++ {
|
||||
// This is hacky, but renaming in libpostal involves retraining the model.
|
||||
if pa[i].Label == "postcode" {
|
||||
pa[i].Label = "zip_code"
|
||||
}
|
||||
addr[pa[i].Label] = pa[i].Value
|
||||
}
|
||||
|
||||
ctx.Response.OK(addr)
|
||||
return addr
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"addrss/pkg/router"
|
||||
)
|
||||
|
||||
type Guestbook struct{}
|
||||
|
||||
type guestbookError struct {
|
||||
Fields []string `json:"fields"`
|
||||
}
|
||||
|
||||
func (g Guestbook) AddRoutes() {
|
||||
router.AddPost("/guestbook", signGuestbook).Anonymous()
|
||||
}
|
||||
|
||||
func signGuestbook(ctx *router.Context) {
|
||||
//gb := ctx.Request.Model.(repo.Guestbook)
|
||||
|
||||
ctx.Response.NoContent()
|
||||
}
|
||||
Reference in New Issue
Block a user