Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement GetNormalizeAddress API #25

Merged
merged 5 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ jobs:
name: Lint
runs-on: ubuntu-18.04
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.17
- uses: actions/checkout@v3
- uses: golangci/golangci-lint-action@v2
- uses: golangci/golangci-lint-action@v3
with:
version: v1.44.2
test:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ Unofficially [kenall](https://kenall.jp/) (ケンオール) client written by Go
## Install

```shell
$ go get github.com/osamingo/go-kenall/v2@v2.2.2
$ go get github.com/osamingo/go-kenall/v2@v2.3.0
```

## APIs supported by this library

- [郵便番号検索API](https://kenall.jp/docs/api-introduction/#%E9%83%B5%E4%BE%BF%E7%95%AA%E5%8F%B7%E6%A4%9C%E7%B4%A2api)
- [住所正規化API](https://kenall.jp/docs/API/postalcode/#%E4%BD%8F%E6%89%80%E6%AD%A3%E8%A6%8F%E5%8C%96%E6%A9%9F%E8%83%BD)
- [市区町村API](https://kenall.jp/docs/api-introduction/#%E5%B8%82%E5%8C%BA%E7%94%BA%E6%9D%91api)
- [日本の祝日API](https://kenall.jp/docs/API/holidays/)
- [自己IPアドレス確認API](https://kenall.jp/docs/API/whoami/#get-whoami)
Expand Down
27 changes: 27 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/url"
"os"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -229,3 +230,29 @@ func (cli *Client) GetHolidaysByPeriod(ctx context.Context, from, to time.Time)
"to": []string{to.Format(RFC3339DateFormat)},
})
}

// A GetNormalizeAddressResponse is a result from the kenall service of the API to normalize address.
type GetNormalizeAddressResponse struct {
Version Version `json:"version"`
Query Query `json:"query"`
}

// GetNormalizeAddress requests to the kenall service to normalize address.
func (cli *Client) GetNormalizeAddress(ctx context.Context, address string) (*GetNormalizeAddressResponse, error) {
address = strings.TrimSpace(address)
if address == "" {
return nil, ErrInvalidArgument
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, cli.Endpoint+"/postalcode/?t="+address, nil)
if err != nil {
return nil, fmt.Errorf("kenall: failed to generate http request: %w", err)
}

var res GetNormalizeAddressResponse
if err := cli.sendRequest(req, &res); err != nil {
return nil, fmt.Errorf("kenall: failed to send request for kenall service: %w", err)
}

return &res, nil
}
92 changes: 92 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var (
whoamiResponse []byte
//go:embed testdata/holidays.json
holidaysResponse []byte
//go:embed testdata/search_address.json
searchAddressResponse []byte
)

func TestNewClient(t *testing.T) {
Expand Down Expand Up @@ -459,6 +461,53 @@ func TestClient_GetHolidaysByPeriod(t *testing.T) {
}
}

func TestClient_GetNormalizeAddress(t *testing.T) {
t.Parallel()

srv := runTestingServer(t)
t.Cleanup(func() {
srv.Close()
})

cases := map[string]struct {
endpoint string
token string
ctx context.Context
giveAddress string
checkAsError bool
wantError error
wantBlockLotNum string
}{
"Normal case": {endpoint: srv.URL, token: "opencollector", ctx: context.Background(), giveAddress: "東京都港区六本木六丁目10番1号六本木ヒルズ森タワー18F", checkAsError: false, wantError: nil, wantBlockLotNum: "6-10-1"},
"Empty case": {endpoint: srv.URL, token: "opencollector", ctx: context.Background(), giveAddress: "", checkAsError: true, wantError: kenall.ErrInvalidArgument, wantBlockLotNum: ""},
"Wrong response": {endpoint: srv.URL, token: "opencollector", ctx: context.Background(), giveAddress: "wrong", checkAsError: true, wantError: &json.MarshalerError{}, wantBlockLotNum: ""},
"nil context": {endpoint: srv.URL, token: "opencollector", ctx: nil, giveAddress: "東京都港区六本木六丁目10番1号六本木ヒルズ森タワー18F", checkAsError: true, wantError: &url.Error{}, wantBlockLotNum: ""},
}

for name, c := range cases {
c := c

t.Run(name, func(t *testing.T) {
t.Parallel()

cli, err := kenall.NewClient(c.token, kenall.WithEndpoint(c.endpoint))
if err != nil {
t.Error(err)
}

res, err := cli.GetNormalizeAddress(c.ctx, c.giveAddress)
if c.checkAsError && !errors.As(err, &c.wantError) {
t.Errorf("give: %v, want: %v", err, c.wantError)
} else if !errors.Is(err, c.wantError) {
t.Errorf("give: %v, want: %v", err, c.wantError)
}
if res != nil && res.Query.BlockLotNum.String != c.wantBlockLotNum {
t.Errorf("give: %v, want: %v", res.Query.BlockLotNum, c.wantBlockLotNum)
}
})
}
}

func ExampleClient_GetAddress() {
if testing.Short() {
// stab
Expand Down Expand Up @@ -590,6 +639,33 @@ func ExampleClient_GetHolidaysByYear() {
// 2022-01-01 元日
}

func ExampleClient_GetNormalizeAddress() {
if testing.Short() {
// stab
fmt.Print("false\n3-12-14 8F\n")

return
}

// NOTE: Please set a valid token in the environment variable and run it.
cli, err := kenall.NewClient(os.Getenv("KENALL_AUTHORIZATION_TOKEN"))
if err != nil {
log.Fatal(err)
}

res, err := cli.GetNormalizeAddress(context.Background(), "東京都千代田区麹町三丁目12-14麹町駅前ヒルトップ8F")
if err != nil {
log.Fatal(err)
}

q := res.Query
fmt.Println(time.Time(res.Version).IsZero())
fmt.Println(q.BlockLotNum.String, q.FloorRoom.String)
// Output:
// false
// 3-12-14 8F
}

func runTestingServer(t *testing.T) *httptest.Server {
t.Helper()

Expand Down Expand Up @@ -622,6 +698,22 @@ func runTestingServer(t *testing.T) *httptest.Server {
func handlePostalAPI(t *testing.T, w http.ResponseWriter, uri string) {
t.Helper()

if strings.HasPrefix(uri, "/postalcode/?") {
// nolint: errcheck
u, _ := url.Parse(uri)

switch u.Query().Get("t") {
case "東京都港区六本木六丁目10番1号六本木ヒルズ森タワー18F":
if _, err := w.Write(searchAddressResponse); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
case "wrong":
if _, err := w.Write([]byte("wrong")); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
}

switch uri {
case "/postalcode/1008105":
if _, err := w.Write(addressResponse); err != nil {
Expand Down
Loading