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

post mode #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 29 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package osrm

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand All @@ -21,28 +22,29 @@ type (
client struct {
httpClient HTTPClient
serverURL string
usePOST bool
}
)

// newClient creates a client with server url and specific getter
func newClient(serverURL string, c HTTPClient) client {
return client{c, serverURL}
func newClient(httpClient HTTPClient, serverURL string, usePOST bool) client {
return client{httpClient: httpClient, serverURL: serverURL, usePOST: usePOST}
}

// doRequest makes GET request to OSRM server and decodes the given JSON
func (c client) doRequest(ctx context.Context, in *request, out interface{}) error {
url, err := in.URL(c.serverURL)
path, err := in.URLPath()
if err != nil {
return err
}

resp, err := c.get(ctx, url)
resp, err := c.httpRequest(ctx, path)
if err != nil {
return err
}
defer closeSilently(resp.Body)

bytes, err := ioutil.ReadAll(resp.Body)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read body: %v", err)
}
Expand All @@ -51,17 +53,25 @@ func (c client) doRequest(ctx context.Context, in *request, out interface{}) err
// In other cases, it returns an unexpected error without a body.
// http://project-osrm.org/docs/v5.5.1/api/#responses
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusBadRequest {
return fmt.Errorf("unexpected http status code %d with body %q", resp.StatusCode, bytes)
return fmt.Errorf("unexpected http status code %d with body %q", resp.StatusCode, body)
}

if err := json.Unmarshal(bytes, out); err != nil {
return fmt.Errorf("failed to unmarshal body %q: %v", bytes, err)
if err := json.Unmarshal(body, out); err != nil {
return fmt.Errorf("failed to unmarshal body %q: %v", body, err)
}

return nil
}

func (c client) get(ctx context.Context, url string) (*http.Response, error) {
func (c client) httpRequest(ctx context.Context, path string) (*http.Response, error) {
if c.usePOST {
return c.post(ctx, path)
}
return c.get(ctx, path)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need get requests?

}

func (c client) get(ctx context.Context, path string) (*http.Response, error) {
url := c.serverURL + "/" + path
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
Expand All @@ -70,6 +80,16 @@ func (c client) get(ctx context.Context, url string) (*http.Response, error) {
return c.httpClient.Do(req.WithContext(ctx))
}

func (c client) post(ctx context.Context, path string) (*http.Response, error) {
req, err := http.NewRequest("POST", c.serverURL, bytes.NewReader([]byte(path)))
if err != nil {
return nil, err
}

req.Header.Add("Content-Type", "application/x-uri")
return c.httpClient.Do(req.WithContext(ctx))
}

func closeSilently(c io.Closer) {
// #nosec - make github.com/GoASTScanner/gas linter ignore this
_ = c.Close() // nothing meaningful to do with this error - so ignore and suppress linter warnings
Expand Down
4 changes: 3 additions & 1 deletion osrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type Config struct {
// Client is custom pre-configured http client to be used for queries.
// New http.Client instance with default settings and one second timeout will be used if not set.
Client HTTPClient
// Use POST method to request OSRM API
UsePOST bool
}

// ResponseStatus represent OSRM API response
Expand Down Expand Up @@ -84,7 +86,7 @@ func NewWithConfig(cfg Config) *OSRM {
cfg.Client = &http.Client{Timeout: defaultTimeout}
}

return &OSRM{client: newClient(cfg.ServerURL, cfg.Client)}
return &OSRM{client: newClient(cfg.Client, cfg.ServerURL, cfg.UsePOST)}
}

func (o OSRM) query(ctx context.Context, in *request, out response) error {
Expand Down
13 changes: 6 additions & 7 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ type request struct {
options options
}

// URL generates a url for OSRM request
func (r *request) URL(serverURL string) (string, error) {
// URLPath generates a url path for OSRM request
func (r *request) URLPath() (string, error) {
if r.service == "" {
return "", ErrEmptyServiceName
}
Expand All @@ -193,18 +193,17 @@ func (r *request) URL(serverURL string) (string, error) {
if r.coords.Length() == 0 {
return "", ErrNoCoordinates
}
// http://{server}/{service}/{version}/{profile}/{coordinates}[.{format}]?option=value&option=value
url := strings.Join([]string{
serverURL, // server
// {service}/{version}/{profile}/{coordinates}[.{format}]?option=value&option=value
u := strings.Join([]string{
r.service, // service
version, // version
r.profile, // profile
"polyline(" + url.PathEscape(r.coords.Polyline(polyline5Factor)) + ")", // coordinates
}, "/")
if len(r.options) > 0 {
url += "?" + r.options.encode() // options
u += "?" + r.options.encode() // options
}
return url, nil
return u, nil
}

// Bearing limits the search to segments with given bearing in degrees towards true north in clockwise direction.
Expand Down