forked from TimmyGuy/bigcommerce-api-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |