-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from vseinstrumentiru/ctx
ctx
- Loading branch information
Showing
26 changed files
with
213 additions
and
257 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
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,63 +1,34 @@ | ||
package cdek | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"context" | ||
"net/http" | ||
"net/url" | ||
"path" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
) | ||
|
||
const ( | ||
citiesURL = "v1/location/cities/json" | ||
) | ||
|
||
//GetCities This method is used to load detailed information on cities. | ||
func (c Client) GetCities(filter map[CityFilter]string) (*GetCitiesResp, error) { | ||
func (c Client) GetCities(ctx context.Context, filter map[CityFilter]string) (*GetCitiesResp, error) { | ||
serverURL, err := url.Parse(c.apiURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
serverURL.Path = path.Join(serverURL.Path, citiesURL) | ||
|
||
queryString := serverURL.Query() | ||
for filterKey, value := range filter { | ||
queryString.Set(string(filterKey), value) | ||
qs := serverURL.Query() | ||
for k, v := range filter { | ||
qs.Set(string(k), v) | ||
} | ||
serverURL.RawQuery = queryString.Encode() | ||
|
||
reqURL := serverURL.String() | ||
serverURL.Path = path.Join(serverURL.Path, citiesURL) | ||
serverURL.RawQuery = qs.Encode() | ||
|
||
resp, err := http.Get(reqURL) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, serverURL.String(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer func() { | ||
_ = resp.Body.Close() | ||
}() | ||
|
||
body, _ := ioutil.ReadAll(resp.Body) | ||
|
||
var cities GetCitiesResp | ||
err = json.Unmarshal(body, &cities) | ||
if err != nil { | ||
var alertResponse AlertResponse | ||
err = json.Unmarshal(body, &alertResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
multiError := &multierror.Error{} | ||
for _, alert := range alertResponse.Alerts { | ||
multiError = multierror.Append(alert) | ||
} | ||
|
||
return nil, multiError.ErrorOrNil() | ||
} | ||
|
||
return &cities, nil | ||
return jsonReq[GetCitiesResp](req) | ||
} |
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
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,5 +1,7 @@ | ||
module github.com/vseinstrumentiru/cdek | ||
|
||
go 1.12 | ||
go 1.18 | ||
|
||
require github.com/hashicorp/go-multierror v1.0.0 | ||
|
||
require github.com/hashicorp/errwrap v1.0.0 // indirect |
Oops, something went wrong.