add methods for polygons

This commit is contained in:
diPhantxm
2023-03-23 00:22:27 +03:00
parent c1661f8cb9
commit 021a577bf9
5 changed files with 273 additions and 4 deletions

View File

@@ -74,9 +74,9 @@
- [x] List of delivery methods for a warehouse
## Polygons
- [ ] Create delivery polygon
- [ ] Link delivery method to a delivery polygon
- [ ] Delete polygon
- [x] Create delivery polygon
- [x] Link delivery method to a delivery polygon
- [x] Delete polygon
## FBO
- [x] Shipments list

View File

@@ -25,6 +25,7 @@ type Client struct {
reports *Reports
cancellations *Cancellations
categories *Categories
polygons *Polygons
}
func (c Client) Analytics() *Analytics {
@@ -75,6 +76,10 @@ func (c Client) Categories() *Categories {
return c.categories
}
func (c Client) Polygons() *Polygons {
return c.polygons
}
func NewClient(clientId, apiKey string) *Client {
coreClient := core.NewClient(DefaultAPIBaseUrl, map[string]string{
"Client-Id": clientId,
@@ -95,6 +100,7 @@ func NewClient(clientId, apiKey string) *Client {
reports: &Reports{client: coreClient},
cancellations: &Cancellations{client: coreClient},
categories: &Categories{client: coreClient},
polygons: &Polygons{client: coreClient},
}
}
@@ -115,5 +121,6 @@ func NewMockClient(handler http.HandlerFunc) *Client {
reports: &Reports{client: coreClient},
cancellations: &Cancellations{client: coreClient},
categories: &Categories{client: coreClient},
polygons: &Polygons{client: coreClient},
}
}

110
ozon/polygons.go Normal file
View File

@@ -0,0 +1,110 @@
package ozon
import (
"net/http"
core "github.com/diphantxm/ozon-api-client"
)
type Polygons struct {
client *core.Client
}
type CreateDeliveryPolygonParams struct {
// Delivery polygon coordinates in [[[lat long]]] format
Coordinates string `json:"coordinates"`
}
type CreateDeliveryPolygonResponse struct {
core.CommonResponse
// Polygon identifier
PolygonId int64 `json:"polygon_id"`
}
// You can link a polygon to the delivery method.
//
// Create a polygon getting its coordinates on https://geojson.io: mark at least 3 points on the map and connect them
func (c Polygons) CreateDelivery(params *CreateDeliveryPolygonParams) (*CreateDeliveryPolygonResponse, error) {
url := "/v1/polygon/create"
resp := &CreateDeliveryPolygonResponse{}
response, err := c.client.Request(http.MethodPost, url, params, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}
type LinkDeliveryMethodToPolygonParams struct {
// Delivery method identifier
DeliveryMethodId int32 `json:"delivery_method_id"`
// Polygons list
Polygons []LinkDeliveryMethodToPolygonPolygon `json:"polygons"`
// Warehouse location
WarehouseLocation LinkDeliveryMethodToPolygonWarehouse `json:"warehouse_location"`
}
type LinkDeliveryMethodToPolygonPolygon struct {
// Polygon identifier
PolygonId int64 `json:"polygon_id"`
// Delivery time within polygon in minutes
Time int64 `json:"time"`
}
type LinkDeliveryMethodToPolygonWarehouse struct {
// Warehouse location latitude
Latitude string `json:"lat"`
// Warehouse location longitude
Longitude string `json:"log"`
}
type LinkDeliveryMethodToPolygonResponse struct {
core.CommonResponse
}
// Link delivery method to a delivery polygon
func (c Polygons) Link(params *LinkDeliveryMethodToPolygonParams) (*LinkDeliveryMethodToPolygonResponse, error) {
url := "/v1/polygon/bind"
resp := &LinkDeliveryMethodToPolygonResponse{}
response, err := c.client.Request(http.MethodPost, url, params, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}
type DeletePolygonParams struct {
// Polygons identifiers list
PolygonIds []int64 `json:"polygon_ids"`
}
type DeletePolygonResponse struct {
core.CommonResponse
}
// Delete polygon
func (c Polygons) Delete(params *DeletePolygonParams) (*DeletePolygonResponse, error) {
url := "/v1/polygon/delete"
resp := &DeletePolygonResponse{}
response, err := c.client.Request(http.MethodPost, url, params, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}

152
ozon/polygons_test.go Normal file
View File

@@ -0,0 +1,152 @@
package ozon
import (
"net/http"
"testing"
core "github.com/diphantxm/ozon-api-client"
)
func TestCreateDeliveryPolygon(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
params *CreateDeliveryPolygonParams
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
&CreateDeliveryPolygonParams{
Coordinates: "[[[30.149574279785153,59.86550435303646],[30.21205902099609,59.846884387977326],[30.255661010742184,59.86240174913176],[30.149574279785153,59.86550435303646]]]",
},
`{
"polygonId": "1323"
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
&CreateDeliveryPolygonParams{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Polygons().CreateDelivery(test.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}
func TestLinkDeliveryMethodToPolygon(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
params *LinkDeliveryMethodToPolygonParams
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
&LinkDeliveryMethodToPolygonParams{
DeliveryMethodId: 0,
Polygons: []LinkDeliveryMethodToPolygonPolygon{
{
PolygonId: 1323,
Time: 30,
},
},
WarehouseLocation: LinkDeliveryMethodToPolygonWarehouse{
Latitude: "58.52391272075821",
Longitude: "31.236791610717773",
},
},
`{}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
&LinkDeliveryMethodToPolygonParams{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Polygons().Link(test.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}
func TestDeletePolygon(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
params *DeletePolygonParams
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
&DeletePolygonParams{
PolygonIds: []int64{1323},
},
`{}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
&DeletePolygonParams{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Polygons().Delete(test.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}

View File

@@ -2361,7 +2361,7 @@ func TestGetMarkdownInfo(t *testing.T) {
}
}
func TestSetDiscountOnMarkdownProductParams(t *testing.T) {
func TestSetDiscountOnMarkdownProduct(t *testing.T) {
t.Parallel()
tests := []struct {