wc-api-go

module
v0.0.0-...-31b83a3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 24, 2021 License: MIT

README

WooCommerce API - Golang Client

A Golang wrapper for the WooCommerce REST API. Easily interact with the WooCommerce REST API securely using this library. If using a HTTPS connection this library uses BasicAuth, else it uses Oauth to provide a secure connection to WooCommerce.

Build Status

Installation

To install this WooCommerce REST API Golang Wrapper, use go get:

go get github.com/DanyelMorales/wc-api-go

Staying up to date

To update WooCommerce REST API Golang Wrapper to the latest version, use

go get -u github.com/DanyelMorales/wc-api-go

Getting started

Generate API credentials (Consumer Key & Consumer Secret) following this instructions http://docs.woocommerce.com/document/woocommerce-rest-api/.

Check out the WooCommerce API endpoints and data that can be manipulated in https://woocommerce.github.io/woocommerce-rest-api-docs/.

Setup

Setup for the new WP REST API integration:

import "github.com/DanyelMorales/wc-api-go/client"

func main() {
	factory := client.Factory{}
	c := factory.NewClient(options.Basic{
		URL:    "http://example.com",
		Key:    "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
		Secret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
		Options: options.Advanced{
			WPAPI:       true,
			WPAPIPrefix: "/wp-json/",
			Version:     "wc/v3",
		},
	})

	// Further using of client ... 
}
Options
Option Type Required Description
URL string yes Your Store URL, example: http://woo.dev/
Key string yes Your API consumer key
Secret string yes Your API consumer secret
Options struct no Extra arguments (see client options table)
Advanced options
Option Type Required Description
WPAPI bool no Allow make requests to the new WP REST API integration (WooCommerce 2.6 or later)
WPAPIPrefix string no Custom WP REST API URL prefix, used to support custom prefixes created with the rest_url_prefix filter
Version string no API version, default is v3
Timeout int no Request timeout, default is 15
FollowRedirects bool no Allow the API call to follow redirects
VerifySsl bool no Verify SSL when connect, use this option as false when need to test with self-signed certificates, default is true
QueryStringAuth bool no Force Basic Authentication as query string when true and using under HTTPS, default is false
OAuthTimestamp string no Custom oAuth timestamp, default is time()
UserAgent string no Custom user-agent, default is WooCommerce API Client-PHP

Methods

Params Type Description
endpoint string WooCommerce API endpoint, example: customers or order/12
data byte array Only for POST and PUT, data that will be converted to JSON
parameters array Only for GET and DELETE, request query string

Post method

Post method receives a byte array as argument, it'll be the payload of the request and usually it's type is:

map[string][]interface{}

There is another type which represents said type of payload and contains useful method for byte array conversion:

data := make(request.JsonMap)
data["update"] = yourPayload
data.ToByteArray()
request.JsonMap methods
Method Args Description
Add key string, value interface{} Add an array into a registry associated with a key
Set key string, value interface{} Add a value into a registry associated with a key
Get key string Return the []interface{} associated to a key
ToByteArray `` Convert registry to []byte
Remove key string Remove key and associated data from registry
RemoveIndex key string, index int Remove a value from []interface{} associated to a key
GET
c.Get(endpoint, parameters)
POST
data :=make(request.JsonMap)
data.Add("create", yourPayloadHere)
c.Post(endpoint, data.ToByteArray())
PUT
c.Put(endpoint, data)
DELETE
c.Delete(endpoint, parameters)
OPTIONS
c.Options(endpoint)
Response

All methods will return *http.Response on success and returning an error on failure.

package main

import (
	"fmt"
	"github.com/DanyelMorales/wc-api-go/client"
	"github.com/DanyelMorales/wc-api-go/options"
	"io/ioutil"
	"log"
	"net/http"
)

func main() {
	factory := client.Factory{}
	c := factory.NewClient(options.Basic{
		URL:    "http://woo.dev/",
		Key:    "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
		Secret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
		Options: options.Advanced{
			WPAPI:       true,
			WPAPIPrefix: "/wp-json/",
			Version:     "wc/v3",
		},
	})

	if r, err := c.Get("products", nil); err != nil {
		log.Fatal(err)
	} else if r.StatusCode != http.StatusOK {
		log.Fatal("Unexpected StatusCode:", r)
	} else {
		defer r.Body.Close()
		if bodyBytes, err := ioutil.ReadAll(r.Body); err != nil {
			log.Fatal(err)
		} else {
			fmt.Println(string(bodyBytes))
		}
	}
}

Release History

  • 2019-04-14 - 1.0.1 - Fix sample integration setup in README.md
  • 2019-01-12 - 1.0.0 - First Release
  • 2021-01-23 - 1.0.1 - Fix post payload issues

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL