api-client-go/client.go

5646 lines
138 KiB
Go
Raw Permalink Normal View History

package retailcrm
2018-02-27 11:37:15 +03:00
import (
2019-09-04 11:38:21 +03:00
"bytes"
2018-02-27 11:37:15 +03:00
"encoding/json"
2019-09-09 15:01:17 +03:00
"errors"
2018-02-27 11:37:15 +03:00
"fmt"
2019-09-04 11:38:21 +03:00
"io"
2018-02-27 11:37:15 +03:00
"io/ioutil"
"log"
2018-02-27 11:37:15 +03:00
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/google/go-querystring/query"
)
// New initialize client.
2018-02-27 11:37:15 +03:00
func New(url string, key string) *Client {
return &Client{
2022-04-18 12:23:18 +03:00
URL: strings.TrimRight(url, "/"),
Key: key,
2019-02-04 11:59:05 +03:00
httpClient: &http.Client{Timeout: time.Minute},
2018-02-27 11:37:15 +03:00
}
}
// WithLogger sets the provided logger instance into the Client.
func (c *Client) WithLogger(logger BasicLogger) *Client {
c.logger = logger
return c
}
// writeLog writes to the log.
func (c *Client) writeLog(format string, v ...interface{}) {
if c.logger != nil {
c.logger.Printf(format, v...)
return
}
log.Printf(format, v...)
}
// GetRequest implements GET Request.
func (c *Client) GetRequest(urlWithParameters string, versioned ...bool) ([]byte, int, error) {
2018-02-27 11:37:15 +03:00
var res []byte
var prefix = "/api/v5"
if len(versioned) > 0 {
if !versioned[0] {
prefix = "/api"
}
}
2018-02-27 11:37:15 +03:00
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s%s", c.URL, prefix, urlWithParameters), nil)
2018-02-27 11:37:15 +03:00
if err != nil {
return res, 0, err
2018-02-27 11:37:15 +03:00
}
req.Header.Set("X-API-KEY", c.Key)
if c.Debug {
c.writeLog("API Request: %s %s", fmt.Sprintf("%s%s%s", c.URL, prefix, urlWithParameters), c.Key)
}
2018-02-27 11:37:15 +03:00
resp, err := c.httpClient.Do(req)
if err != nil {
return res, 0, err
2018-02-27 11:37:15 +03:00
}
if resp.StatusCode >= http.StatusInternalServerError {
return res, resp.StatusCode, CreateGenericAPIError(
fmt.Sprintf("HTTP request error. Status code: %d.", resp.StatusCode))
2018-02-27 11:37:15 +03:00
}
res, err = buildRawResponse(resp)
if err != nil {
return res, 0, err
}
if resp.StatusCode >= http.StatusBadRequest && resp.StatusCode < http.StatusInternalServerError {
return res, resp.StatusCode, CreateAPIError(res)
2018-02-27 11:37:15 +03:00
}
if c.Debug {
c.writeLog("API Response: %s", res)
}
return res, resp.StatusCode, nil
2018-02-27 11:37:15 +03:00
}
// PostRequest implements POST Request with generic body data.
func (c *Client) PostRequest(
uri string,
postData interface{},
contType ...string,
) ([]byte, int, error) {
2019-09-04 11:38:21 +03:00
var (
res []byte
contentType string
)
prefix := "/api/v5"
2018-02-27 11:37:15 +03:00
2019-09-04 11:38:21 +03:00
if len(contType) > 0 {
contentType = contType[0]
} else {
contentType = "application/x-www-form-urlencoded"
}
reader, err := getReaderForPostData(postData)
if err != nil {
return res, 0, err
2019-09-04 11:38:21 +03:00
}
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s%s", c.URL, prefix, uri), reader)
2018-02-27 11:37:15 +03:00
if err != nil {
return res, 0, err
2018-02-27 11:37:15 +03:00
}
2019-09-04 11:38:21 +03:00
req.Header.Set("Content-Type", contentType)
2018-02-27 11:37:15 +03:00
req.Header.Set("X-API-KEY", c.Key)
if c.Debug {
c.writeLog("API Request: %s %s", uri, c.Key)
}
2018-02-27 11:37:15 +03:00
resp, err := c.httpClient.Do(req)
if err != nil {
return res, 0, err
2018-02-27 11:37:15 +03:00
}
if resp.StatusCode >= http.StatusInternalServerError {
return res, resp.StatusCode, CreateGenericAPIError(
fmt.Sprintf("HTTP request error. Status code: %d.", resp.StatusCode))
2018-02-27 11:37:15 +03:00
}
res, err = buildRawResponse(resp)
if err != nil {
return res, 0, err
}
if resp.StatusCode >= http.StatusBadRequest && resp.StatusCode < http.StatusInternalServerError {
return res, resp.StatusCode, CreateAPIError(res)
2018-02-27 11:37:15 +03:00
}
if c.Debug {
c.writeLog("API Response: %s", res)
}
return res, resp.StatusCode, nil
}
func getReaderForPostData(postData interface{}) (io.Reader, error) {
var reader io.Reader
switch d := postData.(type) {
case url.Values:
reader = strings.NewReader(d.Encode())
default:
if i, ok := d.(io.Reader); ok {
reader = i
} else {
err := errors.New("postData should be url.Values or implement io.Reader")
return nil, err
}
}
return reader, nil
2018-02-27 11:37:15 +03:00
}
func buildRawResponse(resp *http.Response) ([]byte, error) {
defer resp.Body.Close()
res, err := ioutil.ReadAll(resp.Body)
if err != nil {
return res, err
}
return res, nil
}
// checkBy select identifier type.
2018-02-27 11:37:15 +03:00
func checkBy(by string) string {
var context = ByID
2018-02-27 11:37:15 +03:00
if by != ByID {
context = ByExternalID
2018-02-27 11:37:15 +03:00
}
return context
}
// fillSite add site code to parameters if present.
2018-02-27 11:37:15 +03:00
func fillSite(p *url.Values, site []string) {
if len(site) > 0 {
s := site[0]
if s != "" {
p.Add("site", s)
}
}
}
// APIVersions get all available API versions for exact account
2018-02-27 11:37:15 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-api-versions
2018-04-12 16:43:53 +03:00
//
2018-02-27 11:37:15 +03:00
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-02-27 11:37:15 +03:00
//
// data, status, err := client.APIVersions()
2018-02-27 11:37:15 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-02-27 11:37:15 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-02-27 11:37:15 +03:00
// }
//
// for _, value := range data.versions {
// log.Printf("%v\n", value)
2018-02-27 11:37:15 +03:00
// }
func (c *Client) APIVersions() (VersionResponse, int, error) {
2018-02-27 11:37:15 +03:00
var resp VersionResponse
data, status, err := c.GetRequest("/api-versions", false)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
// APICredentials get all available API methods for exact account
2018-02-27 15:22:52 +03:00
//
// For more information see https://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-credentials
2018-04-12 16:43:53 +03:00
//
2018-02-27 15:22:52 +03:00
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-02-27 15:22:52 +03:00
//
// data, status, err := client.APICredentials()
2018-02-27 15:22:52 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-02-27 15:22:52 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-02-27 15:22:52 +03:00
// }
//
// for _, value := range data.Scopes {
// log.Printf("%v\n", value)
2018-02-27 15:22:52 +03:00
// }
func (c *Client) APICredentials() (CredentialResponse, int, error) {
2018-02-27 11:37:15 +03:00
var resp CredentialResponse
data, status, err := c.GetRequest("/credentials", false)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
2022-02-03 18:09:33 +03:00
return resp, status, err
}
return resp, status, nil
}
// APISystemInfo get info about system
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-system-info
2022-02-03 18:09:33 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.APISystemInfo()
//
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// log.Printf("%v\n", data)
func (c *Client) APISystemInfo() (SystemInfoResponse, int, error) {
var resp SystemInfoResponse
data, status, err := c.GetRequest("/system-info", false)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
// Customers returns list of customers matched the specified filter
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-customers
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.Customers(retailcrm.CustomersRequest{
2018-04-12 16:43:53 +03:00
// Filter: CustomersFilter{
// City: "Moscow",
// },
// Page: 3,
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// for _, value := range data.Customers {
// log.Printf("%v\n", value)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) Customers(parameters CustomersRequest) (CustomersResponse, int, error) {
var resp CustomersResponse
params, _ := query.Values(parameters)
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest(fmt.Sprintf("/customers?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
// CustomersCombine combine given customers
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-customers-combine
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.CustomersCombine([]retailcrm.Customer{{ID: 1}, {ID: 2}}, Customer{ID: 3})
2018-04-12 16:43:53 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) CustomersCombine(customers []Customer, resultCustomer Customer) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
combineJSONIn, _ := json.Marshal(&customers)
combineJSONOut, _ := json.Marshal(&resultCustomer)
2018-02-27 11:37:15 +03:00
p := url.Values{
"customers": {string(combineJSONIn)},
"resultCustomer": {string(combineJSONOut)},
}
data, status, err := c.PostRequest("/customers/combine", p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
// CustomerCreate creates customer
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-customers-create
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.CustomersCombine(retailcrm.Customer{
2018-04-12 16:43:53 +03:00
// FirstName: "Ivan",
// LastName: "Ivanov",
// Patronymic: "Ivanovich",
// ExternalID: 1,
// Email: "ivanov@example.com",
// Address: &retailcrm.Address{
2018-04-12 16:43:53 +03:00
// City: "Moscow",
// Street: "Kutuzovsky",
// },
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// fmt.Printf("%v", data.ID)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) CustomerCreate(customer Customer, site ...string) (CustomerChangeResponse, int, error) {
var resp CustomerChangeResponse
2018-02-27 11:37:15 +03:00
customerJSON, _ := json.Marshal(&customer)
p := url.Values{
"customer": {string(customerJSON)},
}
fillSite(&p, site)
2018-02-27 11:37:15 +03:00
data, status, err := c.PostRequest("/customers/create", p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
// CustomersFixExternalIds customers external ID
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-customers-fix-external-ids
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.CustomersFixExternalIds([]retailcrm.IdentifiersPair{{
2018-04-12 16:43:53 +03:00
// ID: 1,
// ExternalID: 12,
// }})
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) CustomersFixExternalIds(customers []IdentifiersPair) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
customersJSON, _ := json.Marshal(&customers)
2018-02-27 11:37:15 +03:00
p := url.Values{
"customers": {string(customersJSON)},
}
data, status, err := c.PostRequest("/customers/fix-external-ids", p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
// CustomersHistory returns customer's history
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-customers-history
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.CustomersHistory(retailcrm.CustomersHistoryRequest{
// Filter: retailcrm.CustomersHistoryFilter{
2018-04-12 16:43:53 +03:00
// SinceID: 20,
// },
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// for _, value := range data.History {
// log.Printf("%v\n", value)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) CustomersHistory(parameters CustomersHistoryRequest) (CustomersHistoryResponse, int, error) {
var resp CustomersHistoryResponse
2018-02-27 11:37:15 +03:00
params, _ := query.Values(parameters)
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest(fmt.Sprintf("/customers/history?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
2018-09-28 13:46:09 +03:00
// CustomerNotes returns customer related notes
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-customers-notes
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.CustomerNotes(retailcrm.NotesRequest{
// Filter: retailcrm.NotesFilter{
2018-04-12 16:43:53 +03:00
// CustomerIds: []int{1,2,3}
// },
// Page: 1,
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// for _, value := range data.Notes {
// log.Printf("%v\n", value)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) CustomerNotes(parameters NotesRequest) (NotesResponse, int, error) {
var resp NotesResponse
params, _ := query.Values(parameters)
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest(fmt.Sprintf("/customers/notes?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
// CustomerNoteCreate note creation
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-customers-notes-create
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.CustomerNoteCreate(retailcrm.Note{
2018-04-12 16:43:53 +03:00
// Text: "some text",
// ManagerID: 12,
// Customer: &retailcrm.Customer{
2018-04-12 16:43:53 +03:00
// ID: 1,
// },
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.ID)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) CustomerNoteCreate(note Note, site ...string) (CreateResponse, int, error) {
var resp CreateResponse
2018-02-27 11:37:15 +03:00
noteJSON, _ := json.Marshal(&note)
2018-02-27 11:37:15 +03:00
p := url.Values{
"note": {string(noteJSON)},
2018-02-27 11:37:15 +03:00
}
fillSite(&p, site)
data, status, err := c.PostRequest("/customers/notes/create", p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
2018-09-28 13:46:09 +03:00
// CustomerNoteDelete remove customer related note
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-customers-notes-id-delete
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.CustomerNoteDelete(12)
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) CustomerNoteDelete(id int) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
p := url.Values{
"id": {strconv.Itoa(id)},
}
data, status, err := c.PostRequest(fmt.Sprintf("/customers/notes/%d/delete", id), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
// CustomersUpload customers batch upload
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-customers-upload
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.CustomersUpload([]retailcrm.Customer{
2018-04-12 16:43:53 +03:00
// {
// FirstName: "Ivan",
// LastName: "Ivanov",
// Patronymic: "Ivanovich",
// ExternalID: 1,
// Email: "ivanov@example.com",
// },
// {
// FirstName: "Petr",
// LastName: "Petrov",
// Patronymic: "Petrovich",
// ExternalID: 2,
// Email: "petrov@example.com",
// },
// }}
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.UploadedCustomers)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) CustomersUpload(customers []Customer, site ...string) (CustomersUploadResponse, int, error) {
2018-02-27 11:37:15 +03:00
var resp CustomersUploadResponse
uploadJSON, _ := json.Marshal(&customers)
2018-02-27 11:37:15 +03:00
p := url.Values{
"customers": {string(uploadJSON)},
2018-02-27 11:37:15 +03:00
}
fillSite(&p, site)
data, status, err := c.PostRequest("/customers/upload", p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
// Customer returns information about customer
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-customers-externalId
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.Customer(12, retailcrm.ByExternalID, "")
2018-04-12 16:43:53 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.Customer)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) Customer(id, by, site string) (CustomerResponse, int, error) {
var resp CustomerResponse
var context = checkBy(by)
2018-02-27 11:37:15 +03:00
fw := CustomerRequest{context, site}
params, _ := query.Values(fw)
data, status, err := c.GetRequest(fmt.Sprintf("/customers/%s?%s", id, params.Encode()))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
// CustomerEdit edit exact customer
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-customers-externalId-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
2018-04-23 16:28:54 +03:00
// data, status, err := client.CustomerEdit(
// retailcrm.Customer{
2018-04-12 16:43:53 +03:00
// FirstName: "Ivan",
// LastName: "Ivanov",
// Patronymic: "Ivanovich",
// ID: 1,
// Email: "ivanov@example.com",
// },
// retailcrm.ByID,
2018-04-23 16:28:54 +03:00
// )
2018-04-12 16:43:53 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.Customer)
2018-04-12 16:43:53 +03:00
// }
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)
if context == ByExternalID {
uid = customer.ExternalID
}
customerJSON, _ := json.Marshal(&customer)
2018-02-27 11:37:15 +03:00
p := url.Values{
"by": {context},
"customer": {string(customerJSON)},
2018-02-27 11:37:15 +03:00
}
fillSite(&p, site)
data, status, err := c.PostRequest(fmt.Sprintf("/customers/%s/edit", uid), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
// CorporateCustomers returns list of corporate customers matched the specified filter
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#get--api-v5-customers-corporate
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CorporateCustomers(retailcrm.CorporateCustomersRequest{
// Filter: CorporateCustomersFilter{
// City: "Moscow",
// },
// Page: 3,
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// for _, value := range data.CustomersCorporate {
// log.Printf("%v\n", value)
// }
func (c *Client) CorporateCustomers(parameters CorporateCustomersRequest) (CorporateCustomersResponse, int, error) {
var resp CorporateCustomersResponse
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/customers-corporate?%s", params.Encode()))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomerCreate creates corporate customer
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#post--api-v5-customers-corporate-create
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CorporateCustomerCreate(retailcrm.CorporateCustomer{
// Nickname: "Company",
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// if data.Success == true {
// fmt.Printf("%v", data.ID)
// }
func (c *Client) CorporateCustomerCreate(customer CorporateCustomer, site ...string) (
CorporateCustomerChangeResponse, int, error,
) {
var resp CorporateCustomerChangeResponse
customerJSON, _ := json.Marshal(&customer)
p := url.Values{
"customerCorporate": {string(customerJSON)},
}
fillSite(&p, site)
data, status, err := c.PostRequest("/customers-corporate/create", p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomersFixExternalIds will fix corporate customers external ID's
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#post--api-v5-customers-corporate-fix-external-ids
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CorporateCustomersFixExternalIds([]retailcrm.IdentifiersPair{{
// ID: 1,
// ExternalID: 12,
// }})
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
func (c *Client) CorporateCustomersFixExternalIds(customers []IdentifiersPair) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
customersJSON, _ := json.Marshal(&customers)
p := url.Values{
"customersCorporate": {string(customersJSON)},
}
data, status, err := c.PostRequest("/customers-corporate/fix-external-ids", p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomersHistory returns corporate customer's history
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#post--api-v5-customers-corporate-fix-external-ids
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CorporateCustomersHistory(retailcrm.CorporateCustomersHistoryRequest{
// Filter: retailcrm.CorporateCustomersHistoryFilter{
// SinceID: 20,
// },
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// for _, value := range data.History {
// log.Printf("%v\n", value)
// }
func (c *Client) CorporateCustomersHistory(parameters CorporateCustomersHistoryRequest) (
CorporateCustomersHistoryResponse, int, error,
) {
var resp CorporateCustomersHistoryResponse
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/customers-corporate/history?%s", params.Encode()))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomersNotes returns list of corporate customers notes matched the specified filter
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#get--api-v5-customers-corporate-notes
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CorporateCustomersNotes(retailcrm.CorporateCustomersNotesRequest{
// Filter: CorporateCustomersNotesFilter{
// Text: "text",
// },
// Page: 3,
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// for _, value := range data.Notes {
// log.Printf("%v\n", value)
// }
func (c *Client) CorporateCustomersNotes(parameters CorporateCustomersNotesRequest) (
CorporateCustomersNotesResponse, int, error,
) {
var resp CorporateCustomersNotesResponse
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/customers-corporate/notes?%s", params.Encode()))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomerNoteCreate creates corporate customer note
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#post--api-v5-customers-corporate-notes-create
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CorporateCustomerNoteCreate(retailcrm.CorporateCustomerNote{
// Text: "text",
// Customer: &retailcrm.IdentifiersPair{
// ID: 1,
// }
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// if data.Success == true {
// fmt.Printf("%v", data.ID)
// }
func (c *Client) CorporateCustomerNoteCreate(note CorporateCustomerNote, site ...string) (CreateResponse, int, error) {
var resp CreateResponse
noteJSON, _ := json.Marshal(&note)
p := url.Values{
"note": {string(noteJSON)},
}
fillSite(&p, site)
data, status, err := c.PostRequest("/customers-corporate/notes/create", p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomerNoteDelete removes note from corporate customer
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#post--api-v5-customers-corporate-notes-id-delete
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CorporateCustomerNoteDelete(12)
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
func (c *Client) CorporateCustomerNoteDelete(id int) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
p := url.Values{
"id": {strconv.Itoa(id)},
}
data, status, err := c.PostRequest(fmt.Sprintf("/customers-corporate/notes/%d/delete", id), p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomersUpload corporate customers batch upload
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#post--api-v5-customers-corporate-upload
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CorporateCustomersUpload([]retailcrm.CorporateCustomer{
// {
// Nickname: "Company",
// ExternalID: 1,
// },
// {
// Nickname: "Company 2",
// ExternalID: 2,
// },
// }}
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.UploadedCustomers)
// }
func (c *Client) CorporateCustomersUpload(
customers []CorporateCustomer, site ...string,
) (CorporateCustomersUploadResponse, int, error) {
var resp CorporateCustomersUploadResponse
uploadJSON, _ := json.Marshal(&customers)
p := url.Values{
"customersCorporate": {string(uploadJSON)},
}
fillSite(&p, site)
data, status, err := c.PostRequest("/customers-corporate/upload", p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomer returns information about corporate customer
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#get--api-v5-customers-corporate-externalId
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CorporateCustomer(12, retailcrm.ByExternalID, "")
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.CorporateCustomer)
// }
func (c *Client) CorporateCustomer(id, by, site string) (CorporateCustomerResponse, int, error) {
var resp CorporateCustomerResponse
var context = checkBy(by)
fw := CustomerRequest{context, site}
params, _ := query.Values(fw)
data, status, err := c.GetRequest(fmt.Sprintf("/customers-corporate/%s?%s", id, params.Encode()))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomerAddresses returns information about corporate customer addresses
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#get--api-v5-customers-corporate-externalId-addresses
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CorporateCustomerAddresses("ext-id", retailcrm.CorporateCustomerAddressesRequest{
// Filter: v5,CorporateCustomerAddressesFilter{
// Name: "Main Address",
// },
// By: retailcrm.ByExternalID,
// Site: "site",
// Limit: 20,
// Page: 1,
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.Addresses)
// }
func (c *Client) CorporateCustomerAddresses(
id string, parameters CorporateCustomerAddressesRequest,
) (CorporateCustomersAddressesResponse, int, error) {
var resp CorporateCustomersAddressesResponse
parameters.By = checkBy(parameters.By)
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/customers-corporate/%s/addresses?%s", id, params.Encode()))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomerAddressesCreate creates corporate customer address
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#post--api-v5-customers-corporate-externalId-addresses-create
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := c.CorporateCustomerAddressesCreate("ext-id", retailcrm.ByExternalID, retailcrm.CorporateCustomerAddress{
// Text: "this is new address",
// Name: "New Address",
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// if data.Success == true {
// fmt.Printf("%v", data.ID)
// }
func (c *Client) CorporateCustomerAddressesCreate(
id string, by string, address CorporateCustomerAddress, site ...string,
) (CreateResponse, int, error) {
var resp CreateResponse
addressJSON, _ := json.Marshal(&address)
p := url.Values{
"address": {string(addressJSON)},
"by": {checkBy(by)},
}
fillSite(&p, site)
data, status, err := c.PostRequest(fmt.Sprintf("/customers-corporate/%s/addresses/create", id), p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomerAddressesEdit edit exact corporate customer address
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#post--api-v5-customers-corporate-externalId-addresses-entityExternalId-edit
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := c.CorporateCustomerAddressesEdit(
// "customer-ext-id",
// retailcrm.ByExternalID,
// retailcrm.ByExternalID,
// CorporateCustomerAddress{
// ExternalID: "addr-ext-id",
// Name: "Main Address 2",
// },
// )
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.Customer)
// }
func (c *Client) CorporateCustomerAddressesEdit(
customerID, customerBy, entityBy string, address CorporateCustomerAddress, site ...string,
) (CreateResponse, int, error) {
var (
resp CreateResponse
uid string
)
customerBy = checkBy(customerBy)
entityBy = checkBy(entityBy)
switch entityBy {
case ByID:
uid = strconv.Itoa(address.ID)
case ByExternalID:
uid = address.ExternalID
}
addressJSON, _ := json.Marshal(&address)
p := url.Values{
"by": {customerBy},
"entityBy": {entityBy},
"address": {string(addressJSON)},
}
fillSite(&p, site)
data, status, err := c.PostRequest(fmt.Sprintf("/customers-corporate/%s/addresses/%s/edit", customerID, uid), p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomerCompanies returns information about corporate customer companies
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#get--api-v5-customers-corporate-externalId-companies
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CorporateCustomerCompanies("ext-id", retailcrm.IdentifiersPairRequest{
// Filter: v5,IdentifiersPairFilter{
// Ids: []string{"1"},
// },
// By: retailcrm.ByExternalID,
// Site: "site",
// Limit: 20,
// Page: 1,
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.Companies)
// }
func (c *Client) CorporateCustomerCompanies(
id string, parameters IdentifiersPairRequest,
) (CorporateCustomerCompaniesResponse, int, error) {
var resp CorporateCustomerCompaniesResponse
parameters.By = checkBy(parameters.By)
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/customers-corporate/%s/companies?%s", id, params.Encode()))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomerCompaniesCreate creates corporate customer company
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#post--api-v5-customers-corporate-externalId-companies-create
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := c.CorporateCustomerCompaniesCreate("ext-id", retailcrm.ByExternalID, retailcrm.Company{
// Name: "Company name",
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// if data.Success == true {
// fmt.Printf("%v", data.ID)
// }
func (c *Client) CorporateCustomerCompaniesCreate(
id string, by string, company Company, site ...string,
) (CreateResponse, int, error) {
var resp CreateResponse
companyJSON, _ := json.Marshal(&company)
p := url.Values{
"company": {string(companyJSON)},
"by": {checkBy(by)},
}
fillSite(&p, site)
data, status, err := c.PostRequest(fmt.Sprintf("/customers-corporate/%s/companies/create", id), p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomerCompaniesEdit edit exact corporate customer company
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#post--api-v5-customers-corporate-externalId-companies-entityExternalId-edit
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := c.CorporateCustomerCompaniesEdit(
// "customer-ext-id",
// retailcrm.ByExternalID,
// retailcrm.ByExternalID,
// Company{
// ExternalID: "company-ext-id",
// Name: "New Company Name",
// },
// )
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.ID)
// }
func (c *Client) CorporateCustomerCompaniesEdit(
customerID, customerBy, entityBy string, company Company, site ...string,
) (CreateResponse, int, error) {
var (
resp CreateResponse
uid string
)
customerBy = checkBy(customerBy)
entityBy = checkBy(entityBy)
switch entityBy {
case ByID:
uid = strconv.Itoa(company.ID)
case ByExternalID:
uid = company.ExternalID
}
addressJSON, _ := json.Marshal(&company)
p := url.Values{
"by": {customerBy},
"entityBy": {entityBy},
"company": {string(addressJSON)},
}
fillSite(&p, site)
data, status, err := c.PostRequest(fmt.Sprintf("/customers-corporate/%s/companies/%s/edit", customerID, uid), p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomerContacts returns information about corporate customer contacts
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#get--api-v5-customers-corporate-externalId-contacts
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CorporateCustomerContacts("ext-id", retailcrm.IdentifiersPairRequest{
// Filter: retailcrm.IdentifiersPairFilter{
// Ids: []string{"1"},
// },
// By: retailcrm.ByExternalID,
// Site: "site",
// Limit: 20,
// Page: 1,
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.Contacts)
// }
func (c *Client) CorporateCustomerContacts(
id string, parameters IdentifiersPairRequest,
) (CorporateCustomerContactsResponse, int, error) {
var resp CorporateCustomerContactsResponse
parameters.By = checkBy(parameters.By)
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/customers-corporate/%s/contacts?%s", id, params.Encode()))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomerContactsCreate creates corporate customer contact
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#post--api-v5-customers-corporate-externalId-contacts-create
//
// Example (customer with specified id or externalId should exist in specified site):
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := c.CorporateCustomerContactsCreate("ext-id", retailcrm.ByExternalID, retailcrm.CorporateCustomerContact{
// IsMain: false,
// Customer: retailcrm.CorporateCustomerContactCustomer{
// ExternalID: "external_id",
// Site: "site",
// },
// Companies: []IdentifiersPair{},
// }, "site")
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// if data.Success == true {
// fmt.Printf("%v", data.ID)
// }
func (c *Client) CorporateCustomerContactsCreate(
id string, by string, contact CorporateCustomerContact, site ...string,
) (CreateResponse, int, error) {
var resp CreateResponse
companyJSON, _ := json.Marshal(&contact)
p := url.Values{
"contact": {string(companyJSON)},
"by": {checkBy(by)},
}
fillSite(&p, site)
data, status, err := c.PostRequest(fmt.Sprintf("/customers-corporate/%s/contacts/create", id), p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomerContactsEdit edit exact corporate customer contact
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#post--api-v5-customers-corporate-externalId-contacts-entityExternalId-edit
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := c.CorporateCustomerContactsEdit("ext-id", retailcrm.ByExternalID, retailcrm.ByID, retailcrm.CorporateCustomerContact{
// IsMain: false,
// Customer: retailcrm.CorporateCustomerContactCustomer{
// ID: 2350,
// },
// }, "site")
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.ID)
// }
func (c *Client) CorporateCustomerContactsEdit(
customerID, customerBy, entityBy string, contact CorporateCustomerContact, site ...string,
) (CreateResponse, int, error) {
var (
resp CreateResponse
uid string
)
customerBy = checkBy(customerBy)
entityBy = checkBy(entityBy)
switch entityBy {
case ByID:
uid = strconv.Itoa(contact.Customer.ID)
case ByExternalID:
uid = contact.Customer.ExternalID
}
addressJSON, _ := json.Marshal(&contact)
p := url.Values{
"by": {customerBy},
"entityBy": {entityBy},
"contact": {string(addressJSON)},
}
fillSite(&p, site)
data, status, err := c.PostRequest(fmt.Sprintf("/customers-corporate/%s/contacts/%s/edit", customerID, uid), p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// CorporateCustomerEdit edit exact corporate customer
//
// For more information see http://help.retailcrm.pro/Developers/ApiVersion5#post--api-v5-customers-corporate-externalId-edit
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CorporateCustomerEdit(
// retailcrm.CorporateCustomer{
// FirstName: "Ivan",
// LastName: "Ivanov",
// Patronymic: "Ivanovich",
// ID: 1,
// Email: "ivanov@example.com",
// },
// retailcrm.ByID,
// )
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.Customer)
// }
func (c *Client) CorporateCustomerEdit(customer CorporateCustomer, by string, site ...string) (
CustomerChangeResponse, int, error,
) {
var resp CustomerChangeResponse
var uid = strconv.Itoa(customer.ID)
var context = checkBy(by)
if context == ByExternalID {
uid = customer.ExternalID
}
customerJSON, _ := json.Marshal(&customer)
p := url.Values{
"by": {context},
"customerCorporate": {string(customerJSON)},
}
fillSite(&p, site)
data, status, err := c.PostRequest(fmt.Sprintf("/customers-corporate/%s/edit", uid), p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// DeliveryTracking updates tracking data
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-delivery-generic-subcode-tracking
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2021-01-29 14:29:44 +03:00
// t, _ := time.Parse("2006-01-02 15:04:05", "2012-12-12 12:12:12")
2018-04-12 16:43:53 +03:00
//
2018-04-23 16:28:54 +03:00
// data, status, err := client.DeliveryTracking(
// []retailcrm.DeliveryTrackingRequest{{
2018-04-12 16:43:53 +03:00
// DeliveryID: "1",
2021-01-29 14:29:44 +03:00
// TrackNumber: "123",
// History: []retailcrm.DeliveryHistoryRecord{
2018-04-12 16:43:53 +03:00
// {
// Code: "cancel",
2021-01-29 14:29:44 +03:00
// UpdatedAt: t.Format(time.RFC3339),
2018-04-12 16:43:53 +03:00
// },
2021-01-29 14:29:44 +03:00
// },
// }},
2018-04-12 16:43:53 +03:00
// "delivery-1",
2018-04-23 16:28:54 +03:00
// )
2018-04-12 16:43:53 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
func (c *Client) DeliveryTracking(parameters []DeliveryTrackingRequest, subcode string) (
SuccessfulResponse, int, error,
) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
updateJSON, _ := json.Marshal(&parameters)
2018-02-27 11:37:15 +03:00
p := url.Values{
"statusUpdate": {string(updateJSON)},
2018-02-27 11:37:15 +03:00
}
data, status, err := c.PostRequest(fmt.Sprintf("/delivery/generic/%s/tracking", subcode), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
// DeliveryShipments returns list of shipments
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-delivery-shipments
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.DeliveryShipments(retailcrm.DeliveryShipmentsRequest{
2018-04-12 16:43:53 +03:00
// Limit: 12,
// Filter: retailcrm.ShipmentFilter{
2018-04-12 16:43:53 +03:00
// DateFrom: "2012-12-12",
// },
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// for _, value := range data.DeliveryShipments {
// log.Printf("%v\n", value)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) DeliveryShipments(parameters DeliveryShipmentsRequest) (DeliveryShipmentsResponse, int, error) {
var resp DeliveryShipmentsResponse
2018-02-27 11:37:15 +03:00
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/delivery/shipments?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
// DeliveryShipmentCreate creates shipment
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-delivery-shipments-create
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
2018-04-23 16:28:54 +03:00
// data, status, err := client.DeliveryShipmentCreate(
// retailcrm.DeliveryShipment{
2018-04-12 16:43:53 +03:00
// Date: "2012-12-12",
// Time: retailcrm.DeliveryTime{
2018-04-12 16:43:53 +03:00
// From: "18:00",
// To: "20:00",
2018-04-23 16:28:54 +03:00
// },
// Orders: []retailcrm.Order{{Number: "12"}},
2018-04-23 16:28:54 +03:00
// },
// "sdek",
// )
2018-04-12 16:43:53 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.ID)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) DeliveryShipmentCreate(
shipment DeliveryShipment, deliveryType string, site ...string,
) (DeliveryShipmentUpdateResponse, int, error) {
var resp DeliveryShipmentUpdateResponse
updateJSON, _ := json.Marshal(&shipment)
2018-02-27 11:37:15 +03:00
p := url.Values{
"deliveryType": {deliveryType},
"deliveryShipment": {string(updateJSON)},
}
fillSite(&p, site)
2018-02-27 11:37:15 +03:00
data, status, err := c.PostRequest("/delivery/shipments/create", p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// DeliveryShipment get information about shipment
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-delivery-shipments-id
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.DeliveryShipment(12)
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.DeliveryShipment)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) DeliveryShipment(id int) (DeliveryShipmentResponse, int, error) {
var resp DeliveryShipmentResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest(fmt.Sprintf("/delivery/shipments/%d", id))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// DeliveryShipmentEdit shipment editing
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-delivery-shipments-id-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.DeliveryShipmentEdit(retailcrm.DeliveryShipment{
2018-04-12 16:43:53 +03:00
// ID: "12",
// Time: retailcrm.DeliveryTime{
2018-04-12 16:43:53 +03:00
// From: "14:00",
// To: "18:00",
// },
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) DeliveryShipmentEdit(shipment DeliveryShipment, site ...string) (
DeliveryShipmentUpdateResponse, int, error,
) {
var resp DeliveryShipmentUpdateResponse
updateJSON, _ := json.Marshal(&shipment)
2018-02-27 11:37:15 +03:00
p := url.Values{
"deliveryShipment": {string(updateJSON)},
2018-02-27 11:37:15 +03:00
}
fillSite(&p, site)
data, status, err := c.PostRequest(fmt.Sprintf("/delivery/shipments/%s/edit", strconv.Itoa(shipment.ID)), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// IntegrationModule returns integration module
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-integration-modules-code
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.IntegrationModule("moysklad3")
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.IntegrationModule)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) IntegrationModule(code string) (IntegrationModuleResponse, int, error) {
var resp IntegrationModuleResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest(fmt.Sprintf("/integration-modules/%s", code))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
2018-02-27 11:37:15 +03:00
2018-09-28 13:46:09 +03:00
// IntegrationModuleEdit integration module create/edit
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-integration-modules-code
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-23 16:28:54 +03:00
//
2018-04-12 16:43:53 +03:00
// name := "MS"
// code := "moysklad3"
2018-04-23 16:28:54 +03:00
//
// data, status, err := client.IntegrationModuleEdit(retailcrm.IntegrationModule{
2018-04-12 16:43:53 +03:00
// Code: code,
// IntegrationCode: code,
// Active: false,
// Name: fmt.Sprintf("Integration module %s", name),
// AccountURL: fmt.Sprintf("http://example.com/%s/account", name),
// BaseURL: fmt.Sprintf("http://example.com/%s", name),
// ClientID: "123",
// Logo: "https://cdn.worldvectorlogo.com/logos/github-icon.svg",
// })
//
2019-02-21 11:51:50 +03:00
// if err.Error() != "" {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2018-04-23 16:28:54 +03:00
// }
2018-04-12 16:43:53 +03:00
//
2018-04-23 16:28:54 +03:00
// if status >= http.StatusBadRequest {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2018-04-23 16:28:54 +03:00
// }
2018-04-12 16:43:53 +03:00
//
2018-04-23 16:28:54 +03:00
// if data.Success == true {
// fmt.Printf("%v\n", data.Info)
// }
func (c *Client) IntegrationModuleEdit(integrationModule IntegrationModule) (
IntegrationModuleEditResponse, int, error,
) {
var resp IntegrationModuleEditResponse
updateJSON, _ := json.Marshal(&integrationModule)
p := url.Values{"integrationModule": {string(updateJSON)}}
2018-02-27 11:37:15 +03:00
data, status, err := c.PostRequest(fmt.Sprintf("/integration-modules/%s/edit", integrationModule.Code), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
// UpdateScopes updates permissions for the API key
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-integration-modules-code-update-scopes
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
2022-04-13 17:32:26 +03:00
// data, status, err := client.UpdateScopes("moysklad3", retailcrm.ScopesRequired{
// Scopes: []string{"scope1", "scope2"},
// })
//
// if err.Error() != "" {
// fmt.Printf("%v", err.Error())
// }
//
// if status >= http.StatusBadRequest {
// fmt.Printf("%v", err.Error())
// }
//
// if data.Success == true {
// fmt.Printf("%v\n", data.APIKey)
// }
func (c *Client) UpdateScopes(code string, request ScopesRequired) (UpdateScopesResponse, int, error) {
var resp UpdateScopesResponse
updateJSON, _ := json.Marshal(&request)
p := url.Values{
"requires": {string(updateJSON)},
}
2022-04-13 17:32:26 +03:00
data, status, err := c.PostRequest(fmt.Sprintf("/integration-modules/%s/update-scopes", code), p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
2018-09-28 13:46:09 +03:00
// Orders returns list of orders matched the specified filters
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-orders
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.Orders(retailcrm.OrdersRequest{Filter: retailcrm.OrdersFilter{City: "Moscow"}, Page: 1})
2018-04-12 16:43:53 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// for _, value := range data.Orders {
// log.Printf("%v\n", value)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) Orders(parameters OrdersRequest) (OrdersResponse, int, error) {
var resp OrdersResponse
2018-02-27 11:37:15 +03:00
params, _ := query.Values(parameters)
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest(fmt.Sprintf("/orders?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// OrdersCombine combines given orders
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-orders-combine
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.OrdersCombine("ours", retailcrm.Order{ID: 1}, retailcrm.Order{ID: 1})
2018-04-12 16:43:53 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) OrdersCombine(technique string, order, resultOrder Order) (OperationResponse, int, error) {
2018-02-27 11:37:15 +03:00
var resp OperationResponse
combineJSONIn, _ := json.Marshal(&order)
combineJSONOut, _ := json.Marshal(&resultOrder)
2018-02-27 11:37:15 +03:00
p := url.Values{
"technique": {technique},
"order": {string(combineJSONIn)},
"resultOrder": {string(combineJSONOut)},
2018-02-27 11:37:15 +03:00
}
data, status, err := c.PostRequest("/orders/combine", p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// OrderCreate creates an order
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-orders-create
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.OrderCreate(retailcrm.Order{
2018-04-12 16:43:53 +03:00
// FirstName: "Ivan",
// LastName: "Ivanov",
// Patronymic: "Ivanovich",
// Email: "ivanov@example.com",
// Items: []retailcrm.OrderItem{{Offer: retailcrm.Offer{ID: 12}, Quantity: 5}},
2018-04-12 16:43:53 +03:00
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.ID)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) OrderCreate(order Order, site ...string) (OrderCreateResponse, int, error) {
var resp OrderCreateResponse
orderJSON, _ := json.Marshal(&order)
2018-02-27 11:37:15 +03:00
p := url.Values{
"order": {string(orderJSON)},
2018-02-27 11:37:15 +03:00
}
fillSite(&p, site)
data, status, err := c.PostRequest("/orders/create", p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// OrdersFixExternalIds set order external ID
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-orders-fix-external-ids
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.OrdersFixExternalIds(([]retailcrm.IdentifiersPair{{
2018-04-23 16:28:54 +03:00
// ID: 1,
// ExternalID: 12,
// }})
2018-04-12 16:43:53 +03:00
//
2019-02-21 11:51:50 +03:00
// if err.Error() != "" {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2018-04-23 16:28:54 +03:00
// }
2018-04-12 16:43:53 +03:00
//
2018-04-23 16:28:54 +03:00
// if status >= http.StatusBadRequest {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2018-04-23 16:28:54 +03:00
// }
2018-04-12 16:43:53 +03:00
//
2018-04-23 16:28:54 +03:00
// if data.Success == true {
// fmt.Printf("%v\n", data.ID)
// }
func (c *Client) OrdersFixExternalIds(orders []IdentifiersPair) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
ordersJSON, _ := json.Marshal(&orders)
2018-02-27 11:37:15 +03:00
p := url.Values{
"orders": {string(ordersJSON)},
2018-02-27 11:37:15 +03:00
}
data, status, err := c.PostRequest("/orders/fix-external-ids", p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// OrdersHistory returns orders history
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-orders-history
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.OrdersHistory(retailcrm.OrdersHistoryRequest{Filter: retailcrm.OrdersHistoryFilter{SinceID: 20}})
2018-04-12 16:43:53 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// for _, value := range data.History {
// log.Printf("%v\n", value)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) OrdersHistory(parameters OrdersHistoryRequest) (OrdersHistoryResponse, int, error) {
2019-08-09 16:45:48 +03:00
var resp OrdersHistoryResponse
2018-02-27 11:37:15 +03:00
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/orders/history?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// OrderPaymentCreate creates payment
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-orders-payments-create
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.OrderPaymentCreate(retailcrm.Payment{
// Order: &retailcrm.Order{
2018-04-12 16:43:53 +03:00
// ID: 12,
// },
// Amount: 300,
// Type: "cash",
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.ID)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) OrderPaymentCreate(payment Payment, site ...string) (CreateResponse, int, error) {
2018-02-27 11:37:15 +03:00
var resp CreateResponse
paymentJSON, _ := json.Marshal(&payment)
2018-02-27 11:37:15 +03:00
p := url.Values{
"payment": {string(paymentJSON)},
2018-02-27 11:37:15 +03:00
}
fillSite(&p, site)
data, status, err := c.PostRequest("/orders/payments/create", p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
// OrderPaymentDelete payment removing
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-orders-payments-id-delete
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.OrderPaymentDelete(12)
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) OrderPaymentDelete(id int) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
p := url.Values{
"id": {strconv.Itoa(id)},
2018-02-27 11:37:15 +03:00
}
data, status, err := c.PostRequest(fmt.Sprintf("/orders/payments/%d/delete", id), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// OrderPaymentEdit edit payment
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-orders-payments-id-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.OrderPaymentEdit(
// retailcrm.Payment{
2018-04-12 16:43:53 +03:00
// ID: 12,
// Amount: 500,
// },
// retailcrm.ByID,
2018-04-12 16:43:53 +03:00
// )
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) OrderPaymentEdit(payment Payment, by string, site ...string) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
var uid = strconv.Itoa(payment.ID)
var context = checkBy(by)
2018-02-27 11:37:15 +03:00
if context == ByExternalID {
uid = payment.ExternalID
2018-02-27 11:37:15 +03:00
}
paymentJSON, _ := json.Marshal(&payment)
2018-02-27 11:37:15 +03:00
p := url.Values{
2020-08-03 12:56:15 +03:00
"by": {context},
"payment": {string(paymentJSON)},
}
2018-02-27 11:37:15 +03:00
fillSite(&p, site)
2018-02-27 11:37:15 +03:00
data, status, err := c.PostRequest(fmt.Sprintf("/orders/payments/%s/edit", uid), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
// OrdersStatuses returns orders statuses
2020-06-29 16:26:10 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-orders-statuses
2020-06-29 16:26:10 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2020-06-29 16:26:10 +03:00
//
// data, status, err := client.OrdersStatuses(retailcrm.OrdersStatusesRequest{
2020-06-29 16:26:10 +03:00
// IDs: []int{1},
// ExternalIDs: []string{"2"},
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2020-06-29 16:26:10 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2020-06-29 16:26:10 +03:00
// }
func (c *Client) OrdersStatuses(request OrdersStatusesRequest) (OrdersStatusesResponse, int, error) {
2020-06-29 16:26:10 +03:00
var resp OrdersStatusesResponse
params, _ := query.Values(request)
data, status, err := c.GetRequest(fmt.Sprintf("/orders/statuses?%s", params.Encode()))
if err != nil {
2020-06-29 16:26:10 +03:00
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2020-06-29 16:26:10 +03:00
return resp, status, nil
}
2018-09-28 13:46:09 +03:00
// OrdersUpload batch orders uploading
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-orders-upload
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.OrdersUpload([]retailcrm.Order{
2018-04-12 16:43:53 +03:00
// {
// FirstName: "Ivan",
// LastName: "Ivanov",
// Patronymic: "Ivanovich",
// Email: "ivanov@example.com",
// Items: []retailcrm.OrderItem{{Offer: retailcrm.Offer{ID: 12}, Quantity: 5}},
2018-04-12 16:43:53 +03:00
// },
// {
// FirstName: "Pert",
// LastName: "Petrov",
// Patronymic: "Petrovich",
// Email: "petrov@example.com",
// Items: []retailcrm.OrderItem{{Offer: retailcrm.Offer{ID: 13}, Quantity: 1}},
2018-04-12 16:43:53 +03:00
// }
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.UploadedOrders)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) OrdersUpload(orders []Order, site ...string) (OrdersUploadResponse, int, error) {
var resp OrdersUploadResponse
2018-02-27 11:37:15 +03:00
uploadJSON, _ := json.Marshal(&orders)
2018-02-27 11:37:15 +03:00
p := url.Values{
"orders": {string(uploadJSON)},
2018-02-27 11:37:15 +03:00
}
fillSite(&p, site)
2018-02-27 11:37:15 +03:00
data, status, err := c.PostRequest("/orders/upload", p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// Order returns information about order
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-orders-externalId
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.Order(12, retailcrm.ByExternalID, "")
2018-04-12 16:43:53 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.Order)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) Order(id, by, site string) (OrderResponse, int, error) {
var resp OrderResponse
var context = checkBy(by)
2018-02-27 11:37:15 +03:00
fw := OrderRequest{context, site}
params, _ := query.Values(fw)
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest(fmt.Sprintf("/orders/%s?%s", id, params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// OrderEdit edit an order
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-orders-externalId-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.OrderEdit(
// retailcrm.Order{
2018-04-12 16:43:53 +03:00
// ID: 12,
// Items: []retailcrm.OrderItem{{Offer: retailcrm.Offer{ID: 13}, Quantity: 6}},
2018-04-12 16:43:53 +03:00
// },
// retailcrm.ByID,
2018-04-12 16:43:53 +03:00
// )
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) OrderEdit(order Order, by string, site ...string) (CreateResponse, int, error) {
2018-02-27 11:37:15 +03:00
var resp CreateResponse
var uid = strconv.Itoa(order.ID)
var context = checkBy(by)
2018-02-27 11:37:15 +03:00
if context == ByExternalID {
uid = order.ExternalID
2018-02-27 11:37:15 +03:00
}
orderJSON, _ := json.Marshal(&order)
2018-02-27 11:37:15 +03:00
p := url.Values{
"by": {context},
"order": {string(orderJSON)},
2018-02-27 11:37:15 +03:00
}
fillSite(&p, site)
data, status, err := c.PostRequest(fmt.Sprintf("/orders/%s/edit", uid), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// Packs returns list of packs matched the specified filters
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-orders-packs
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.Packs(retailcrm.PacksRequest{Filter: retailcrm.PacksFilter{OrderID: 12}})
2018-04-12 16:43:53 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// for _, value := range data.Packs {
// log.Printf("%v\n", value)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) Packs(parameters PacksRequest) (PacksResponse, int, error) {
var resp PacksResponse
2018-02-27 11:37:15 +03:00
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/orders/packs?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// PackCreate creates a pack
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-orders-packs-create
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.PackCreate(Pack{
// Store: "store-1",
// ItemID: 12,
// Quantity: 1,
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.ID)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) PackCreate(pack Pack) (CreateResponse, int, error) {
2018-02-27 11:37:15 +03:00
var resp CreateResponse
packJSON, _ := json.Marshal(&pack)
2018-02-27 11:37:15 +03:00
p := url.Values{
"pack": {string(packJSON)},
2018-02-27 11:37:15 +03:00
}
data, status, err := c.PostRequest("/orders/packs/create", p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// PacksHistory returns a history of order packing
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-orders-packs-history
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.PacksHistory(retailcrm.PacksHistoryRequest{Filter: retailcrm.OrdersHistoryFilter{SinceID: 5}})
2018-04-12 16:43:53 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// for _, value := range data.History {
// log.Printf("%v\n", value)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) PacksHistory(parameters PacksHistoryRequest) (PacksHistoryResponse, int, error) {
var resp PacksHistoryResponse
2018-02-27 11:37:15 +03:00
params, _ := query.Values(parameters)
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest(fmt.Sprintf("/orders/packs/history?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// Pack returns a pack info
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-orders-packs-id
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.Pack(112)
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.Pack)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) Pack(id int) (PackResponse, int, error) {
var resp PackResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest(fmt.Sprintf("/orders/packs/%d", id))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// PackDelete removes a pack
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-orders-packs-id-delete
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.PackDelete(112)
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) PackDelete(id int) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.PostRequest(fmt.Sprintf("/orders/packs/%d/delete", id), url.Values{})
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// PackEdit edit a pack
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-orders-packs-id-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.PackEdit(Pack{ID: 12, Quantity: 2})
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) PackEdit(pack Pack) (CreateResponse, int, error) {
var resp CreateResponse
2018-02-27 11:37:15 +03:00
packJSON, _ := json.Marshal(&pack)
2018-02-27 11:37:15 +03:00
p := url.Values{
"pack": {string(packJSON)},
2018-02-27 11:37:15 +03:00
}
data, status, err := c.PostRequest(fmt.Sprintf("/orders/packs/%d/edit", pack.ID), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// Countries returns list of available country codes
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-countries
func (c *Client) Countries() (CountriesResponse, int, error) {
2018-02-27 11:37:15 +03:00
var resp CountriesResponse
data, status, err := c.GetRequest("/reference/countries")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// CostGroups returns costs groups list
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-cost-groups
func (c *Client) CostGroups() (CostGroupsResponse, int, error) {
2018-02-27 11:37:15 +03:00
var resp CostGroupsResponse
data, status, err := c.GetRequest("/reference/cost-groups")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// CostGroupEdit edit costs groups
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-reference-cost-groups-code-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.CostGroupEdit(retailcrm.CostGroup{
2018-04-12 16:43:53 +03:00
// Code: "group-1",
// Color: "#da5c98",
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) CostGroupEdit(costGroup CostGroup) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
objJSON, _ := json.Marshal(&costGroup)
p := url.Values{
"costGroup": {string(objJSON)},
}
2018-02-27 11:37:15 +03:00
data, status, err := c.PostRequest(fmt.Sprintf("/reference/cost-groups/%s/edit", costGroup.Code), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// CostItems returns costs items list
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-cost-items
func (c *Client) CostItems() (CostItemsResponse, int, error) {
var resp CostItemsResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/reference/cost-items")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// CostItemEdit edit costs items
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-reference-cost-items-code-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.CostItemEdit(retailcrm.CostItem{
2018-04-12 16:43:53 +03:00
// Code: "seo",
// Active: false,
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) CostItemEdit(costItem CostItem) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
objJSON, _ := json.Marshal(&costItem)
p := url.Values{
"costItem": {string(objJSON)},
}
2018-02-27 11:37:15 +03:00
data, status, err := c.PostRequest(fmt.Sprintf("/reference/cost-items/%s/edit", costItem.Code), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// Couriers returns list of couriers
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-couriers
func (c *Client) Couriers() (CouriersResponse, int, error) {
var resp CouriersResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/reference/couriers")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// CourierCreate creates a courier
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-couriers
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.CostItemEdit(retailcrm.Courier{
2018-04-12 16:43:53 +03:00
// Active: true,
// Email: "courier1@example.com",
// FirstName: "Ivan",
// LastName: "Ivanov",
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v", data.ID)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) CourierCreate(courier Courier) (CreateResponse, int, error) {
var resp CreateResponse
objJSON, _ := json.Marshal(&courier)
p := url.Values{
"courier": {string(objJSON)},
}
2018-02-27 11:37:15 +03:00
data, status, err := c.PostRequest("/reference/couriers/create", p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// CourierEdit edit a courier
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-reference-couriers-id-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.CostItemEdit(retailcrm.Courier{
2018-04-12 16:43:53 +03:00
// ID: 1,
// Patronymic: "Ivanovich",
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) CourierEdit(courier Courier) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
objJSON, _ := json.Marshal(&courier)
2018-02-27 11:37:15 +03:00
p := url.Values{
"courier": {string(objJSON)},
}
data, status, err := c.PostRequest(fmt.Sprintf("/reference/couriers/%d/edit", courier.ID), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// DeliveryServices returns list of delivery services
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-delivery-services
func (c *Client) DeliveryServices() (DeliveryServiceResponse, int, error) {
var resp DeliveryServiceResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/reference/delivery-services")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// DeliveryServiceEdit delivery service create/edit
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-reference-delivery-services-code-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.DeliveryServiceEdit(retailcrm.DeliveryService{
2018-04-12 16:43:53 +03:00
// Active: false,
// Code: "delivery-1",
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) DeliveryServiceEdit(deliveryService DeliveryService) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
objJSON, _ := json.Marshal(&deliveryService)
2018-02-27 11:37:15 +03:00
p := url.Values{
"deliveryService": {string(objJSON)},
}
data, status, err := c.PostRequest(fmt.Sprintf("/reference/delivery-services/%s/edit", deliveryService.Code), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// DeliveryTypes returns list of delivery types
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-delivery-types
func (c *Client) DeliveryTypes() (DeliveryTypesResponse, int, error) {
var resp DeliveryTypesResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/reference/delivery-types")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// DeliveryTypeEdit delivery type create/edit
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-reference-delivery-types-code-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.DeliveryTypeEdit(retailcrm.DeliveryType{
2018-04-12 16:43:53 +03:00
// Active: false,
// Code: "type-1",
// DefaultCost: 300,
// DefaultForCrm: false,
// }
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) DeliveryTypeEdit(deliveryType DeliveryType) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
objJSON, _ := json.Marshal(&deliveryType)
2018-02-27 11:37:15 +03:00
p := url.Values{
"deliveryType": {string(objJSON)},
}
data, status, err := c.PostRequest(fmt.Sprintf("/reference/delivery-types/%s/edit", deliveryType.Code), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// LegalEntities returns list of legal entities
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-legal-entities
func (c *Client) LegalEntities() (LegalEntitiesResponse, int, error) {
var resp LegalEntitiesResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/reference/legal-entities")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// LegalEntityEdit change information about legal entity
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-reference-legal-entities-code-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.LegalEntityEdit(retailcrm.LegalEntity{
2018-04-12 16:43:53 +03:00
// Code: "legal-entity-1",
// CertificateDate: "2012-12-12",
// }
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) LegalEntityEdit(legalEntity LegalEntity) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
objJSON, _ := json.Marshal(&legalEntity)
2018-02-27 11:37:15 +03:00
p := url.Values{
"legalEntity": {string(objJSON)},
}
data, status, err := c.PostRequest(fmt.Sprintf("/reference/legal-entities/%s/edit", legalEntity.Code), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// OrderMethods returns list of order methods
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-order-methods
func (c *Client) OrderMethods() (OrderMethodsResponse, int, error) {
var resp OrderMethodsResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/reference/order-methods")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// OrderMethodEdit order method create/edit
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-reference-order-methods-code-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.OrderMethodEdit(retailcrm.OrderMethod{
2018-04-12 16:43:53 +03:00
// Code: "method-1",
// Active: false,
// DefaultForCRM: false,
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) OrderMethodEdit(orderMethod OrderMethod) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
objJSON, _ := json.Marshal(&orderMethod)
2018-02-27 11:37:15 +03:00
p := url.Values{
"orderMethod": {string(objJSON)},
}
data, status, err := c.PostRequest(fmt.Sprintf("/reference/order-methods/%s/edit", orderMethod.Code), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// OrderTypes return list of order types
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-order-types
func (c *Client) OrderTypes() (OrderTypesResponse, int, error) {
var resp OrderTypesResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/reference/order-types")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// OrderTypeEdit create/edit order type
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-reference-order-methods-code-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.OrderTypeEdit(retailcrm.OrderType{
2018-04-12 16:43:53 +03:00
// Code: "order-type-1",
// Active: false,
// DefaultForCRM: false,
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) OrderTypeEdit(orderType OrderType) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
objJSON, _ := json.Marshal(&orderType)
2018-02-27 11:37:15 +03:00
p := url.Values{
"orderType": {string(objJSON)},
2018-02-27 11:37:15 +03:00
}
data, status, err := c.PostRequest(fmt.Sprintf("/reference/order-types/%s/edit", orderType.Code), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// PaymentStatuses returns list of payment statuses
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-payment-statuses
func (c *Client) PaymentStatuses() (PaymentStatusesResponse, int, error) {
var resp PaymentStatusesResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/reference/payment-statuses")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// PaymentStatusEdit payment status creation/editing
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-reference-payment-statuses-code-edit
func (c *Client) PaymentStatusEdit(paymentStatus PaymentStatus) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
objJSON, _ := json.Marshal(&paymentStatus)
2018-02-27 11:37:15 +03:00
p := url.Values{
"paymentStatus": {string(objJSON)},
2018-02-27 11:37:15 +03:00
}
data, status, err := c.PostRequest(fmt.Sprintf("/reference/payment-statuses/%s/edit", paymentStatus.Code), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// PaymentTypes returns list of payment types
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-payment-types
func (c *Client) PaymentTypes() (PaymentTypesResponse, int, error) {
var resp PaymentTypesResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/reference/payment-types")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// PaymentTypeEdit payment type create/edit
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-reference-payment-types-code-edit
func (c *Client) PaymentTypeEdit(paymentType PaymentType) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
objJSON, _ := json.Marshal(&paymentType)
2018-02-27 11:37:15 +03:00
p := url.Values{
"paymentType": {string(objJSON)},
2018-02-27 11:37:15 +03:00
}
data, status, err := c.PostRequest(fmt.Sprintf("/reference/payment-types/%s/edit", paymentType.Code), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// PriceTypes returns list of price types
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-price-types
func (c *Client) PriceTypes() (PriceTypesResponse, int, error) {
var resp PriceTypesResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/reference/price-types")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// PriceTypeEdit price type create/edit
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-reference-price-types-code-edit
func (c *Client) PriceTypeEdit(priceType PriceType) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
objJSON, _ := json.Marshal(&priceType)
2018-02-27 11:37:15 +03:00
p := url.Values{
"priceType": {string(objJSON)},
2018-02-27 11:37:15 +03:00
}
data, status, err := c.PostRequest(fmt.Sprintf("/reference/price-types/%s/edit", priceType.Code), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// ProductStatuses returns list of item statuses in order
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-product-statuses
func (c *Client) ProductStatuses() (ProductStatusesResponse, int, error) {
var resp ProductStatusesResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/reference/product-statuses")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// ProductStatusEdit order item status create/edit
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-reference-product-statuses-code-edit
func (c *Client) ProductStatusEdit(productStatus ProductStatus) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
objJSON, _ := json.Marshal(&productStatus)
2018-02-27 11:37:15 +03:00
p := url.Values{
"productStatus": {string(objJSON)},
2018-02-27 11:37:15 +03:00
}
data, status, err := c.PostRequest(fmt.Sprintf("/reference/product-statuses/%s/edit", productStatus.Code), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// Sites returns the sites list
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-sites
func (c *Client) Sites() (SitesResponse, int, error) {
var resp SitesResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/reference/sites")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// SiteEdit site create/edit
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-reference-sites-code-edit
func (c *Client) SiteEdit(site Site) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
objJSON, _ := json.Marshal(&site)
2018-02-27 11:37:15 +03:00
p := url.Values{
"site": {string(objJSON)},
2018-02-27 11:37:15 +03:00
}
data, status, err := c.PostRequest(fmt.Sprintf("/reference/sites/%s/edit", site.Code), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// StatusGroups returns list of order status groups
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-status-groups
func (c *Client) StatusGroups() (StatusGroupsResponse, int, error) {
var resp StatusGroupsResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/reference/status-groups")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// Statuses returns list of order statuses
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-statuses
func (c *Client) Statuses() (StatusesResponse, int, error) {
var resp StatusesResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/reference/statuses")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// StatusEdit order status create/edit
2018-04-12 16:43:53 +03:00
//
// For more information see www.retailcrm.pro/docs/Developers/ApiVersion5#post--api-v5-reference-sites-code-edit.
func (c *Client) StatusEdit(st Status) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
objJSON, _ := json.Marshal(&st)
2018-02-27 11:37:15 +03:00
p := url.Values{
"status": {string(objJSON)},
2018-02-27 11:37:15 +03:00
}
data, status, err := c.PostRequest(fmt.Sprintf("/reference/statuses/%s/edit", st.Code), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// Stores returns list of warehouses
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-stores
func (c *Client) Stores() (StoresResponse, int, error) {
var resp StoresResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/reference/stores")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// StoreEdit warehouse create/edit
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-reference-stores-code-edit
func (c *Client) StoreEdit(store Store) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
objJSON, _ := json.Marshal(&store)
2018-02-27 11:37:15 +03:00
p := url.Values{
"store": {string(objJSON)},
2018-02-27 11:37:15 +03:00
}
data, status, err := c.PostRequest(fmt.Sprintf("/reference/stores/%s/edit", store.Code), p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
2019-02-20 10:07:02 +03:00
// Units returns units list
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-reference-units
func (c *Client) Units() (UnitsResponse, int, error) {
2019-02-20 10:07:02 +03:00
var resp UnitsResponse
data, status, err := c.GetRequest("/reference/units")
if err != nil {
2019-02-20 10:07:02 +03:00
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-20 10:07:02 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2019-02-20 10:07:02 +03:00
}
// UnitEdit unit create/edit
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-reference-units-code-edit
func (c *Client) UnitEdit(unit Unit) (SuccessfulResponse, int, error) {
2019-02-20 10:07:02 +03:00
var resp SuccessfulResponse
objJSON, _ := json.Marshal(&unit)
p := url.Values{
"unit": {string(objJSON)},
2019-02-20 10:07:02 +03:00
}
data, status, err := c.PostRequest(fmt.Sprintf("/reference/units/%s/edit", unit.Code), p)
if err != nil {
2019-02-20 10:07:02 +03:00
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-20 10:07:02 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2019-02-20 10:07:02 +03:00
}
2018-09-28 13:46:09 +03:00
// Segments returns segments
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-segments
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
2018-04-23 16:28:54 +03:00
// data, status, err := client.Segments(SegmentsRequest{
// Filter: retailcrm.SegmentsFilter{
2018-04-23 16:28:54 +03:00
// Ids: []int{1,2,3}
// }
// })
2018-04-12 16:43:53 +03:00
//
2019-02-21 11:51:50 +03:00
// if err.Error() != "" {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2018-04-23 16:28:54 +03:00
// }
2018-04-12 16:43:53 +03:00
//
2018-04-23 16:28:54 +03:00
// if status >= http.StatusBadRequest {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2018-04-23 16:28:54 +03:00
// }
2018-04-12 16:43:53 +03:00
//
// for _, value := range data.Segments {
2018-04-23 16:28:54 +03:00
// fmt.Printf("%v\n", value)
// }
func (c *Client) Segments(parameters SegmentsRequest) (SegmentsResponse, int, error) {
var resp SegmentsResponse
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/segments?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2020-07-14 10:22:36 +03:00
// Settings returns system settings
//
// For more information see https://help.retailcrm.pro/Developers/ApiVersion5#get--api-v5-settings
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2020-07-14 10:22:36 +03:00
//
// data, status, err := client.Settings()
//
// if err.Error() != "" {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2020-07-14 10:22:36 +03:00
// }
//
// if status >= http.StatusBadRequest {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2020-07-14 10:22:36 +03:00
// }
//
// fmt.Printf("%#v\n", data)
func (c *Client) Settings() (SettingsResponse, int, error) {
2020-07-14 10:22:36 +03:00
var resp SettingsResponse
data, status, err := c.GetRequest("/settings")
if err != nil {
2020-07-14 10:22:36 +03:00
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2020-07-14 10:22:36 +03:00
return resp, status, nil
}
2018-09-28 13:46:09 +03:00
// Inventories returns leftover stocks and purchasing prices
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-store-inventories
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.Inventories(retailcrm.InventoriesRequest{Filter: retailcrm.InventoriesFilter{Details: 1, ProductActive: 1}, Page: 1})
2018-04-12 16:43:53 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// for _, value := range data.Offers {
// log.Printf("%v\n", value)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) Inventories(parameters InventoriesRequest) (InventoriesResponse, int, error) {
2018-02-27 11:37:15 +03:00
var resp InventoriesResponse
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/store/inventories?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// InventoriesUpload updates the leftover stocks and purchasing prices
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-store-inventories-upload
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
2018-09-28 13:46:09 +03:00
// data, status, err := client.InventoriesUpload(
// []retailcrm.InventoryUpload{
2018-04-23 16:28:54 +03:00
// {
2018-09-28 13:46:09 +03:00
// XMLID: "pT22K9YzX21HTdzFCe1",
2018-04-23 16:28:54 +03:00
// Stores: []InventoryUploadStore{
// {Code: "test-store-v5", Available: 10, PurchasePrice: 1500},
// {Code: "test-store-v4", Available: 20, PurchasePrice: 1530},
// {Code: "test-store", Available: 30, PurchasePrice: 1510},
// },
// },
// {
2018-09-28 13:46:09 +03:00
// XMLID: "JQICtiSpOV3AAfMiQB3",
2018-04-23 16:28:54 +03:00
// Stores: []InventoryUploadStore{
// {Code: "test-store-v5", Available: 45, PurchasePrice: 1500},
// {Code: "test-store-v4", Available: 32, PurchasePrice: 1530},
// {Code: "test-store", Available: 46, PurchasePrice: 1510},
// },
// },
// },
// )
2018-04-12 16:43:53 +03:00
//
2019-02-21 11:51:50 +03:00
// if err.Error() != "" {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2018-04-23 16:28:54 +03:00
// }
2018-04-12 16:43:53 +03:00
//
2018-04-23 16:28:54 +03:00
// if status >= http.StatusBadRequest {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2018-04-23 16:28:54 +03:00
// }
2018-04-12 16:43:53 +03:00
//
2018-04-23 16:28:54 +03:00
// fmt.Printf("%v\n", data.NotFoundOffers)
func (c *Client) InventoriesUpload(inventories []InventoryUpload, site ...string) (StoreUploadResponse, int, error) {
2018-02-27 11:37:15 +03:00
var resp StoreUploadResponse
uploadJSON, _ := json.Marshal(&inventories)
2018-02-27 11:37:15 +03:00
p := url.Values{
"offers": {string(uploadJSON)},
2018-02-27 11:37:15 +03:00
}
fillSite(&p, site)
data, status, err := c.PostRequest("/store/inventories/upload", p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// PricesUpload updates prices
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-store-prices-upload
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.PricesUpload([]retailcrm.OfferPriceUpload{
2018-04-23 16:28:54 +03:00
// {
2018-04-12 16:43:53 +03:00
// ID 1
// Site "store-1"
// Prices []PriceUpload{{Code: "price-1"}}
2018-04-23 16:28:54 +03:00
// },
// {
2018-04-12 16:43:53 +03:00
// ID 2
// Site "store-1"
// Prices []PriceUpload{{Code: "price-2"}}
2018-04-23 16:28:54 +03:00
// },
// })
2018-04-12 16:43:53 +03:00
//
2019-02-21 11:51:50 +03:00
// if err.Error() != "" {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2018-04-23 16:28:54 +03:00
// }
2018-04-12 16:43:53 +03:00
//
2018-04-23 16:28:54 +03:00
// if status >= http.StatusBadRequest {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2018-04-23 16:28:54 +03:00
// }
2018-04-12 16:43:53 +03:00
//
2018-04-23 16:28:54 +03:00
// fmt.Printf("%v\n", data.NotFoundOffers)
func (c *Client) PricesUpload(prices []OfferPriceUpload) (StoreUploadResponse, int, error) {
2018-02-27 11:37:15 +03:00
var resp StoreUploadResponse
uploadJSON, _ := json.Marshal(&prices)
2018-02-27 11:37:15 +03:00
p := url.Values{
"prices": {string(uploadJSON)},
2018-02-27 11:37:15 +03:00
}
data, status, err := c.PostRequest("/store/prices/upload", p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// ProductsGroup returns list of product groups
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-store-product-groups
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.ProductsGroup(retailcrm.ProductsGroupsRequest{
// Filter: retailcrm.ProductsGroupsFilter{
2018-04-12 16:43:53 +03:00
// Active: 1,
// },
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// for _, value := range data.ProductGroup {
// log.Printf("%v\n", value)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) ProductsGroup(parameters ProductsGroupsRequest) (ProductsGroupsResponse, int, error) {
2018-02-27 11:37:15 +03:00
var resp ProductsGroupsResponse
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/store/product-groups?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// Products returns list of products and SKU
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-store-products
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.Products(retailcrm.ProductsRequest{
// Filter: retailcrm.ProductsFilter{
2018-04-12 16:43:53 +03:00
// Active: 1,
// MinPrice: 1000,
// },
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// for _, value := range data.Products {
// log.Printf("%v\n", value)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) Products(parameters ProductsRequest) (ProductsResponse, int, error) {
2018-02-27 11:37:15 +03:00
var resp ProductsResponse
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/store/products?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// ProductsProperties returns list of item properties, matching the specified filters
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-store-products-properties
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.ProductsProperties(retailcrm.ProductsPropertiesRequest{
// Filter: retailcrm.ProductsPropertiesFilter{
2018-04-12 16:43:53 +03:00
// Sites: []string["store"],
// },
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// for _, value := range data.Properties {
// log.Printf("%v\n", value)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) ProductsProperties(parameters ProductsPropertiesRequest) (ProductsPropertiesResponse, int, error) {
2018-02-27 11:37:15 +03:00
var resp ProductsPropertiesResponse
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/store/products/properties?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// Tasks returns task list
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-tasks
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.Tasks(retailcrm.TasksRequest{
2018-04-12 16:43:53 +03:00
// Filter: TasksFilter{
// DateFrom: "2012-12-12",
// },
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// for _, value := range data.Tasks {
// log.Printf("%v\n", value)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) Tasks(parameters TasksRequest) (TasksResponse, int, error) {
var resp TasksResponse
2018-02-27 11:37:15 +03:00
params, _ := query.Values(parameters)
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest(fmt.Sprintf("/tasks?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// TaskCreate create a task
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-tasks-create
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.Tasks(retailcrm.Task{
2018-04-12 16:43:53 +03:00
// Text: "task №1",
// PerformerID: 12,
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.ID)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) TaskCreate(task Task, site ...string) (CreateResponse, int, error) {
var resp CreateResponse
taskJSON, _ := json.Marshal(&task)
2018-02-27 11:37:15 +03:00
p := url.Values{
"task": {string(taskJSON)},
}
2018-02-27 11:37:15 +03:00
fillSite(&p, site)
data, status, err := c.PostRequest("/tasks/create", p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// Task returns task
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-tasks-id
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.Task(12)
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.Task)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) Task(id int) (TaskResponse, int, error) {
var resp TaskResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest(fmt.Sprintf("/tasks/%d", id))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-04-23 16:28:54 +03:00
2018-09-28 13:46:09 +03:00
// TaskEdit edit a task
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-tasks-id-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.Task(retailcrm.Task{
2018-04-12 16:43:53 +03:00
// ID: 12
// Text: "task №2",
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) TaskEdit(task Task, site ...string) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
var uid = strconv.Itoa(task.ID)
taskJSON, _ := json.Marshal(&task)
2018-02-27 11:37:15 +03:00
p := url.Values{
"task": {string(taskJSON)},
2018-02-27 11:37:15 +03:00
}
fillSite(&p, site)
data, status, err := c.PostRequest(fmt.Sprintf("/tasks/%s/edit", uid), p)
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
if !resp.Success {
a := CreateAPIError(data)
return resp, status, a
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// UserGroups returns list of user groups
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-user-groups
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.UserGroups(retailcrm.UserGroupsRequest{Page: 1})
2018-04-12 16:43:53 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// for _, value := range data.Groups {
// log.Printf("%v\n", value)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) UserGroups(parameters UserGroupsRequest) (UserGroupsResponse, int, error) {
var resp UserGroupsResponse
2018-02-27 11:37:15 +03:00
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/user-groups?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
2018-09-28 13:46:09 +03:00
// Users returns list of users matched the specified filters
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-users
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.Users(retailcrm.UsersRequest{Filter: retailcrm.UsersFilter{Active: 1}, Page: 1})
2018-04-12 16:43:53 +03:00
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// for _, value := range data.Users {
// log.Printf("%v\n", value)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) Users(parameters UsersRequest) (UsersResponse, int, error) {
var resp UsersResponse
2018-02-27 11:37:15 +03:00
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/users?%s", params.Encode()))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// User returns information about user
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-users-id
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.User(12)
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.User)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) User(id int) (UserResponse, int, error) {
var resp UserResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest(fmt.Sprintf("/users/%d", id))
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// UserStatus change user status
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-users
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2018-04-12 16:43:53 +03:00
//
// data, status, err := client.UserStatus(12, "busy")
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2018-04-12 16:43:53 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2018-04-12 16:43:53 +03:00
// }
func (c *Client) UserStatus(id int, status string) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
p := url.Values{
"status": {status},
}
data, st, err := c.PostRequest(fmt.Sprintf("/users/%d/status", id), p)
if err != nil {
return resp, st, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, st, err
}
if !resp.Success {
return resp, st, CreateAPIError(data)
}
2019-02-21 11:51:50 +03:00
return resp, st, nil
}
2018-09-28 13:46:09 +03:00
// StaticticsUpdate updates statistics
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-statistic-update
func (c *Client) StaticticsUpdate() (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
2018-02-27 11:37:15 +03:00
data, status, err := c.GetRequest("/statistic/update")
if err != nil {
return resp, status, err
2018-02-27 11:37:15 +03:00
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2018-02-27 11:37:15 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
2018-02-27 11:37:15 +03:00
}
2018-09-28 13:46:09 +03:00
// Costs returns costs list
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-costs
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.Costs(CostsRequest{
// Filter: CostsFilter{
// Ids: []string{"1","2","3"},
// MinSumm: "1000"
// },
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// for _, value := range data.Costs {
// log.Printf("%v\n", value.Summ)
// }
func (c *Client) Costs(costs CostsRequest) (CostsResponse, int, error) {
var resp CostsResponse
params, _ := query.Values(costs)
data, status, err := c.GetRequest(fmt.Sprintf("/costs?%s", params.Encode()))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
// CostCreate create the cost
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-costs-create
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CostCreate(
// retailcrm.CostRecord{
// DateFrom: "2012-12-12",
// DateTo: "2012-12-12",
// Summ: 12,
// CostItem: "calculation-of-costs",
// Order: Order{
// Number: "1"
// },
// Sites: []string{"store"},
// },
// "store"
// )
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// If data.Success == true {
// log.Printf("%v", data.ID)
// }
func (c *Client) CostCreate(cost CostRecord, site ...string) (CreateResponse, int, error) {
var resp CreateResponse
costJSON, _ := json.Marshal(&cost)
p := url.Values{
"cost": {string(costJSON)},
}
fillSite(&p, site)
data, status, err := c.PostRequest("/costs/create", p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
2018-09-28 13:46:09 +03:00
// CostsDelete removes a cost
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-costs-delete
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.client.CostsDelete([]int{1, 2, 3, 48, 49, 50})
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// If data.Success == true {
// log.Printf("Not removed costs: %v", data.NotRemovedIds)
// }
func (c *Client) CostsDelete(ids []int) (CostsDeleteResponse, int, error) {
var resp CostsDeleteResponse
costJSON, _ := json.Marshal(&ids)
p := url.Values{
"ids": {string(costJSON)},
}
data, status, err := c.PostRequest("/costs/delete", p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
2018-09-28 13:46:09 +03:00
// CostsUpload batch costs upload
2018-04-12 16:43:53 +03:00
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-costs-upload
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CostCreate([]retailcrm.CostRecord{
// {
// DateFrom: "2012-12-12",
// DateTo: "2012-12-12",
// Summ: 12,
// CostItem: "calculation-of-costs",
// Order: Order{
// Number: "1"
// },
// Sites: []string{"store"},
// },
// {
// DateFrom: "2012-12-13",
// DateTo: "2012-12-13",
// Summ: 13,
// CostItem: "seo",
// }
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// If data.Success == true {
// log.Printf("Uploaded costs: %v", data.UploadedCosts)
// }
func (c *Client) CostsUpload(cost []CostRecord) (CostsUploadResponse, int, error) {
var resp CostsUploadResponse
costJSON, _ := json.Marshal(&cost)
p := url.Values{
"costs": {string(costJSON)},
}
data, status, err := c.PostRequest("/costs/upload", p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
2018-09-28 13:46:09 +03:00
// Cost returns information about specified cost
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-costs-id
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.Cost(1)
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// If data.Success == true {
// log.Printf("%v", data.Cost)
// }
func (c *Client) Cost(id int) (CostResponse, int, error) {
var resp CostResponse
data, status, err := c.GetRequest(fmt.Sprintf("/costs/%d", id))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
2018-09-28 13:46:09 +03:00
// CostDelete removes cost
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-costs-id-delete
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CostDelete(1)
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
func (c *Client) CostDelete(id int) (SuccessfulResponse, int, error) {
var resp SuccessfulResponse
costJSON, _ := json.Marshal(&id)
p := url.Values{
"costs": {string(costJSON)},
}
data, status, err := c.PostRequest(fmt.Sprintf("/costs/%d/delete", id), p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
2018-09-28 13:46:09 +03:00
// CostEdit edit a cost
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-costs-id-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CostEdit(1, retailcrm.Cost{
// DateFrom: "2012-12-12",
// DateTo: "2018-12-13",
// Summ: 321,
// CostItem: "seo",
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// If data.Success == true {
// log.Printf("%v", data.ID)
// }
func (c *Client) CostEdit(id int, cost CostRecord, site ...string) (CreateResponse, int, error) {
var resp CreateResponse
costJSON, _ := json.Marshal(&cost)
p := url.Values{
"cost": {string(costJSON)},
}
fillSite(&p, site)
data, status, err := c.PostRequest(fmt.Sprintf("/costs/%d/edit", id), p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-09-04 11:38:21 +03:00
return resp, status, nil
}
// Files returns files list
//
// For more information see https://help.retailcrm.pro/Developers/ApiVersion5#get--api-v5-files
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2019-09-04 11:38:21 +03:00
//
// data, status, err := client.Files(FilesRequest{
// Filter: FilesFilter{
// Filename: "image.jpeg",
// },
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2019-09-04 11:38:21 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2019-09-04 11:38:21 +03:00
// }
func (c *Client) Files(files FilesRequest) (FilesResponse, int, error) {
2019-09-04 11:38:21 +03:00
var resp FilesResponse
params, _ := query.Values(files)
data, status, err := c.GetRequest(fmt.Sprintf("/files?%s", params.Encode()))
if err != nil && err.Error() != "" {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-09-04 11:38:21 +03:00
return resp, status, nil
}
// FileUpload uploads file to RetailCRM
2019-09-04 11:38:21 +03:00
//
// For more information see https://help.retailcrm.pro/Developers/ApiVersion5#get--api-v5-files
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2019-09-04 11:38:21 +03:00
//
// file, err := os.Open("file.jpg")
// if err != nil {
// fmt.Print(err)
// }
//
// data, status, err := client.FileUpload(file)
//
// if err.Error() != "" {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2019-09-04 11:38:21 +03:00
// }
//
// if status >= http.StatusBadRequest {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2019-09-04 11:38:21 +03:00
// }
func (c *Client) FileUpload(reader io.Reader) (FileUploadResponse, int, error) {
2019-09-04 11:38:21 +03:00
var resp FileUploadResponse
2019-09-09 15:39:26 +03:00
data, status, err := c.PostRequest("/files/upload", reader, "application/octet-stream")
2019-09-04 11:38:21 +03:00
if err != nil && err.Error() != "" {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-09-04 11:38:21 +03:00
return resp, status, nil
}
// File returns a file info
//
// For more information see https://help.retailcrm.pro/Developers/ApiVersion5#get--api-v5-files
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2019-09-04 11:38:21 +03:00
//
// data, status, err := client.File(112)
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
2019-09-04 11:38:21 +03:00
//
// log.Fatalf("http status: %d, error: %s", status, err)
2019-09-04 11:38:21 +03:00
// }
//
// if data.Success == true {
// log.Printf("%v\n", data.File)
2019-09-04 11:38:21 +03:00
// }
func (c *Client) File(id int) (FileResponse, int, error) {
2019-09-04 11:38:21 +03:00
var resp FileResponse
data, status, err := c.GetRequest(fmt.Sprintf("/files/%d", id))
if err != nil {
2019-09-04 11:38:21 +03:00
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-09-04 11:38:21 +03:00
return resp, status, nil
}
// FileDelete removes file from RetailCRM
2019-09-04 11:38:21 +03:00
//
// For more information see https://help.retailcrm.pro/Developers/ApiVersion5#get--api-v5-files
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2019-09-04 11:38:21 +03:00
//
// data, status, err := client.FileDelete(123)
//
// if err.Error() != "" {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2019-09-04 11:38:21 +03:00
// }
//
// if status >= http.StatusBadRequest {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2019-09-04 11:38:21 +03:00
// }
func (c *Client) FileDelete(id int) (SuccessfulResponse, int, error) {
2019-09-04 11:38:21 +03:00
var resp SuccessfulResponse
data, status, err := c.PostRequest(fmt.Sprintf("/files/%d/delete", id), strings.NewReader(""))
if err != nil && err.Error() != "" {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-09-04 11:38:21 +03:00
return resp, status, nil
}
// FileDownload downloads file from RetailCRM
2019-09-04 11:38:21 +03:00
//
// For more information see https://help.retailcrm.pro/Developers/ApiVersion5#get--api-v5-files
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2019-09-04 11:38:21 +03:00
//
// fileData, status, err := client.FileDownload(123)
//
// if err.Error() != "" {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2019-09-04 11:38:21 +03:00
// }
//
// if status >= http.StatusBadRequest {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2019-09-04 11:38:21 +03:00
// }
func (c *Client) FileDownload(id int) (io.ReadCloser, int, error) {
2019-09-04 11:38:21 +03:00
data, status, err := c.GetRequest(fmt.Sprintf("/files/%d/download", id))
if status != http.StatusOK {
return nil, status, err
}
closer := ioutil.NopCloser(bytes.NewReader(data))
return closer, status, nil
}
// FileEdit edits file name and relations with orders and customers in RetailCRM
2019-09-04 11:38:21 +03:00
//
// For more information see https://help.retailcrm.pro/Developers/ApiVersion5#get--api-v5-files
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
2019-09-04 11:38:21 +03:00
//
// data, status, err := client.FileEdit(123, File{Filename: "image2.jpg"})
//
// if err.Error() != "" {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2019-09-04 11:38:21 +03:00
// }
//
// if status >= http.StatusBadRequest {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
2019-09-04 11:38:21 +03:00
// }
func (c *Client) FileEdit(id int, file File) (FileResponse, int, error) {
2019-09-04 11:38:21 +03:00
var resp FileResponse
req, _ := json.Marshal(file)
data, status, err := c.PostRequest(
fmt.Sprintf("/files/%d/edit", id), url.Values{
"file": {string(req)},
},
)
2019-09-04 11:38:21 +03:00
if err != nil && err.Error() != "" {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-09-04 11:38:21 +03:00
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
2018-09-28 13:46:09 +03:00
// CustomFields returns list of custom fields
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-custom-fields
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CustomFields(retailcrm.CustomFieldsRequest{
// Type: "string",
// Entity: "customer",
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// for _, value := range data.CustomFields {
// fmt.Printf("%v\n", value)
// }
func (c *Client) CustomFields(customFields CustomFieldsRequest) (CustomFieldsResponse, int, error) {
var resp CustomFieldsResponse
params, _ := query.Values(customFields)
data, status, err := c.GetRequest(fmt.Sprintf("/custom-fields?%s", params.Encode()))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
2018-09-28 13:46:09 +03:00
// CustomDictionaries returns list of custom directory
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-custom-fields-dictionaries
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CustomDictionaries(retailcrm.CustomDictionariesRequest{
// Filter: retailcrm.CustomDictionariesFilter{
// Name: "Dictionary-1",
// },
// })
//
2019-02-21 11:51:50 +03:00
// if err.Error() != "" {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
// }
//
// if status >= http.StatusBadRequest {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
// }
//
// for _, value := range data.CustomDictionaries {
// fmt.Printf("%v\n", value.Elements)
// }
func (c *Client) CustomDictionaries(customDictionaries CustomDictionariesRequest) (
CustomDictionariesResponse, int, error,
) {
var resp CustomDictionariesResponse
params, _ := query.Values(customDictionaries)
data, status, err := c.GetRequest(fmt.Sprintf("/custom-fields/dictionaries?%s", params.Encode()))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
2018-09-28 13:46:09 +03:00
// CustomDictionariesCreate creates a custom dictionary
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-custom-fields-dictionaries-create
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CustomDictionariesCreate(retailcrm.CustomDictionary{
// Name: "Courier profiles",
// Code: "courier-profiles",
// Elements: []Element{
// {
// Name: "Name",
// Code: "name",
// },
// {
// Name: "Lastname",
// Code: "lastname",
// }
// },
// })
//
2019-02-21 11:51:50 +03:00
// if err.Error() != "" {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
// }
//
// if status >= http.StatusBadRequest {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
// }
//
// If data.Success == true {
// fmt.Printf("%v", data.Code)
// }
func (c *Client) CustomDictionariesCreate(customDictionary CustomDictionary) (CustomResponse, int, error) {
var resp CustomResponse
costJSON, _ := json.Marshal(&customDictionary)
p := url.Values{
"customDictionary": {string(costJSON)},
}
data, status, err := c.PostRequest("/custom-fields/dictionaries/create", p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
2018-09-28 13:46:09 +03:00
// CustomDictionary returns information about dictionary
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-custom-fields-entity-code
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CustomDictionary("courier-profiles")
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// If data.Success == true {
// log.Printf("%v", data.CustomDictionary.Name)
// }
func (c *Client) CustomDictionary(code string) (CustomDictionaryResponse, int, error) {
var resp CustomDictionaryResponse
data, status, err := c.GetRequest(fmt.Sprintf("/custom-fields/dictionaries/%s", code))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
// CustomDictionaryEdit edit custom dictionary
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-custom-fields-dictionaries-code-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CustomDictionaryEdit(retailcrm.CustomDictionary{
// Name: "Courier profiles",
// Code: "courier-profiles",
// Elements: []Element{
// {
// Name: "Name",
// Code: "name",
// },
// {
// Name: "Lastname",
// Code: "lastname",
// }
// },
// })
//
2019-02-21 11:51:50 +03:00
// if err.Error() != "" {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
// }
//
// if status >= http.StatusBadRequest {
2021-01-29 14:29:44 +03:00
// fmt.Printf("%v", err.Error())
// }
//
// If data.Success == true {
// fmt.Printf("%v", data.Code)
// }
func (c *Client) CustomDictionaryEdit(customDictionary CustomDictionary) (CustomResponse, int, error) {
var resp CustomResponse
costJSON, _ := json.Marshal(&customDictionary)
p := url.Values{
"customDictionary": {string(costJSON)},
}
data, status, err := c.PostRequest(fmt.Sprintf("/custom-fields/dictionaries/%s/edit", customDictionary.Code), p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
// CustomFieldsCreate creates custom field
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-custom-fields-entity-create
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
2018-04-12 16:43:53 +03:00
// data, status, err := client.CustomFieldsCreate(CustomFields{
// Name: "First order",
// Code: "first-order",
// Type: "bool",
// Entity: "order",
// DisplayArea: "customer",
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// If data.Success == true {
// log.Printf("%v", data.Code)
// }
func (c *Client) CustomFieldsCreate(customFields CustomFields) (CustomResponse, int, error) {
var resp CustomResponse
costJSON, _ := json.Marshal(&customFields)
p := url.Values{
"customField": {string(costJSON)},
}
2018-04-12 16:43:53 +03:00
data, status, err := c.PostRequest(fmt.Sprintf("/custom-fields/%s/create", customFields.Entity), p)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
// CustomField returns information about custom fields
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-custom-fields-entity-code
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.CustomField("order", "first-order")
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// If data.Success == true {
// log.Printf("%v", data.CustomField)
// }
func (c *Client) CustomField(entity, code string) (CustomFieldResponse, int, error) {
var resp CustomFieldResponse
data, status, err := c.GetRequest(fmt.Sprintf("/custom-fields/%s/%s", entity, code))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
// CustomFieldEdit list method
//
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-custom-fields-entity-code-edit
2018-04-12 16:43:53 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
2018-04-12 16:43:53 +03:00
// data, status, err := client.CustomFieldEdit(CustomFields{
// Code: "first-order",
// Entity: "order",
// DisplayArea: "delivery",
// })
//
2021-01-29 14:29:44 +03:00
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// If data.Success == true {
// log.Printf("%v", data.Code)
// }
func (c *Client) CustomFieldEdit(customFields CustomFields) (CustomResponse, int, error) {
var resp CustomResponse
costJSON, _ := json.Marshal(&customFields)
p := url.Values{
"customField": {string(costJSON)},
}
data, status, err := c.PostRequest(
fmt.Sprintf("/custom-fields/%s/%s/edit", customFields.Entity, customFields.Code), p,
)
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
2019-02-21 11:51:50 +03:00
return resp, status, nil
}
2022-08-09 13:45:11 +03:00
// BonusOperations returns history of the bonus account for all participations
//
// For more information see https://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-loyalty-bonus-operations
2022-08-09 13:45:11 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.BonusOperations(retailcrm.BonusOperationsRequest{
// Filter: BonusOperationsFilter{
// LoyaltyId: []int{123, 456},
// },
// Limit: 20,
2022-08-09 13:45:11 +03:00
// Cursor: "123456",
// })
//
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// for _, value := range data.BonusOperations {
2022-08-09 13:45:11 +03:00
// log.Printf("%v\n", value)
// }
func (c *Client) BonusOperations(parameters BonusOperationsRequest) (BonusOperationsResponse, int, error) {
var resp BonusOperationsResponse
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("/loyalty/bonus/operations?%s", params.Encode()))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}
// AccountBonusOperations returns history of the bonus account for a specific participation
//
// For more information see https://www.simla.com/docs/Developers/API/APIVersions/APIv5#get--api-v5-loyalty-account-id-bonus-operations
2022-08-09 13:45:11 +03:00
//
// Example:
//
// var client = retailcrm.New("https://demo.url", "09jIJ")
//
// data, status, err := client.AccountBonusOperations(retailcrm.AccountBonusOperationsRequest{
// Filter: AccountBonusOperationsFilter{
// CreatedAtFrom: "2012-12-12",
// CreatedAtTo: "2012-12-13",
// },
// Limit: 20,
2022-08-09 13:45:11 +03:00
// Page: 3,
// })
//
// if err != nil {
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
// log.Fatalf("http status: %d, %s", status, apiErr.String())
// }
//
// log.Fatalf("http status: %d, error: %s", status, err)
// }
//
// for _, value := range data.BonusOperations {
2022-08-09 13:45:11 +03:00
// log.Printf("%v\n", value)
// }
func (c *Client) AccountBonusOperations(id int, parameters AccountBonusOperationsRequest) (BonusOperationsResponse, int, error) {
var resp BonusOperationsResponse
if id == 0 {
c.writeLog("cannot get loyalty bonus operations for user with id %d", id)
}
2022-08-09 13:45:11 +03:00
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf(
"/loyalty/account/%d/bonus/operations?%s",
2022-08-09 13:45:11 +03:00
id, params.Encode(),
))
if err != nil {
return resp, status, err
}
err = json.Unmarshal(data, &resp)
if err != nil {
return resp, status, err
}
return resp, status, nil
}