GithubHelp home page GithubHelp logo

go-prestashop-api's Introduction

go-prestashop-api

A PrestaShop API Golang Wrapper for the Prestashop Webservice API

Install

$ go get github.com/dinistavares/go-prestashop-api

Usage

Create a new API client and authenticate with your Webservice key. You should specify the URL protocol by prefixing your domain name with https:// or http://. Follow the Prestashop documentation to enable the webservice through the Prestashop user interface or programmatically then create your API keys through the Prestashop user interface or programmatically.

Authenticate

import (
  "github.com/dinistavares/go-prestashop-api"
)

func main(){
  shopURL := "https://example.com"
  key := "xxxxxxx"

  client, err := prestashop.New(shopURL)

  if err != nil {
    // handle error

    return
  }

  client.Authenticate(key)
}

Resources

The API routes are broken down into services, the supported services are:

  • Carts (Create, Get, List, ListCartsByCustomerID)
  • Customers (Create, Get, GetCustomersByEmail, List)
  • Orders (Create, Get, List, ListOrdersByCustomerID)
  • Products (Create, Get, List)

Get Customer by ID with all fields.

func (client *prestashop.Client) listOrders() {
  customer, _, err := client.Customer.Get(2, nil)

  if err != nil {
    // Handle errors

    return
  }


  // ....
}

Get Customer by ID with specified fields.

func (client *prestashop.Client) listOrders() {
  params := &prestashop.ServiceListParams{
    // Results should only show firstname & email
    Display: &prestashop.ServiceListDisplay{
      "firstname",
      "email"
    },
  }

  customer, _, err := client.Customer.Get(2, params)

  if err != nil {
    // Handle errors

    return
  }


  // ....
}

List customer with firstname 'bob'.

func (client *prestashop.Client) listOrders() {
  params := &prestashop.ServiceListParams{
    // Results should only show customer id, firstname & email
    Display: &prestashop.ServiceListDisplay{
      "id",
      "firstname",
      "email"
    },
    // Filter customers with firstname as 'bob'
    Filter: &prestashop.ServiceListFilter{
      Key: "firstname",
      Values: []string{"bob"},
      Operator: prestashop.ListFilterOperatorLiteral,
    },
  }

  customer, _, err := client.Customer.List(params)

  if err != nil {
    // Handle errors

    return
  }


  // ....
}

XML Support

func (client *prestashop.Client) listOrders() {
  params := &prestashop.ServiceListParams{
    Display: &prestashop.ServiceListDisplay{
      "id",
      "name",
      "price",
    },
  }

  product, _, err := client.Product.Get(1, params)

  if err != nil {
    // Handle error

    return
  }

  if product != nil {
    productXML, _ := xml.Marshal(*product)
    fmt.Print(string(productXML))
  } else {
    // Hanlde no product found
  }


  // ....
}

Result:

<Product>
    <id>1</id>
    <price>23.900000</price>
    <name>
        <language id="1" href="https://crisp-plugin-dinis.ngrok.io/api/languages/1">Hummingbird printed t-shirt</language>
        <language id="2" href="https://crisp-plugin-dinis.ngrok.io/api/languages/3">تيشيرت بطبعة الطائر الطنان</language>
    </name>
</Product>

JSON Support

func (client *prestashop.Client) listOrders() {
  params := &prestashop.ServiceListParams{
    Display: &prestashop.ServiceListDisplay{
      "id",
      "name",
      "price",
    },
  }

  product, _, err := client.Product.Get(1, params)

  if err != nil {
    // Handle error

    return
  }

  if product != nil {
    productJSON, _ := json.Marshal(*product)
    fmt.Print(string(productJSON))
  } else {
    // Hanlde no product found
  }


  // ....
}

Result:

{
  "id": 1,
  "price": "23.900000",
  "name": {
    "language": [
      {
        "id": "1",
        "href": "https://crisp-plugin-dinis.ngrok.io/api/languages/1",
        "text": "Hummingbird printed t-shirt"
      },
      {
        "id": "2",
        "href": "https://crisp-plugin-dinis.ngrok.io/api/languages/3",
        "text": "تيشيرت بطبعة الطائر الطنان"
      }
    ]
  }
}

go-prestashop-api's People

Contributors

dinistavares avatar

Watchers

 avatar

go-prestashop-api's Issues

Suggestion: Add IDLang to the Query Params to get the right data

If possible, change the struct like this:

type ServiceListParams struct {
...
IDLang int
}

Then the client will return the list/detail data like:
lang = 1 & id = 1: p.Name => "Hummingbird printed t-shirt"
lang = 2 & id = 1: p.Name => "تيشيرت بطبعة الطائر الطنان"

Fill free if you have any better solution.

:)

Can't get Product.Name in search results

I'm testing the api with default prestashop 8.1.3

params := &prestashop.ServiceListParams{
	Display: &prestashop.ServiceListDisplay{
		"id",
		"name",
		"price",
	},
}

list, response, err := client.Product.List(params)
if err != nil {
	slog.Error("prestashop", "err", err.Error())
	return
}

slog.Info("prestashop", "response", response)

for _, v := range *list {
	//slog.Info("prestashop", "product", v)
	slog.Info("prestashop", "product", fmt.Sprintf("id: %d, name: %s, price: %s", v.ID, v.Name, v.Price))
}

Then I got these:

2024/01/12 22:33:48 INFO prestashop product="id: 1, name: , price: 23.900000"
2024/01/12 22:33:48 INFO prestashop product="id: 2, name: , price: 35.900000"
2024/01/12 22:33:48 INFO prestashop product="id: 16, name: , price: 12.900000"

Please help, thanks,

[email protected]

Struct Name or Tag Error

As the new struct LanguageData with Language array, the original key name should be changed back with the xml definition.

Product {
Names *LanguageData xml:"name,omitempty" json:"name,omitempty"
}
==> Product {
Name
}

and the Associations xml maybe error:

type ProductAssociations struct {
ProductRows *[]ProductRow xml:"product_rows>cart_row,omitempty" json:"cart_rows,omitempty"
}

Customer struct ID *int ?

Testing the find by email service, got these result:

2024/01/25 11:19:54 INFO prestashop "customer by email"="&{ID:0xc000014c08 IDDefaultGroup:3 IDLang:3 NewsletterDateAdd:2024-01-25 12:14:56 ..."

type Customer struct {
ID *int
...
}

Would the ID be changed to int better than *int?

Thanks

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.