Skip to content
/ cv3go Public
forked from estivate/cv3go

Go library for abstracting the CV3 API. Provides types, structs and methods for common tasks.

License

Notifications You must be signed in to change notification settings

cv3/cv3go

 
 

Repository files navigation

cv3go

cv3go is a go library for working with the CV3 API. It's a thin wrapper around the raw http requests that takes care of authentication and allows the user to just work with the JSON objects that comprise the request and response payloads.

Install

After you've installed Go and set up your project workspace, you can install this module with:

go get -u github.com/cv3/cv3go/v2

Then you can use it in your code via a standard import:

import "github.com/cv3/cv3go/v2"

Quick Start

package main

import (
  "fmt"
  "os"

  "github.com/cv3/cv3go/v2"
)

const (
  REST_KEY    = "CV3_REST_API_KEY"
  REST_SECRET = "CV3_REST_API_SECRET"
)

func main() {

  payload := `{ 
    "data": {
      "exportProducts": {
        "export_by_range": {
          "start": 1
        }
      }
    }
  }
  `

  api := cv3go.NewApi(REST_KEY, REST_SECRET)
  data, err := api.Execute("products", payload)

  if err != nil {
    fmt.Printf("request failed: %v\n", err)
    os.Exit(0)
  }

  fmt.Println(string(data))
}

Detailed Usage

Initializing an API connection & Authentication

Call NewApi(key string, secret string) with your REST authentication credentials one time at the beginning of your project. You can use this object to make all subsequest requests to any CV3 endpoint, even in a long-running process. It will manage all endpoint authentication tokens and refresh them as needed.

Note that when you call this method it just instantiates the struct. No calls are actually made to the CV3 API until a data request is made. At that time if any authentication requests are required they will occur.

api := cv3go.NewApi(REST_KEY, REST_SECRET)

Making Data Requests

Call Execute(endpoint string, payload string) on your initialized Api Object to interact with the API. You can utilize any JSON payload requests supported by the API.

payload := `{
  "data": {
    "orderExport": {
      "newOrders": true
    }
  }
}
`
data, err := api.Execute("orders", payload)

The above payload will export all pending orders. The example below will update the name of a particular product SKU:

payload := `{
  "data": {
    "importProducts": {
      "products":[
        {
          "sku": "0001",
          "name": "New Product Name"
        }
      ]
    }
  }
}
`
data, err := api.Execute("products", payload)

You can make as many requests as you like to any endpoints on a single API object.

Umarshal raw JSON responses

The JSON response body from the API is provided in raw form for further processing. You can create your own structs to hold just the data you want, or you can unmarshal the JSON into the default Schema provided by this library. Here's an example of using the built-in schema:

data, _ := api.Execute("products", payload)

jsonData := cv3go.Data{}
json.Unmarshal(data, &jsonData)

fmt.Println(jsonData.ExportProducts.Products[0].Name)

You can review the default schema here

Using With Postman

If you use Postman to construct API requests, the code you include in the Body tab of the endpoint request can be copy/pasted into your Go code to create the payload strings. We have Postman collections available here if you'd care to try it.

More documentation

Full CV3 API documentation is available here.

History & Breaking Changes

Version 1.x.x of this library targeted the CV3 SOAP API and was constructed prior to the advent of Go modules. It is not recommended to use this version due to changes in Go and in the CV3 APIs themselves, but if needed it's still available and this is how you use it.

Version 2.x.x of this library targets the CV3 REST API and is not used in the same way as the 1.x.x versions. Do not upgrade legacy code using v1 to v2 or IT WILL BREAK STUFFS. However, if you have reviewed documentation and are familiar with the JSON payloads of the REST API, you may not have too much trouble updating legacy applications to utilize the newer version and by extention the REST API.

Contributing

Contribute here or by contacting CV3 Support.

About

Go library for abstracting the CV3 API. Provides types, structs and methods for common tasks.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%