Skip to content

Commit

Permalink
Added inventory resource
Browse files Browse the repository at this point in the history
  • Loading branch information
imbue committed Sep 12, 2023
1 parent d55070b commit 33f73b6
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions inventory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package bigcommerce

import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

type InventoryResource struct {
Inventories []Inventory `json:"data"`
Meta Meta `json:"meta"`
}

type Identity struct {
Sku string `json:"sku"`
VariantID int `json:"variant_id"`
ProductID int `json:"product_id"`
}
type Settings struct {
SafetyStock int `json:"safety_stock"`
IsInStock bool `json:"is_in_stock"`
WarningLevel int `json:"warning_level"`
BinPickingNumber string `json:"bin_picking_number"`
}
type Inventory struct {
Identity Identity `json:"identity"`
AvailableToSell int `json:"available_to_sell"`
TotalInventoryOnhand int `json:"total_inventory_onhand"`
Settings Settings `json:"settings"`
}

type Links struct {
Previous string `json:"previous"`
Current string `json:"current"`
Next string `json:"next"`
}

type Meta struct {
Pagination Pagination `json:"pagination"`
}

func (bc *Client) GetInventoryForLocation(ID int64, filters map[string]string) (*InventoryResource, error) {
var params []string
for k, v := range filters {
params = append(params, fmt.Sprintf("%s=%s", k, v))
}

url := fmt.Sprintf("/v3/inventory/locations/%d/items", ID) + strings.Join(params, "&")

req := bc.getAPIRequest(http.MethodGet, url, nil)
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, err
}

defer res.Body.Close()
body, err := processBody(res)
if err != nil {
if res.StatusCode == http.StatusNoContent {
return &InventoryResource{}, nil
}
return nil, err
}

var resource InventoryResource
err = json.Unmarshal(body, &resource)

if err != nil {
return nil, err
}
return &resource, nil
}

0 comments on commit 33f73b6

Please sign in to comment.