func (v *Version) UnmarshalJSON(data []byte) error {
	if string(data) == "null" {
		return nil
	}
	t, err := time.Parse(`"`+RFC3339DateFormat+`"`, string(data))
	if err != nil {
		return fmt.Errorf("kenall: failed to parse date with RFC3339 Date: %w", err)
	}
	*v = Version(t)
	return nil
}
UnmarshalJSON implements json.Unmarshaler interface.
package kenall
import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"strconv"
	"time"
)
const (
	Endpoint = "https://api.kenall.jp/v1"
	RFC3339DateFormat = "2006-01-02"
)
var (
	ErrInvalidArgument = fmt.Errorf("kenall: invalid argument")
	ErrUnauthorized = fmt.Errorf("kenall: 401 unauthorized error")
	ErrPaymentRequired = fmt.Errorf("kenall: 402 payment required error")
	ErrForbidden = fmt.Errorf("kenall: 403 forbidden error")
	ErrNotFound = fmt.Errorf("kenall: 404 not found error")
	ErrInternalServerError = fmt.Errorf("kenall: 500 internal server error")
	ErrBadGateway = fmt.Errorf("kenall: 502 bad gateway error")
)
type (
	Client struct {
		HTTPClient *http.Client
		Endpoint   string
		token string
	}
	ClientOption interface {
		Apply(*Client)
	}
	withHTTPClient struct {
		client *http.Client
	}
	withEndpoint struct {
		endpoint string
	}
	GetAddressResponse struct {
		Version   Version    `json:"version"`
		Addresses []*Address `json:"data"`
	}
	GetCityResponse struct {
		Version Version `json:"version"`
		Cities  []*City `json:"data"`
	}
	Version time.Time
	Address struct {
		JISX0402           string `json:"jisx0402"`
		OldCode            string `json:"old_code"`
		PostalCode         string `json:"postal_code"`
		PrefectureKana     string `json:"prefecture_kana"`
		CityKana           string `json:"city_kana"`
		TownKana           string `json:"town_kana"`
		TownKanaRaw        string `json:"town_kana_raw"`
		Prefecture         string `json:"prefecture"`
		City               string `json:"city"`
		Town               string `json:"town"`
		Koaza              string `json:"koaza"`
		KyotoStreet        string `json:"kyoto_street"`
		Building           string `json:"building"`
		Floor              string `json:"floor"`
		TownPartial        bool   `json:"town_partial"`
		TownAddressedKoaza bool   `json:"town_addressed_koaza"`
		TownChome          bool   `json:"town_chome"`
		TownMulti          bool   `json:"town_multi"`
		TownRaw            string `json:"town_raw"`
		Corporation        struct {
			Name       string `json:"name"`
			NameKana   string `json:"name_kana"`
			BlockLot   string `json:"block_lot"`
			PostOffice string `json:"post_office"`
			CodeType   int    `json:"code_type"`
		} `json:"corporation"`
	}
	City struct {
		JISX0402       string `json:"jisx0402"`
		PrefectureCode string `json:"prefecture_code"`
		CityCode       string `json:"city_code"`
		PrefectureKana string `json:"prefecture_kana"`
		CityKana       string `json:"city_kana"`
		Prefecture     string `json:"prefecture"`
		City           string `json:"city"`
	}
)
func NewClient(token string, opts ...ClientOption) (*Client, error) {
	if token == "" {
		return nil, ErrInvalidArgument
	}
	cli := &Client{
		HTTPClient: http.DefaultClient,
		Endpoint:   Endpoint,
		token:      token,
	}
	for _, opt := range opts {
		opt.Apply(cli)
	}
	return cli, nil
}
func WithHTTPClient(cli *http.Client) ClientOption {
	return &withHTTPClient{client: cli}
}
func WithEndpoint(endpoint string) ClientOption {
	return &withEndpoint{endpoint: endpoint}
}
func (cli *Client) GetAddress(ctx context.Context, postalCode string) (*GetAddressResponse, error) {
	if _, err := strconv.Atoi(postalCode); err != nil || len(postalCode) != 7 {
		return nil, ErrInvalidArgument
	}
	const path = "/postalCode/"
	req, err := http.NewRequestWithContext(ctx, http.MethodGet, cli.Endpoint+path+postalCode, nil)
	if err != nil {
		return nil, fmt.Errorf("kenall: failed to generate http request: %w", err)
	}
	var res GetAddressResponse
	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
}
func (cli *Client) GetCity(ctx context.Context, prefectureCode string) (*GetCityResponse, error) {
	if _, err := strconv.Atoi(prefectureCode); err != nil || len(prefectureCode) != 2 {
		return nil, ErrInvalidArgument
	}
	const path = "/cities/"
	req, err := http.NewRequestWithContext(ctx, http.MethodGet, cli.Endpoint+path+prefectureCode, nil)
	if err != nil {
		return nil, fmt.Errorf("kenall: failed to generate http request: %w", err)
	}
	var res GetCityResponse
	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
}
func (cli *Client) sendRequest(req *http.Request, res interface{}) error {
	req.Header.Add("Authorization", "token "+cli.token)
	resp, err := cli.HTTPClient.Do(req)
	if err != nil {
		return fmt.Errorf("kenall: failed to do http client with a request for kenall service: %w", err)
	}
	defer func() {
		defer resp.Body.Close()
		io.Copy(ioutil.Discard, resp.Body)
	}()
	switch resp.StatusCode {
	case http.StatusOK:
	case http.StatusUnauthorized:
		return ErrUnauthorized
	case http.StatusPaymentRequired:
		return ErrPaymentRequired
	case http.StatusForbidden:
		return ErrForbidden
	case http.StatusNotFound:
		return ErrNotFound
	case http.StatusInternalServerError:
		return ErrInternalServerError
	case http.StatusBadGateway:
		return ErrBadGateway
	default:
		return fmt.Errorf("kenall: not registered in the error handling, http status code = %d", resp.StatusCode)
	}
	if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
		return fmt.Errorf("kenall: failed to decode to response: %w", err)
	}
	return nil
}