Giter Site home page Giter Site logo

api-client-go's Introduction

Build Status Covarage GitHub release Go Report Card GoLang version pkg.go.dev

RetailCRM API Go client

This is golang RetailCRM API client.

Installation

go get -u github.com/retailcrm/api-client-go/v2

Usage

Example:

package main

import (
	"log"

	"github.com/retailcrm/api-client-go/v2"
)

func main() {
	var client = retailcrm.New("https://demo.retailcrm.pro", "09jIJ09j0JKhgyfvyuUIKhiugF")

	data, status, err := client.Orders(retailcrm.OrdersRequest{
		Filter: retailcrm.OrdersFilter{},
		Limit: 20,
		Page: 1,
	})
	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.Orders {
		log.Printf("%v\n", value.Email)
	}

	log.Println(data.Orders[1].FirstName)

	inventories, status, err := client.InventoriesUpload([]retailcrm.InventoryUpload{
			{
				XMLID: "pTKIKAeghYzX21HTdzFCe1",
				Stores: []retailcrm.InventoryUploadStore{
					{
						Code: "test-store-v5",
						Available: 10,
						PurchasePrice: 1500,
					},
					{
						Code: "test-store-v4",
						Available: 20,
						PurchasePrice: 1530,
					},
					{
						Code: "test-store",
						Available: 30,
						PurchasePrice: 1510,
					},
				},
			},
			{
				XMLID: "JQIvcrCtiSpOV3AAfMiQB3",
				Stores: []retailcrm.InventoryUploadStore{
					{
						Code: "test-store-v5",
						Available: 45,
						PurchasePrice: 1500,
					},
					{
						Code: "test-store-v4",
						Available: 32,
						PurchasePrice: 1530,
					},
					{
						Code: "test-store",
						Available: 46,
						PurchasePrice: 1510,
					},
				},
			},
		},
	)
	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.Println(inventories.ProcessedOffersCount)
}

You can use different error types and retailcrm.AsAPIError to process client errors. Example:

package retailcrm

import (
	"errors"
	"log"
	"os"
	"strings"

	"github.com/retailcrm/api-client-go/v2"
)

func main() {
	var client = retailcrm.New("https://demo.retailcrm.pro", "09jIJ09j0JKhgyfvyuUIKhiugF")

	resp, status, err := client.APICredentials()
	if err != nil {
		apiErr, ok := retailcrm.AsAPIError(err)
		if !ok {
			log.Fatalf("http status: %d, error: %s", status, err)
		}

		switch {
		case errors.Is(apiErr, retailcrm.ErrMissingCredentials):
			log.Fatalln("No API key provided.")
		case errors.Is(apiErr, retailcrm.ErrInvalidCredentials):
			log.Fatalln("Invalid API key.")
		case errors.Is(apiErr, retailcrm.ErrAccessDenied):
			log.Fatalln("Access denied. Please check that the provided key has access to the credentials info.")
		case errors.Is(apiErr, retailcrm.ErrAccountDoesNotExist):
			log.Fatalln("There is no RetailCRM at the provided URL.")
		case errors.Is(apiErr, retailcrm.ErrMissingParameter):
			// retailcrm.APIError in this case will always contain "Name" key in the errors list with the parameter name.
			log.Fatalln("This parameter should be present:", apiErr.Errors()["Name"])
		case errors.Is(apiErr, retailcrm.ErrValidation):
			log.Println("Validation errors from the API:")

			for name, value := range apiErr.Errors() {
				log.Printf(" - %s: %s\n", name, value)
			}

			os.Exit(1)
		case errors.Is(apiErr, retailcrm.ErrGeneric):
			log.Fatalf("failure from the API. %s", apiErr.String())
		}
	}

	log.Println("Available scopes:", strings.Join(resp.Scopes, ", "))
}

Upgrading

Please check the UPGRADING.md to learn how to upgrade to the new version.

api-client-go's People

Contributors

blide avatar defernest avatar dendd1 avatar gridnevalex avatar gwinn avatar ilyavlasoff avatar iyzoer avatar kifril avatar muxx avatar neur0toxine avatar pm14kas avatar rencurs avatar soldierofheart avatar tishmaria90 avatar vragovr avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

api-client-go's Issues

Method OrderCreate response

The response does not return the order object, although according to the api documentation it should be present

CustomerCreate returns error when using tags

When attempting to create a customer through client, I'm getting an error:

apiErrs:
key = "tags"
value = "The following tags are invalid: Array\n"

For example:

phone := v5.Phone{Number: lead.Phone}

tag := v5.Tag{
	Name: "opt-lead",
}
tags := []v5.Tag{}
tags = append(tags, tag)
customer := v5.Customer{
	FirstName: lead.Name,
	Email:     lead.Email,
	Phones:    []v5.Phone{phone},
	Tags:      tags,
	Site:      "opt.dreamwhite.ru",
}

resp, status, err := tools.RetailClient.CustomerCreate(customer, "opt")

This probably means that since updating Customer with Tags in #40 RetailCRM backend still expects just an array of strings instead of an array of objects with name and color

Delivery order to dpd

I can not fill in the fields for dpd delivery:
Fields locked, selfPickup, payerType not in api and golang types
Field deliveryType not in golang types

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.