mirror of
https://github.com/retailcrm/api-client-go.git
synced 2025-04-03 13:13:37 +03:00
minor fix
This commit is contained in:
parent
c7f57a271b
commit
6fc29d2957
2 changed files with 21 additions and 56 deletions
51
v5/client.go
51
v5/client.go
|
@ -1,20 +1,20 @@
|
|||
package v5
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"github.com/google/go-querystring/query"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"github.com/google/go-querystring/query"
|
||||
)
|
||||
|
||||
const (
|
||||
versionedPrefix = "/api/v5"
|
||||
versionedPrefix = "/api/v5"
|
||||
unversionedPrefix = "/api"
|
||||
)
|
||||
|
||||
|
@ -43,7 +43,7 @@ func New(url string, key string) *Client {
|
|||
func (c *Client) getRequest(urlWithParameters string) ([]byte, int, error) {
|
||||
var res []byte
|
||||
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", c.Url , urlWithParameters), nil)
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", c.Url, urlWithParameters), nil)
|
||||
if err != nil {
|
||||
return res, 0, err
|
||||
}
|
||||
|
@ -119,7 +119,7 @@ func (c *Client) ErrorResponse(data []byte) (*ErrorResponse, error) {
|
|||
}
|
||||
|
||||
// checkBy select identifier type
|
||||
func checkBy(by string) string {
|
||||
func checkBy(by string) string {
|
||||
var context = "id"
|
||||
|
||||
if by != "id" {
|
||||
|
@ -133,15 +133,10 @@ func checkBy(by string) string {
|
|||
func (c *Client) ApiVersions() (*VersionResponse, int, error) {
|
||||
var resp VersionResponse
|
||||
data, status, err := c.getRequest(fmt.Sprintf("%s/api-versions", unversionedPrefix))
|
||||
|
||||
if err != nil {
|
||||
return &resp, status, err
|
||||
}
|
||||
|
||||
if status >= http.StatusBadRequest {
|
||||
return &resp, status, errors.New(fmt.Sprintf("HTTP request error. Status code: %d.\n", status))
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &resp)
|
||||
|
||||
return &resp, status, err
|
||||
|
@ -151,15 +146,10 @@ func (c *Client) ApiVersions() (*VersionResponse, int, error) {
|
|||
func (c *Client) ApiCredentials() (*CredentialResponse, int, error) {
|
||||
var resp CredentialResponse
|
||||
data, status, err := c.getRequest(fmt.Sprintf("%s/credentials", unversionedPrefix))
|
||||
|
||||
if err != nil {
|
||||
return &resp, status, err
|
||||
}
|
||||
|
||||
if status >= http.StatusBadRequest {
|
||||
return &resp, status, errors.New(fmt.Sprintf("HTTP request error. Status code: %d.\n", status))
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &resp)
|
||||
|
||||
return &resp, status, err
|
||||
|
@ -177,10 +167,6 @@ func (c *Client) Customer(id, by, site string) (*CustomerResponse, int, error) {
|
|||
return &resp, status, err
|
||||
}
|
||||
|
||||
if status >= http.StatusBadRequest {
|
||||
return &resp, status, errors.New(fmt.Sprintf("HTTP request error. Status code: %d.\n", status))
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &resp)
|
||||
|
||||
return &resp, status, err
|
||||
|
@ -202,21 +188,16 @@ func (c *Client) Customers(filter CustomersFilter, limit, page int) (*CustomersR
|
|||
params, _ := query.Values(fw)
|
||||
|
||||
data, status, err := c.getRequest(fmt.Sprintf("%s/customers?%s", versionedPrefix, params.Encode()))
|
||||
|
||||
if err != nil {
|
||||
return &resp, status, err
|
||||
}
|
||||
|
||||
if status >= http.StatusBadRequest {
|
||||
return &resp, status, errors.New(fmt.Sprintf("HTTP request error. Status code: %d.\n", status))
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &resp)
|
||||
|
||||
return &resp, status, err
|
||||
}
|
||||
|
||||
func (c *Client) CustomerCreate(customer Customer, site ...string) (*CustomerChangeResponse, int, error) {
|
||||
func (c *Client) CustomerCreate(customer Customer, site ...string) (*CustomerChangeResponse, int, error) {
|
||||
var resp CustomerChangeResponse
|
||||
customerJson, _ := json.Marshal(&customer)
|
||||
|
||||
|
@ -233,22 +214,16 @@ func (c *Client) CustomerCreate(customer Customer, site ...string) (*CustomerCha
|
|||
}
|
||||
|
||||
data, status, err := c.postRequest(fmt.Sprintf("%s/customers/create", versionedPrefix), p)
|
||||
|
||||
|
||||
if err != nil {
|
||||
return &resp, status, err
|
||||
}
|
||||
|
||||
if status >= http.StatusBadRequest {
|
||||
return &resp, status, errors.New(fmt.Sprintf("HTTP request error. Status code: %d.\n", status))
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &resp)
|
||||
|
||||
return &resp, status, err
|
||||
}
|
||||
|
||||
func (c *Client) CustomerEdit(customer Customer, by string, site ...string) (*CustomerChangeResponse, int, error) {
|
||||
func (c *Client) CustomerEdit(customer Customer, by string, site ...string) (*CustomerChangeResponse, int, error) {
|
||||
var resp CustomerChangeResponse
|
||||
var uid = strconv.Itoa(customer.Id)
|
||||
var context = checkBy(by)
|
||||
|
@ -260,7 +235,7 @@ func (c *Client) CustomerEdit(customer Customer, by string, site ...string) (*Cu
|
|||
customerJson, _ := json.Marshal(&customer)
|
||||
|
||||
p := url.Values{
|
||||
"by": {string(context)},
|
||||
"by": {string(context)},
|
||||
"customer": {string(customerJson[:])},
|
||||
}
|
||||
|
||||
|
@ -277,11 +252,11 @@ func (c *Client) CustomerEdit(customer Customer, by string, site ...string) (*Cu
|
|||
return &resp, status, err
|
||||
}
|
||||
|
||||
if status >= http.StatusBadRequest {
|
||||
return &resp, status, errors.New(fmt.Sprintf("HTTP request error. Status code: %d.\n", status))
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &resp)
|
||||
|
||||
return &resp, status, err
|
||||
}
|
||||
|
||||
func (c *Client) CustomersUpload() {
|
||||
|
||||
}
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
package v5
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
"os"
|
||||
"testing"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"encoding/json"
|
||||
"math/rand"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Configuration struct {
|
||||
|
@ -26,7 +26,7 @@ func buildConfiguration() *Configuration {
|
|||
fmt.Println("error:", err)
|
||||
}
|
||||
|
||||
return &Configuration {
|
||||
return &Configuration{
|
||||
configuration.Url,
|
||||
configuration.Key,
|
||||
configuration.Ver,
|
||||
|
@ -42,9 +42,11 @@ func init() {
|
|||
func RandomString(strlen int) string {
|
||||
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
|
||||
result := make([]byte, strlen)
|
||||
|
||||
for i := range result {
|
||||
result[i] = chars[r.Intn(len(chars))]
|
||||
}
|
||||
|
||||
return string(result)
|
||||
}
|
||||
|
||||
|
@ -55,7 +57,6 @@ func client() *Client {
|
|||
|
||||
func TestGetRequest(t *testing.T) {
|
||||
c := client()
|
||||
|
||||
_, status, _ := c.getRequest("/fake-method")
|
||||
|
||||
if status != http.StatusNotFound {
|
||||
|
@ -65,7 +66,6 @@ func TestGetRequest(t *testing.T) {
|
|||
|
||||
func TestPostRequest(t *testing.T) {
|
||||
c := client()
|
||||
|
||||
_, status, _ := c.postRequest("/fake-method", url.Values{})
|
||||
|
||||
if status != http.StatusNotFound {
|
||||
|
@ -75,9 +75,7 @@ func TestPostRequest(t *testing.T) {
|
|||
|
||||
func TestClient_ApiVersionsVersions(t *testing.T) {
|
||||
c := client()
|
||||
|
||||
data, status, err := c.ApiVersions()
|
||||
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
|
@ -93,9 +91,7 @@ func TestClient_ApiVersionsVersions(t *testing.T) {
|
|||
|
||||
func TestClient_ApiCredentialsCredentials(t *testing.T) {
|
||||
c := client()
|
||||
|
||||
data, status, err := c.ApiCredentials()
|
||||
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
|
@ -115,7 +111,6 @@ func TestClient_CustomersCustomers(t *testing.T) {
|
|||
f.City = "Москва"
|
||||
|
||||
data, status, err := c.Customers(f, 20, 1)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("%s", err)
|
||||
t.Fail()
|
||||
|
@ -132,8 +127,6 @@ func TestClient_CustomersCustomers(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func TestClient_CustomerChange(t *testing.T) {
|
||||
c := client()
|
||||
f := Customer{}
|
||||
|
@ -147,7 +140,6 @@ func TestClient_CustomerChange(t *testing.T) {
|
|||
f.Email = fmt.Sprintf("%s@example.com", random)
|
||||
|
||||
cr, sc, err := c.CustomerCreate(f)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("%s", err)
|
||||
t.Fail()
|
||||
|
@ -167,7 +159,6 @@ func TestClient_CustomerChange(t *testing.T) {
|
|||
f.Vip = true
|
||||
|
||||
ed, se, err := c.CustomerEdit(f, "id")
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("%s", err)
|
||||
t.Fail()
|
||||
|
@ -184,7 +175,6 @@ func TestClient_CustomerChange(t *testing.T) {
|
|||
}
|
||||
|
||||
data, status, err := c.Customer(f.ExternalId, "externalId", "")
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("%s", err)
|
||||
t.Fail()
|
||||
|
|
Loading…
Add table
Reference in a new issue