From d00330738c0c5f83fc4aec32acf20f36dde5ea6d Mon Sep 17 00:00:00 2001 From: alecha Date: Fri, 29 Jan 2016 16:25:43 +0100 Subject: [PATCH 1/9] adding first orders-related structs and wrappers --- shopify/model.go | 221 +++++++++++++++++++++++++++++++++++++++++++ shopify/responses.go | 21 ++++ shopify/shopify.go | 59 +++++------- shopify/wrapper.go | 81 ++++++++++++++++ 4 files changed, 349 insertions(+), 33 deletions(-) create mode 100644 shopify/model.go create mode 100644 shopify/responses.go create mode 100644 shopify/wrapper.go diff --git a/shopify/model.go b/shopify/model.go new file mode 100644 index 0000000..666a1f4 --- /dev/null +++ b/shopify/model.go @@ -0,0 +1,221 @@ +package shopify + +import "time" + +//BillingAddress is a billing address +type BillingAddress struct { + Address1 string `json:"address1"` + Address2 string `json:"address2"` + City string `json:"city"` + Company string `json:"company"` + Country string `json:"country"` + FirstName string `json:"first_name"` + ID int64 `json:"id"` + LastName string `json:"last_name"` + Phone string `json:"phone"` + Province string `json:"province"` + Zip string `json:"zip"` + Name string `json:"name"` + ProvinceCode string `json:"province_code"` + CountryCode string `json:"country_code"` + Default bool `json:"default"` +} + +//ClientDetails are details about a client +type ClientDetails struct { + AcceptLanguage *string `json:"accept_language"` //TODO check + BrowserHeight int `json:"browser_height"` + BrowserIP string `json:"browser_ip"` + BrowserWidth int `json:"browser_width"` + SessionHash *string `json:"session_hash"` + UserAgent *string `json:"user_agent"` +} + +//Customer is a customer +type Customer struct { + AcceptsMarketing bool `json:"accepts_marketing"` + CreatedAt time.Time `json:"created_at"` + Email string `json:"email"` + ID int64 `json:"id"` + FirstName string `json:"first_name"` + Note string `json:"note"` + LastName string `json:"last_name"` + OrdersCount int `json:"orders_count"` + State string `json:"state"` + TotalSpent string `json:"total_spent"` + UpdatedAt time.Time + Tags string `json:"tags"` +} + +//DiscountCode is a discount code +type DiscountCode struct { + Amount string `json:"amount"` + Code string `json:"code"` + Type string `json:"type"` +} + +//Fulfillment is a fulfillment +type Fulfillment struct { + ID int64 `json:"id"` + CreatedAt time.Time `json:"created_at"` + OrderID int64 `json:"order_id"` + Status string `json:"status"` + TrackingCompany string `json:"tracking_company"` + TrackingNumber string `json:"tracking_number"` + UpdatedAt time.Time `json:"updated_at"` +} + +//LineItem is an order line item +type LineItem struct { + FulfillableQuantity int `json:"fulfillable_quantity"` + FulfillmentService *string `json:"fulfillment_service"` + FulfillmentStatus *string `json:"fulfillment_status"` + Grams int `json:"grams"` + ID int64 `json:"id"` + Price string `json:"price"` //e.g. 199.99 + ProductID int64 `json:"product_id"` + Quantity int `json:"id"` + RequiresShipping bool `json:"requires_shipping"` + SKU string `json:"sku"` + Title string `json:"title"` + VariantID int64 `json:"variant_id"` + VariantTitle string `json:"variant_title"` + Vendor string `json:"vendor"` + GiftCard *bool `json:"gift_card"` + Taxable bool `json:"taxable"` + TaxLines []TaxLine `json:"tax_line"` + TotalDiscount string `json:"total_discount"` +} + +//NoteAttribute is a note attribute +type NoteAttribute struct { + Name string `json:"name"` + Value string `json:"value"` +} + +//Order is a product +type Order struct { + BillingAddress BillingAddress `json:"billing_address"` + BrowserIP string `json:"browser_ip"` + BuyerAcceptsMarketing bool `json:"buyer_accepts_marketing"` + CancelReason *string `json:"cancel_reason"` + CancelledAt *time.Time `json:"cancelled_at"` + ClientDetails ClientDetails `json:"client_details"` + ClosedAt *time.Time `json:"closed_at"` + CreatedAt time.Time `json:"created_at"` + Currency string `json:"currency"` + Customer *Customer `json:"customer"` + DiscountCodes []DiscountCode `json:"discount_codes"` + Email string `json:"email"` + FinancialStatus string `json:"financial_status"` + Fulfillments []Fulfillment `json:"fulfillments"` + FulfillmentStatus string `json:"fulfillment_status"` + Tags string `json:"tags"` + ID int64 `json:"id"` + LandingSite string `json:"landing_site"` + LineItems []LineItem `json:"line_items"` + Name string `json:"name"` + Note *string `json:"note"` + NoteAttributes []NoteAttribute `json:"note_attributes"` + Number int64 `json:"number"` + OrderNumber int64 `json:"order_number"` + PaymentGatewayNames []string `json:"payment_gateway_names"` + ProcessedAt time.Time `json:"processed_at"` + ProcessingMethod string `json:"processing_method"` + ReferringSite string `json:"referring_site"` + Refunds []Refund `json:"refunds"` + ShippingAddress ShippingAddress `json:"shipping_address"` + ShippingLines []ShippingLine `json:"shipping_lines"` + SourceName string `json:"source_name"` + SubtotalPrice string `json:"subtotal_price"` + TaxLines []TaxLine `json:"tax_lines"` + TaxesIncluded bool `json:"taxes_included"` + TotalDiscounts string `json:"total_discounts"` + TotalPrice string `json:"total_price"` + TotalTax string `json:"total_tax"` + TotalWeight float64 `json:"total_weight"` + UpdatedAt time.Time `json:"updatedAt"` +} + +//PaymentDetails are the details about a payment +type PaymentDetails struct { + AvsResultCode *string `json:"avs_result_code"` + CreditCardBin *string `json:"credit_card_bin"` + CvvResultCode *string `json:"cvv_result_code"` + CreditCardNumber string `json:"credit_card_number"` + CreditCardCompany string `json:"credit_card_company"` +} + +//Refund is a refund +type Refund struct { + CreatedAt time.Time `json:"created_at"` + ID int64 `json:"id"` + Note string `json:"note"` + RefundLineItems []RefundLineItem `json:"refund_line_items"` + Restock bool `json:"restock"` + Transactions []Transaction `json:"transactions"` + UserID int64 `json:"user_id"` +} + +//RefundLineItem is a refund line item +type RefundLineItem struct { + ID int64 `json:"id"` + LineItem LineItem `json:"line_item"` + LineItemID int64 `json:"line_item_id"` + Quantity int `json:"quantity"` +} + +//ShippingAddress is a billing address +type ShippingAddress struct { + Address1 string `json:"address1"` + Address2 string `json:"address2"` + City string `json:"city"` + Company string `json:"company"` + Country string `json:"country"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + Latitude float64 `json:"latitude"` + Longitude float64 `json:"longitude"` + Phone string `json:"phone"` + Province string `json:"province"` + Zip string `json:"zip"` + Name string `json:"name"` + ProvinceCode string `json:"province_code"` + CountryCode string `json:"country_code"` +} + +//ShippingLine is a shipping line +type ShippingLine struct { + Code string `json:"code"` + Price string `json:"price"` + Source string `json:"source"` + Title string `json:"title"` + TaxLines []TaxLine `json:"tax_lines"` +} + +//TaxLine is a tax line +type TaxLine struct { + Title string `json:"title"` + Price float64 `json:"price"` + Rate float64 `json:"rate"` +} + +//Transaction is a transaction +type Transaction struct { + Amount string `json:"amount"` + Authorization *string `json:"authorization"` + CreatedAt time.Time `json:"created_at"` + DeviceID *string `json:"device_id"` + Gateway string `json:"gateway"` + SourceName string `json:"source_name"` + PaymentDetails PaymentDetails `json:"payment_details"` + ID int64 `json:"id"` + Kind string `json:"kind"` + OrderID int64 `json:"orderId"` + Receipt string `json:"receipt"` + ErrorCode string `json:"error_code"` + Status string `json:"status"` + Test bool `json:"test"` + UserID *int64 `json:"userId"` + Currency string `json:"currency"` +} diff --git a/shopify/responses.go b/shopify/responses.go new file mode 100644 index 0000000..e1f1a4f --- /dev/null +++ b/shopify/responses.go @@ -0,0 +1,21 @@ +package shopify + +//OrdersResponse is a response to /orders endpoint +type OrdersResponse struct { + Orders []Order `json:"orders"` +} + +//OrderResponse is a response to /orders endpoint +type OrderResponse struct { + Order Order `json:"order"` +} + +//TransactionsResponse is a response to /orders/{id}/transactions +type TransactionsResponse struct { + Transactions []Transaction `json:"transactions"` +} + +//CountResponse is a response to counts endpoint +type CountResponse struct { + Count int `json:"count"` +} diff --git a/shopify/shopify.go b/shopify/shopify.go index 4dc49dc..c9bb15f 100755 --- a/shopify/shopify.go +++ b/shopify/shopify.go @@ -1,11 +1,10 @@ -// Package shopify: -// go-shopify provides an easy-to-use API -// for making CRUD request to shopify. +// Package shopify provides an easy-to-use API for making CRUD request to shopify. package shopify import ( - "fmt" "encoding/json" + "fmt" + "github.com/parnurzeal/gorequest" ) @@ -13,26 +12,24 @@ import ( // to wrap our request operations. type Shopify struct { // Store domain-name - store string + store string // Store API key - apiKey string + apiKey string // Store password - pass string + pass string } const ( domain = ".myshopify.com/admin" ) -// Creates a New Shopify Store API object with the +// New Creates a New Shopify Store API object with the // store, apiKey and pass of your store. // Usage: // shopify.New("mystore", "XXX","YYY") func New(store, apiKey, pass string) Shopify { - shop := Shopify{store: store, apiKey: apiKey, pass: pass} // fmt.Println("[New] Creating Shopify client with: ", store, apiKey, pass) - return shop } @@ -43,8 +40,8 @@ func (shopify *Shopify) createTargetURL(endpoint string) string { return result } -// Extracts Json Bytes from map[string]interface -func getJsonBytesFromMap(data map[string]interface{}) ([]byte, error) { +// getJSONBytesFromMap Extracts Json Bytes from map[string]interface +func getJSONBytesFromMap(data map[string]interface{}) ([]byte, error) { jsonData, err := json.Marshal(data) if err != nil { fmt.Println("Invalid data object, can't parse to json:") @@ -55,7 +52,7 @@ func getJsonBytesFromMap(data map[string]interface{}) ([]byte, error) { return jsonData, nil } -// Creates a new Request to Shopify and returns +// Request Creates a new Request to Shopify and returns // the response as a map[string]interface{}. // method: GET/POST/PUT - string // url: target endpoint like "products" - string @@ -63,12 +60,11 @@ func getJsonBytesFromMap(data map[string]interface{}) ([]byte, error) { // Usage: shopify.request("GET","products",nil) func (shopify *Shopify) Request(method, endpoint string, data map[string]interface{}) ([]byte, []error) { // fmt.Println("[request] Arguments: ", method, endpoint, data) - - jsonData, _ := getJsonBytesFromMap(data) + jsonData, _ := getJSONBytesFromMap(data) // fmt.Println("[request] data: ", string(jsonData)) targetURL := shopify.createTargetURL(endpoint) - // fmt.Println("[request] targetUrl: ", targetURL) + // fmt.Println("[request] targetURL: ", targetURL) request := gorequest.New() request.Get(targetURL) @@ -79,39 +75,36 @@ func (shopify *Shopify) Request(method, endpoint string, data map[string]interfa _, body, errs := request.End() - return []byte(body), errs } -// Makes a GET request to shopify with the +// Get Makes a GET request to shopify with the // given endpoint. // Usage: // shopify.Get("products/5.json") // shopify.Get("products/5/variants.json") func (shopify *Shopify) Get(endpoint string) ([]byte, []error) { - - targetUrl := shopify.createTargetURL(endpoint) - + targetURL := shopify.createTargetURL(endpoint) request := gorequest.New() - _, body, errs := request.Get(targetUrl).End() + _, body, errs := request.Get(targetURL).End() return []byte(body), errs } -// Makes a POST request to shopify with the +// Post Makes a POST request to shopify with the // given endpoint and data. // Usage: // shopify.Post("products", map[string]interface{} = product data map) func (shopify *Shopify) Post(endpoint string, data map[string]interface{}) ([]byte, []error) { - targetUrl := shopify.createTargetURL(endpoint) - jsonData, err := getJsonBytesFromMap(data) + targetURL := shopify.createTargetURL(endpoint) + jsonData, err := getJSONBytesFromMap(data) if err != nil { return nil, []error{err} } request := gorequest.New() - request.Post(targetUrl) + request.Post(targetURL) if jsonData != nil && data != nil { request.Send(string(jsonData)) } @@ -120,20 +113,20 @@ func (shopify *Shopify) Post(endpoint string, data map[string]interface{}) ([]by return []byte(body), errs } -// Makes a PUT request to shopify with the +// Put Makes a PUT request to shopify with the // given endpoint and data. // Usage: // shopify.Put("products", map[string]interface{} = product data map) func (shopify *Shopify) Put(endpoint string, data map[string]interface{}) ([]byte, []error) { - targetUrl := shopify.createTargetURL(endpoint) - jsonData, err := getJsonBytesFromMap(data) + targetURL := shopify.createTargetURL(endpoint) + jsonData, err := getJSONBytesFromMap(data) if err != nil { return nil, []error{err} } request := gorequest.New() - request.Put(targetUrl) + request.Put(targetURL) if jsonData != nil && data != nil { request.Send(string(jsonData)) } @@ -142,16 +135,16 @@ func (shopify *Shopify) Put(endpoint string, data map[string]interface{}) ([]byt return []byte(body), errs } -// Makes a DELETE request to shopify with the +// Delete Makes a DELETE request to shopify with the // given endpoint. // Usage: // shopify.Delete("products/5.json") func (shopify *Shopify) Delete(endpoint string) ([]byte, []error) { - targetUrl := shopify.createTargetURL(endpoint) + targetURL := shopify.createTargetURL(endpoint) request := gorequest.New() - _, body, errs := request.Delete(targetUrl).End() + _, body, errs := request.Delete(targetURL).End() return []byte(body), errs } diff --git a/shopify/wrapper.go b/shopify/wrapper.go new file mode 100644 index 0000000..159e8b3 --- /dev/null +++ b/shopify/wrapper.go @@ -0,0 +1,81 @@ +package shopify + +import ( + "encoding/json" + "fmt" + "log" +) + +//GetOrders returns all the orders +func (shopify *Shopify) GetOrders() ([]Order, []error) { + response, errors := shopify.Get("orders") + if len(errors) > 0 { + return nil, errors + } + + var orders OrdersResponse + if err := json.Unmarshal(response, &orders); err != nil { + return nil, []error{err} + } + return orders.Orders, nil +} + +//GetOrder returns a order given its id +func (shopify *Shopify) GetOrder(orderID int64) (*Order, []error) { + response, errors := shopify.Get(fmt.Sprintf("orders/%v", orderID)) + if len(errors) > 0 { + return nil, errors + } + + var orderResponse OrderResponse + if err := json.Unmarshal(response, &orderResponse); err != nil { + return nil, []error{err} + } + return &orderResponse.Order, nil +} + +//GetOrderTransactions returns the order's transactions +func (shopify *Shopify) GetOrderTransactions(orderID int64) ([]Transaction, []error) { + response, errors := shopify.Get(fmt.Sprintf("orders/%v/transactions", orderID)) + if len(errors) > 0 { + return nil, errors + } + + log.Print(string(response)) + + var transactionsResponse TransactionsResponse + if err := json.Unmarshal(response, &transactionsResponse); err != nil { + return nil, []error{err} + } + return transactionsResponse.Transactions, nil +} + +//GetOrderTransactionsCount returns the order's transactions count +func (shopify *Shopify) GetOrderTransactionsCount(orderID int64) (int, []error) { + response, errors := shopify.Get(fmt.Sprintf("orders/%v/transactions/count", orderID)) + if len(errors) > 0 { + return 0, errors + } + + log.Print(string(response)) + + var count CountResponse + if err := json.Unmarshal(response, &count); err != nil { + return 0, []error{err} + } + return count.Count, nil +} + +//GetOrdersCount returns all the products +func (shopify *Shopify) GetOrdersCount() (int, []error) { + response, errors := shopify.Get("orders/count") + if len(errors) > 0 { + return 0, errors + } + + var ordersCount CountResponse + if err := json.Unmarshal(response, &ordersCount); err != nil { + return 0, []error{err} + } + return ordersCount.Count, nil +} From 4fd03cdb1c394fa9feb46a7e470c6340226d4644 Mon Sep 17 00:00:00 2001 From: alecha Date: Fri, 29 Jan 2016 17:10:30 +0100 Subject: [PATCH 2/9] added the first basic product's wrappers --- shopify/model.go | 69 ++++++++++++++++++++++++++++++++++++++++++++ shopify/responses.go | 10 +++++++ shopify/shopify.go | 47 +++++++++--------------------- shopify/wrapper.go | 28 ++++++++++++++++++ 4 files changed, 120 insertions(+), 34 deletions(-) diff --git a/shopify/model.go b/shopify/model.go index 666a1f4..47df996 100644 --- a/shopify/model.go +++ b/shopify/model.go @@ -47,6 +47,23 @@ type Customer struct { Tags string `json:"tags"` } +//Discount is a discount +type Discount struct { + ID int64 `json:"id"` + DiscountType string `json:"discount_type"` + Code string `json:"code"` + Value string `json:"value"` + EndsAt time.Time `json:"ends_at"` + StartsAt time.Time `json:"starts_at"` + Status string `json:"status"` + MinimumOrderAmount string `json:"minimum_order_amount"` + UsageLimit int `json:"usage_limit"` + AppliesToID int64 `json:"applies_to_id"` + AppliesOnce bool `json:"applies_once"` + AppliesToResource string `json:"applies_to_resource"` + TimesUsed int `json:"times_used"` +} + //DiscountCode is a discount code type DiscountCode struct { Amount string `json:"amount"` @@ -65,6 +82,11 @@ type Fulfillment struct { UpdatedAt time.Time `json:"updated_at"` } +//Image is a product's image +type Image struct { + Src string `json:"src"` +} + //LineItem is an order line item type LineItem struct { FulfillableQuantity int `json:"fulfillable_quantity"` @@ -146,6 +168,27 @@ type PaymentDetails struct { CreditCardCompany string `json:"credit_card_company"` } +//Product is a product +type Product struct { + BodyHTML string `json:"body_html"` + CreatedAt time.Time `json:"created_at"` + Handle string `json:"handle"` + ID int64 `json:"id"` + Images []Image `json:"images"` + Options []map[string]interface{} `json:"options"` + ProductType string `json:"product_type"` + PublishedAt *time.Time `json:"published_at"` + PublishedScope string `json:"published_scope"` + Tags string `json:"tags"` + TemplateSuffix string `json:"template_suffix"` + Title string `json:"title"` + MetafieldsGlobalTitleTag string `json:"metafields_global_title_tag"` + MetafieldsGlobalDescriptionTag string `json:"metafields_global_description_tag"` + UpdatedAt time.Time `json:"updatedAt"` + Variants []Variant `json:"variants"` + Vendor string `json:"vendor"` +} + //Refund is a refund type Refund struct { CreatedAt time.Time `json:"created_at"` @@ -219,3 +262,29 @@ type Transaction struct { UserID *int64 `json:"userId"` Currency string `json:"currency"` } + +//Variant is a product's variant +type Variant struct { + BarCode string `json:"bar_code"` + CompareAtPrice string `json:"compare_at_price"` + CreatedAt time.Time `json:"created_at"` + FulfillmentService string `json:"fulfillment_service"` + Grams float64 `json:"grams"` + Weight float64 `json:"weight"` + WeightUnit string `json:"weight_unit"` + ID int64 `json:"id"` + InventoryManagement string `json:"inventory_management"` + InventoryPolicy string `json:"inventory_policy"` + InventoryQuantity int `json:"inventory_quantity"` + Option1 string `json:"option1"` + Option2 string `json:"option2"` + Option3 string `json:"option3"` + Position int `json:"position"` + Price string `json:"price"` + ProductID int64 `json:"product_id"` + RequiresShipping bool `json:"requires_shipping"` + SKU string `json:"sku"` + Taxable bool `json:"taxable"` + Title string `json:"title"` + UpdatedAt time.Time `json:"updated_at"` +} diff --git a/shopify/responses.go b/shopify/responses.go index e1f1a4f..754170a 100644 --- a/shopify/responses.go +++ b/shopify/responses.go @@ -19,3 +19,13 @@ type TransactionsResponse struct { type CountResponse struct { Count int `json:"count"` } + +//ProductsResponse is a response from products +type ProductsResponse struct { + Products []Product `json:"products"` +} + +//ProductResponse is a response for a product +type ProductResponse struct { + Product Product `json:"product"` +} diff --git a/shopify/shopify.go b/shopify/shopify.go index c9bb15f..489a2a5 100755 --- a/shopify/shopify.go +++ b/shopify/shopify.go @@ -8,8 +8,7 @@ import ( "github.com/parnurzeal/gorequest" ) -// Shopify store struct which we use -// to wrap our request operations. +// Shopify store struct which we use to wrap our request operations. type Shopify struct { // Store domain-name store string @@ -23,18 +22,13 @@ const ( domain = ".myshopify.com/admin" ) -// New Creates a New Shopify Store API object with the -// store, apiKey and pass of your store. -// Usage: -// shopify.New("mystore", "XXX","YYY") +// New Creates a New Shopify Store API object with the store, apiKey and pass of your store. +// Usage: shopify.New("mystore", "XXX","YYY") func New(store, apiKey, pass string) Shopify { - shop := Shopify{store: store, apiKey: apiKey, pass: pass} - // fmt.Println("[New] Creating Shopify client with: ", store, apiKey, pass) - return shop + return Shopify{store: store, apiKey: apiKey, pass: pass} } -// Creates target URL for making a Shopify Request -// to a given endpoint +// Creates target URL for making a Shopify Request to a given endpoint func (shopify *Shopify) createTargetURL(endpoint string) string { result := fmt.Sprintf("https://%s:%s@%s%s/%s.json", shopify.apiKey, shopify.pass, shopify.store, domain, endpoint) return result @@ -52,19 +46,14 @@ func getJSONBytesFromMap(data map[string]interface{}) ([]byte, error) { return jsonData, nil } -// Request Creates a new Request to Shopify and returns -// the response as a map[string]interface{}. +// Request Creates a new Request to Shopify and returns the response as a map[string]interface{}. // method: GET/POST/PUT - string // url: target endpoint like "products" - string // data: content to be sent with the request // Usage: shopify.request("GET","products",nil) func (shopify *Shopify) Request(method, endpoint string, data map[string]interface{}) ([]byte, []error) { - // fmt.Println("[request] Arguments: ", method, endpoint, data) jsonData, _ := getJSONBytesFromMap(data) - // fmt.Println("[request] data: ", string(jsonData)) - targetURL := shopify.createTargetURL(endpoint) - // fmt.Println("[request] targetURL: ", targetURL) request := gorequest.New() request.Get(targetURL) @@ -78,8 +67,7 @@ func (shopify *Shopify) Request(method, endpoint string, data map[string]interfa return []byte(body), errs } -// Get Makes a GET request to shopify with the -// given endpoint. +// Get Makes a GET request to shopify with the given endpoint. // Usage: // shopify.Get("products/5.json") // shopify.Get("products/5/variants.json") @@ -91,12 +79,9 @@ func (shopify *Shopify) Get(endpoint string) ([]byte, []error) { return []byte(body), errs } -// Post Makes a POST request to shopify with the -// given endpoint and data. -// Usage: -// shopify.Post("products", map[string]interface{} = product data map) +// Post Makes a POST request to shopify with the given endpoint and data. +// Usage: shopify.Post("products", map[string]interface{} = product data map) func (shopify *Shopify) Post(endpoint string, data map[string]interface{}) ([]byte, []error) { - targetURL := shopify.createTargetURL(endpoint) jsonData, err := getJSONBytesFromMap(data) if err != nil { @@ -113,12 +98,9 @@ func (shopify *Shopify) Post(endpoint string, data map[string]interface{}) ([]by return []byte(body), errs } -// Put Makes a PUT request to shopify with the -// given endpoint and data. -// Usage: -// shopify.Put("products", map[string]interface{} = product data map) +// Put Makes a PUT request to shopify with the given endpoint and data. +// Usage: shopify.Put("products", map[string]interface{} = product data map) func (shopify *Shopify) Put(endpoint string, data map[string]interface{}) ([]byte, []error) { - targetURL := shopify.createTargetURL(endpoint) jsonData, err := getJSONBytesFromMap(data) if err != nil { @@ -135,12 +117,9 @@ func (shopify *Shopify) Put(endpoint string, data map[string]interface{}) ([]byt return []byte(body), errs } -// Delete Makes a DELETE request to shopify with the -// given endpoint. -// Usage: -// shopify.Delete("products/5.json") +// Delete Makes a DELETE request to shopify with the given endpoint. +// Usage: shopify.Delete("products/5.json") func (shopify *Shopify) Delete(endpoint string) ([]byte, []error) { - targetURL := shopify.createTargetURL(endpoint) request := gorequest.New() diff --git a/shopify/wrapper.go b/shopify/wrapper.go index 159e8b3..bafe2d7 100644 --- a/shopify/wrapper.go +++ b/shopify/wrapper.go @@ -79,3 +79,31 @@ func (shopify *Shopify) GetOrdersCount() (int, []error) { } return ordersCount.Count, nil } + +//GetProducts returns all the orders +func (shopify *Shopify) GetProducts() ([]Product, []error) { + response, errors := shopify.Get("products") + if len(errors) > 0 { + return nil, errors + } + + var products ProductsResponse + if err := json.Unmarshal(response, &products); err != nil { + return nil, []error{err} + } + return products.Products, nil +} + +//GetProduct returns all the orders +func (shopify *Shopify) GetProduct(productID int64) (*Product, []error) { + response, errors := shopify.Get(fmt.Sprintf("products/%v", productID)) + if len(errors) > 0 { + return nil, errors + } + + var product ProductResponse + if err := json.Unmarshal(response, &product); err != nil { + return nil, []error{err} + } + return &product.Product, nil +} From abc820a71bf11fc371de8fa0f8a23b1825072ace Mon Sep 17 00:00:00 2001 From: alecha Date: Fri, 29 Jan 2016 19:22:46 +0100 Subject: [PATCH 3/9] updated README and added product variants and images wrappers --- README.md | 34 ++++++++++++++++++++++------------ shopify/model.go | 18 ++++++++++++------ shopify/responses.go | 10 ++++++++++ shopify/wrapper.go | 28 ++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 0f27829..b94f9da 100644 --- a/README.md +++ b/README.md @@ -21,45 +21,55 @@ How-to-use import "fmt" import "github.com/rapito/go-shopify/shopify" ... - + shop := shopify.New(storeDomain,apiKey,pass) result, _ := shop.Get("products") - + fmt.Println(string(result)) ``` - Check out the *examples* folder for simple usage. - Read some of the tests at *shopify_test.go* for complete CRUD examples. +- There are some built-in wrappers than you can use, you are welcome to add new ones: + - `GetOrders()` + - `GetOrder(orderID)` + - `GetOrderTransactions(orderID)` + - `GetOrderTransactionsCount(orderID)` + - `GetOrdersCount()` + - `GetProducts()` + - `GetProduct(productID)` + - `GetProductImages(productID)` + - `GetProductVariants(productID)` + Contribution ------------ - + - You may fork this library and modify it as you please. - You can make a pull request and I will be happy to check it out and merge it. - - If you find a bug, create an issue and I will do my best to fix it (someday). + - If you find a bug, create an issue and I will do my best to fix it (someday). Original Work ------------- -While I was looking for something cool to do with this new language im learning -(Go, obviously), I ran into [hammond-bones'](https://github.com/hammond-bones/) **go-shopify** -library. Which inspired me to start creating this one. +While I was looking for something cool to do with this new language im learning +(Go, obviously), I ran into [hammond-bones'](https://github.com/hammond-bones/) **go-shopify** +library. Which inspired me to start creating this one. - Fork it at: [go-shopify](https://github.com/hammond-bones/go-shopify) Links ----- -While I was on my *go-trip* to create this api, I found some awesome libs which made +While I was on my *go-trip* to create this api, I found some awesome libs which made my life easier. Check them out, hopefully they'll do the same for you: - + - http://github.com/parnurzeal/gorequest - http://github.com/bmizerany/assert - http://github.com/avelino/awesome-go - + Other APIs ---------- - + - http://github.com/rapito/go-spotify - diff --git a/shopify/model.go b/shopify/model.go index 47df996..b4fb1bb 100644 --- a/shopify/model.go +++ b/shopify/model.go @@ -82,11 +82,6 @@ type Fulfillment struct { UpdatedAt time.Time `json:"updated_at"` } -//Image is a product's image -type Image struct { - Src string `json:"src"` -} - //LineItem is an order line item type LineItem struct { FulfillableQuantity int `json:"fulfillable_quantity"` @@ -174,7 +169,7 @@ type Product struct { CreatedAt time.Time `json:"created_at"` Handle string `json:"handle"` ID int64 `json:"id"` - Images []Image `json:"images"` + Images []ProductImage `json:"images"` Options []map[string]interface{} `json:"options"` ProductType string `json:"product_type"` PublishedAt *time.Time `json:"published_at"` @@ -189,6 +184,17 @@ type Product struct { Vendor string `json:"vendor"` } +//ProductImage is a product's image +type ProductImage struct { + CreatedAt time.Time `json:"created_at"` + ID int64 `json:"id"` + Position int `json:"position"` + ProductID int64 `json:"product_id"` + VariantIDs []int64 `json:"variant_ids"` + Src string `json:"src"` + UpdatedAt time.Time `json:"id"` +} + //Refund is a refund type Refund struct { CreatedAt time.Time `json:"created_at"` diff --git a/shopify/responses.go b/shopify/responses.go index 754170a..3aa3060 100644 --- a/shopify/responses.go +++ b/shopify/responses.go @@ -29,3 +29,13 @@ type ProductsResponse struct { type ProductResponse struct { Product Product `json:"product"` } + +//ImagesResponse is a response for product images +type ImagesResponse struct { + Images []ProductImage `json:"images"` +} + +//VariantsResponse is a response for product images +type VariantsResponse struct { + Variants []Variant `json:"variants"` +} diff --git a/shopify/wrapper.go b/shopify/wrapper.go index bafe2d7..7a1dffa 100644 --- a/shopify/wrapper.go +++ b/shopify/wrapper.go @@ -107,3 +107,31 @@ func (shopify *Shopify) GetProduct(productID int64) (*Product, []error) { } return &product.Product, nil } + +//GetProductImages returns all the orders +func (shopify *Shopify) GetProductImages(productID int64) ([]ProductImage, []error) { + response, errors := shopify.Get(fmt.Sprintf("products/%v/images", productID)) + if len(errors) > 0 { + return nil, errors + } + + var images ImagesResponse + if err := json.Unmarshal(response, &images); err != nil { + return nil, []error{err} + } + return images.Images, nil +} + +//GetProductVariants returns all the product variants +func (shopify *Shopify) GetProductVariants(productID int64) ([]Variant, []error) { + response, errors := shopify.Get(fmt.Sprintf("products/%v/variants", productID)) + if len(errors) > 0 { + return nil, errors + } + + var variants VariantsResponse + if err := json.Unmarshal(response, &variants); err != nil { + return nil, []error{err} + } + return variants.Variants, nil +} From fb1a50f41e6e33eadd874a1e8b4ed5be97d76e0c Mon Sep 17 00:00:00 2001 From: alecha Date: Mon, 1 Feb 2016 11:08:57 +0100 Subject: [PATCH 4/9] updated wrapper for using a common method, README cleaned --- README.md | 46 ++++++++---------- shopify/shopify_test.go | 29 ++++------- shopify/wrapper.go | 103 ++++++++++++++-------------------------- shopify/wrapper_test.go | 8 ++++ 4 files changed, 75 insertions(+), 111 deletions(-) create mode 100644 shopify/wrapper_test.go diff --git a/README.md b/README.md index b94f9da..ad2f9da 100644 --- a/README.md +++ b/README.md @@ -11,21 +11,33 @@ Installation go get github.com/rapito/go-shopify ``` -How-to-use +Dependencies +------------ +``` +go get github.com/parnurzeal/gorequest +``` + +Test dependencies +------------ +``` +go get github.com/bitly/go-simplejson +go get github.com/bmizerany/assert +``` + +Use it ---------- -- Get Requests +- GET Requests -``` - import "fmt" - import "github.com/rapito/go-shopify/shopify" - ... +```go +import "fmt" +import "github.com/rapito/go-shopify/shopify" - shop := shopify.New(storeDomain,apiKey,pass) - result, _ := shop.Get("products") +shop := shopify.New(storeDomain,apiKey,pass) +result, _ := shop.Get("products") - fmt.Println(string(result)) +fmt.Println(string(result)) ``` - Check out the *examples* folder for simple usage. @@ -57,19 +69,3 @@ While I was looking for something cool to do with this new language im learning library. Which inspired me to start creating this one. - Fork it at: [go-shopify](https://github.com/hammond-bones/go-shopify) - -Links ------ - -While I was on my *go-trip* to create this api, I found some awesome libs which made -my life easier. -Check them out, hopefully they'll do the same for you: - - - http://github.com/parnurzeal/gorequest - - http://github.com/bmizerany/assert - - http://github.com/avelino/awesome-go - - Other APIs - ---------- - - - http://github.com/rapito/go-spotify diff --git a/shopify/shopify_test.go b/shopify/shopify_test.go index 0f87cd0..b1df321 100755 --- a/shopify/shopify_test.go +++ b/shopify/shopify_test.go @@ -2,30 +2,31 @@ package shopify // Import Testing frameworks needed import ( - "testing" + "encoding/json" "fmt" - "github.com/bmizerany/assert" + "os" + "testing" + simplejson "github.com/bitly/go-simplejson" - "encoding/json" + "github.com/bmizerany/assert" ) // Create out store variables for easy access const ( - store = "your-store-domain-name-here" - apiKey = "your-api-key-here" - pass = "your-secret-pass-here" + store = os.Getenv("SHOPIFY_STORE_TEST") + apiKey = os.Getenv("SHOPIFY_API_KEY_TEST") + pass = os.Getenv("SHOPIFY_PASSWORD_TEST") ) // We declare out shop here just to reuse // it later on. var shop = New(store, apiKey, pass) -var objIdToDelete int64 +var objIDToDelete int64 // Should create a new Store. func TestNew(t *testing.T) { - if shop.store != store || shop.apiKey != apiKey || shop.pass != pass { - t.Errorf("Error creating client, was suppposed to have store:$v apiKey:$v pass:$v", store, apiKey, pass) + t.Errorf("Error creating client, was suppposed to have store:%v apiKey:%v pass:%v", store, apiKey, pass) } } @@ -48,28 +49,22 @@ func TestGet(t *testing.T) { fmt.Println(js) fmt.Println(err) - assert.T(t, errors == nil) assert.T(t, products != nil) - // product := products["products"].([]interface{}) // fmt.Println(product[0]["vendor"]) // fmt.Println(products) - - } // Should make a new Post Request func TestPost(t *testing.T) { - str := ` {"product": {"title": "MyProduct","body_html": "Good snowboard!","vendor": "Burton","product_type": "Snowboard","variants": [ { "option1": "First", "price": "10.00", "sku": 123 }, { "option1": "Second", "price": "20.00", "sku": "123" }]}} ` var data map[string]interface{} json.Unmarshal([]byte(str), &data) result, errors := shop.Post("products", data) - js, err := simplejson.NewJson(result) fmt.Println(js) @@ -111,7 +106,6 @@ func TestPut(t *testing.T) { // Should make a new Delete Request func TestDelete(t *testing.T) { - endpoint := fmt.Sprintf("products/%v", objIdToDelete) result, error := shop.Delete(endpoint) @@ -126,6 +120,3 @@ func TestCreateTargetURL(t *testing.T) { test := fmt.Sprintf("https://%s:%s@%s%s/%s.json", apiKey, pass, store, domain, endpoint) assert.Equal(t, result, test) } - - - diff --git a/shopify/wrapper.go b/shopify/wrapper.go index 7a1dffa..f95a75d 100644 --- a/shopify/wrapper.go +++ b/shopify/wrapper.go @@ -3,135 +3,104 @@ package shopify import ( "encoding/json" "fmt" - "log" ) //GetOrders returns all the orders func (shopify *Shopify) GetOrders() ([]Order, []error) { - response, errors := shopify.Get("orders") - if len(errors) > 0 { - return nil, errors - } - var orders OrdersResponse - if err := json.Unmarshal(response, &orders); err != nil { - return nil, []error{err} + response, errors := shopify.Get("orders") + if err := unmarshal(response, errors, &orders); len(err) > 0 { + return nil, err } return orders.Orders, nil } //GetOrder returns a order given its id func (shopify *Shopify) GetOrder(orderID int64) (*Order, []error) { - response, errors := shopify.Get(fmt.Sprintf("orders/%v", orderID)) - if len(errors) > 0 { - return nil, errors - } - var orderResponse OrderResponse - if err := json.Unmarshal(response, &orderResponse); err != nil { - return nil, []error{err} + response, errors := shopify.Get(fmt.Sprintf("orders/%v", orderID)) + if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { + return nil, err } return &orderResponse.Order, nil } //GetOrderTransactions returns the order's transactions func (shopify *Shopify) GetOrderTransactions(orderID int64) ([]Transaction, []error) { - response, errors := shopify.Get(fmt.Sprintf("orders/%v/transactions", orderID)) - if len(errors) > 0 { - return nil, errors - } - - log.Print(string(response)) - var transactionsResponse TransactionsResponse - if err := json.Unmarshal(response, &transactionsResponse); err != nil { - return nil, []error{err} + response, errors := shopify.Get(fmt.Sprintf("orders/%v/transactions", orderID)) + if err := unmarshal(response, errors, &transactionsResponse); len(err) > 0 { + return nil, err } return transactionsResponse.Transactions, nil } //GetOrderTransactionsCount returns the order's transactions count func (shopify *Shopify) GetOrderTransactionsCount(orderID int64) (int, []error) { - response, errors := shopify.Get(fmt.Sprintf("orders/%v/transactions/count", orderID)) - if len(errors) > 0 { - return 0, errors - } - - log.Print(string(response)) - var count CountResponse - if err := json.Unmarshal(response, &count); err != nil { - return 0, []error{err} + response, errors := shopify.Get(fmt.Sprintf("orders/%v/transactions/count", orderID)) + if err := unmarshal(response, errors, &count); len(err) > 0 { + return 0, err } return count.Count, nil } //GetOrdersCount returns all the products func (shopify *Shopify) GetOrdersCount() (int, []error) { - response, errors := shopify.Get("orders/count") - if len(errors) > 0 { - return 0, errors - } - var ordersCount CountResponse - if err := json.Unmarshal(response, &ordersCount); err != nil { - return 0, []error{err} + response, errors := shopify.Get("orders/count") + if err := unmarshal(response, errors, &ordersCount); len(err) > 0 { + return 0, err } return ordersCount.Count, nil } //GetProducts returns all the orders func (shopify *Shopify) GetProducts() ([]Product, []error) { - response, errors := shopify.Get("products") - if len(errors) > 0 { - return nil, errors - } - var products ProductsResponse - if err := json.Unmarshal(response, &products); err != nil { - return nil, []error{err} + response, errors := shopify.Get("products") + if err := unmarshal(response, errors, &products); len(err) > 0 { + return nil, err } return products.Products, nil } //GetProduct returns all the orders func (shopify *Shopify) GetProduct(productID int64) (*Product, []error) { - response, errors := shopify.Get(fmt.Sprintf("products/%v", productID)) - if len(errors) > 0 { - return nil, errors - } - var product ProductResponse - if err := json.Unmarshal(response, &product); err != nil { - return nil, []error{err} + response, errors := shopify.Get(fmt.Sprintf("products/%v", productID)) + if err := unmarshal(response, errors, &product); len(err) > 0 { + return nil, err } return &product.Product, nil } //GetProductImages returns all the orders func (shopify *Shopify) GetProductImages(productID int64) ([]ProductImage, []error) { - response, errors := shopify.Get(fmt.Sprintf("products/%v/images", productID)) - if len(errors) > 0 { - return nil, errors - } - var images ImagesResponse - if err := json.Unmarshal(response, &images); err != nil { - return nil, []error{err} + response, errors := shopify.Get(fmt.Sprintf("products/%v/images", productID)) + if err := unmarshal(response, errors, &images); len(err) > 0 { + return nil, err } return images.Images, nil } //GetProductVariants returns all the product variants func (shopify *Shopify) GetProductVariants(productID int64) ([]Variant, []error) { + var variants VariantsResponse response, errors := shopify.Get(fmt.Sprintf("products/%v/variants", productID)) - if len(errors) > 0 { - return nil, errors + if err := unmarshal(response, errors, &variants); len(err) > 0 { + return nil, err } + return variants.Variants, nil +} - var variants VariantsResponse - if err := json.Unmarshal(response, &variants); err != nil { - return nil, []error{err} +func unmarshal(responseData []byte, responseErrors []error, output interface{}) []error { + if len(responseErrors) > 0 { + return responseErrors } - return variants.Variants, nil + if err := json.Unmarshal(responseData, output); err != nil { + return []error{err} + } + return nil } diff --git a/shopify/wrapper_test.go b/shopify/wrapper_test.go new file mode 100644 index 0000000..1fa54b6 --- /dev/null +++ b/shopify/wrapper_test.go @@ -0,0 +1,8 @@ +package shopify + +var shop *Shopify + +func init() { + //init shop instance + shop = New("", "", "") +} From dcaf80e520d75991420bb9a68c8f39895c0ede5b Mon Sep 17 00:00:00 2001 From: alecha Date: Mon, 1 Feb 2016 22:41:55 +0100 Subject: [PATCH 5/9] removed empty test file --- shopify/shopify_test.go | 8 ++++---- shopify/wrapper_test.go | 8 -------- 2 files changed, 4 insertions(+), 12 deletions(-) delete mode 100644 shopify/wrapper_test.go diff --git a/shopify/shopify_test.go b/shopify/shopify_test.go index b1df321..78fcd69 100755 --- a/shopify/shopify_test.go +++ b/shopify/shopify_test.go @@ -12,7 +12,7 @@ import ( ) // Create out store variables for easy access -const ( +var ( store = os.Getenv("SHOPIFY_STORE_TEST") apiKey = os.Getenv("SHOPIFY_API_KEY_TEST") pass = os.Getenv("SHOPIFY_PASSWORD_TEST") @@ -78,7 +78,7 @@ func TestPost(t *testing.T) { assert.T(t, title == "MyProduct") id, _ := js.Get("product").Get("id").Int64() - objIdToDelete = id + objIDToDelete = id } // Should make a new Put Request @@ -88,7 +88,7 @@ func TestPut(t *testing.T) { var data map[string]interface{} json.Unmarshal([]byte(str), &data) - endpoint := fmt.Sprintf("products/%v", objIdToDelete) + endpoint := fmt.Sprintf("products/%v", objIDToDelete) result, errors := shop.Put(endpoint, data) js, err := simplejson.NewJson(result) @@ -106,7 +106,7 @@ func TestPut(t *testing.T) { // Should make a new Delete Request func TestDelete(t *testing.T) { - endpoint := fmt.Sprintf("products/%v", objIdToDelete) + endpoint := fmt.Sprintf("products/%v", objIDToDelete) result, error := shop.Delete(endpoint) assert.T(t, error == nil, "should be null") diff --git a/shopify/wrapper_test.go b/shopify/wrapper_test.go deleted file mode 100644 index 1fa54b6..0000000 --- a/shopify/wrapper_test.go +++ /dev/null @@ -1,8 +0,0 @@ -package shopify - -var shop *Shopify - -func init() { - //init shop instance - shop = New("", "", "") -} From 421e5aa31082cf60d4d74d7b16a8cc5160c60130 Mon Sep 17 00:00:00 2001 From: alecha Date: Mon, 1 Feb 2016 23:07:50 +0100 Subject: [PATCH 6/9] added GetWithParameter method --- shopify/model.go | 13 ++++++ shopify/order.go | 53 +++++++++++++++++++++++ shopify/product.go | 43 ++++++++++++++++++ shopify/shopify.go | 49 +++++++++++---------- shopify/utils.go | 22 ++++++++++ shopify/wrapper.go | 106 --------------------------------------------- 6 files changed, 157 insertions(+), 129 deletions(-) create mode 100644 shopify/order.go create mode 100644 shopify/product.go create mode 100644 shopify/utils.go delete mode 100644 shopify/wrapper.go diff --git a/shopify/model.go b/shopify/model.go index b4fb1bb..f6ab789 100644 --- a/shopify/model.go +++ b/shopify/model.go @@ -2,6 +2,19 @@ package shopify import "time" +//ApplicationCharge is an application charge +type ApplicationCharge struct { + ConfirmationURL string `json:"confirmation_url"` + CreatedAt time.Time `json:"created_at"` + ID int64 `json:"id"` + Name string `json:"name"` + Price string `json:"price"` + ReturnURL string `json:"return_url"` + Status string `json:"status"` + Test string `json:"test"` + UpdatedAt time.Time `json:"updated_at"` +} + //BillingAddress is a billing address type BillingAddress struct { Address1 string `json:"address1"` diff --git a/shopify/order.go b/shopify/order.go new file mode 100644 index 0000000..185eda9 --- /dev/null +++ b/shopify/order.go @@ -0,0 +1,53 @@ +package shopify + +import "fmt" + +//GetOrders returns all the orders +func (shopify *Shopify) GetOrders(parameters map[string]string) ([]Order, []error) { + var orders OrdersResponse + response, errors := shopify.GetWithParameters("orders", parameters) + if err := unmarshal(response, errors, &orders); len(err) > 0 { + return nil, err + } + return orders.Orders, nil +} + +//GetOrder returns a order given its id +func (shopify *Shopify) GetOrder(orderID int64) (*Order, []error) { + var orderResponse OrderResponse + response, errors := shopify.Get(fmt.Sprintf("orders/%v", orderID)) + if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { + return nil, err + } + return &orderResponse.Order, nil +} + +//GetOrderTransactions returns the order's transactions +func (shopify *Shopify) GetOrderTransactions(orderID int64) ([]Transaction, []error) { + var transactionsResponse TransactionsResponse + response, errors := shopify.Get(fmt.Sprintf("orders/%v/transactions", orderID)) + if err := unmarshal(response, errors, &transactionsResponse); len(err) > 0 { + return nil, err + } + return transactionsResponse.Transactions, nil +} + +//GetOrderTransactionsCount returns the order's transactions count +func (shopify *Shopify) GetOrderTransactionsCount(orderID int64) (int, []error) { + var count CountResponse + response, errors := shopify.Get(fmt.Sprintf("orders/%v/transactions/count", orderID)) + if err := unmarshal(response, errors, &count); len(err) > 0 { + return 0, err + } + return count.Count, nil +} + +//GetOrdersCount returns all the products +func (shopify *Shopify) GetOrdersCount() (int, []error) { + var ordersCount CountResponse + response, errors := shopify.Get("orders/count") + if err := unmarshal(response, errors, &ordersCount); len(err) > 0 { + return 0, err + } + return ordersCount.Count, nil +} diff --git a/shopify/product.go b/shopify/product.go new file mode 100644 index 0000000..f155a0e --- /dev/null +++ b/shopify/product.go @@ -0,0 +1,43 @@ +package shopify + +import "fmt" + +//GetProducts returns all the orders +func (shopify *Shopify) GetProducts() ([]Product, []error) { + var products ProductsResponse + response, errors := shopify.Get("products") + if err := unmarshal(response, errors, &products); len(err) > 0 { + return nil, err + } + return products.Products, nil +} + +//GetProduct returns all the orders +func (shopify *Shopify) GetProduct(productID int64) (*Product, []error) { + var product ProductResponse + response, errors := shopify.Get(fmt.Sprintf("products/%v", productID)) + if err := unmarshal(response, errors, &product); len(err) > 0 { + return nil, err + } + return &product.Product, nil +} + +//GetProductImages returns all the orders +func (shopify *Shopify) GetProductImages(productID int64) ([]ProductImage, []error) { + var images ImagesResponse + response, errors := shopify.Get(fmt.Sprintf("products/%v/images", productID)) + if err := unmarshal(response, errors, &images); len(err) > 0 { + return nil, err + } + return images.Images, nil +} + +//GetProductVariants returns all the product variants +func (shopify *Shopify) GetProductVariants(productID int64) ([]Variant, []error) { + var variants VariantsResponse + response, errors := shopify.Get(fmt.Sprintf("products/%v/variants", productID)) + if err := unmarshal(response, errors, &variants); len(err) > 0 { + return nil, err + } + return variants.Variants, nil +} diff --git a/shopify/shopify.go b/shopify/shopify.go index 489a2a5..a40a656 100755 --- a/shopify/shopify.go +++ b/shopify/shopify.go @@ -2,7 +2,6 @@ package shopify import ( - "encoding/json" "fmt" "github.com/parnurzeal/gorequest" @@ -28,30 +27,12 @@ func New(store, apiKey, pass string) Shopify { return Shopify{store: store, apiKey: apiKey, pass: pass} } -// Creates target URL for making a Shopify Request to a given endpoint -func (shopify *Shopify) createTargetURL(endpoint string) string { - result := fmt.Sprintf("https://%s:%s@%s%s/%s.json", shopify.apiKey, shopify.pass, shopify.store, domain, endpoint) - return result -} - -// getJSONBytesFromMap Extracts Json Bytes from map[string]interface -func getJSONBytesFromMap(data map[string]interface{}) ([]byte, error) { - jsonData, err := json.Marshal(data) - if err != nil { - fmt.Println("Invalid data object, can't parse to json:") - fmt.Println("Error:", err) - fmt.Println("Data:", data) - return nil, err - } - return jsonData, nil -} - // Request Creates a new Request to Shopify and returns the response as a map[string]interface{}. // method: GET/POST/PUT - string // url: target endpoint like "products" - string // data: content to be sent with the request // Usage: shopify.request("GET","products",nil) -func (shopify *Shopify) Request(method, endpoint string, data map[string]interface{}) ([]byte, []error) { +func (shopify *Shopify) Request(method, endpoint string, data interface{}) ([]byte, []error) { jsonData, _ := getJSONBytesFromMap(data) targetURL := shopify.createTargetURL(endpoint) @@ -72,7 +53,12 @@ func (shopify *Shopify) Request(method, endpoint string, data map[string]interfa // shopify.Get("products/5.json") // shopify.Get("products/5/variants.json") func (shopify *Shopify) Get(endpoint string) ([]byte, []error) { - targetURL := shopify.createTargetURL(endpoint) + return shopify.GetWithParameters(endpoint, nil) +} + +// GetWithParameters Makes a GET request to shopify with the given endpoint and given parameters +func (shopify *Shopify) GetWithParameters(endpoint string, parameters map[string]string) ([]byte, []error) { + targetURL := shopify.createTargetURLWithParameters(endpoint, parameters) request := gorequest.New() _, body, errs := request.Get(targetURL).End() @@ -81,7 +67,7 @@ func (shopify *Shopify) Get(endpoint string) ([]byte, []error) { // Post Makes a POST request to shopify with the given endpoint and data. // Usage: shopify.Post("products", map[string]interface{} = product data map) -func (shopify *Shopify) Post(endpoint string, data map[string]interface{}) ([]byte, []error) { +func (shopify *Shopify) Post(endpoint string, data interface{}) ([]byte, []error) { targetURL := shopify.createTargetURL(endpoint) jsonData, err := getJSONBytesFromMap(data) if err != nil { @@ -100,7 +86,7 @@ func (shopify *Shopify) Post(endpoint string, data map[string]interface{}) ([]by // Put Makes a PUT request to shopify with the given endpoint and data. // Usage: shopify.Put("products", map[string]interface{} = product data map) -func (shopify *Shopify) Put(endpoint string, data map[string]interface{}) ([]byte, []error) { +func (shopify *Shopify) Put(endpoint string, data interface{}) ([]byte, []error) { targetURL := shopify.createTargetURL(endpoint) jsonData, err := getJSONBytesFromMap(data) if err != nil { @@ -127,3 +113,20 @@ func (shopify *Shopify) Delete(endpoint string) ([]byte, []error) { return []byte(body), errs } + +// Creates target URL for making a Shopify Request to a given endpoint +func (shopify *Shopify) createTargetURL(endpoint string) string { + return shopify.createTargetURLWithParameters(endpoint, nil) +} + +// Creates target URL for making a Shopify Request to a given endpoint with the given parameters +func (shopify *Shopify) createTargetURLWithParameters(endpoint string, parameters map[string]string) string { + var parametersString = "" + if parameters != nil && len(parameters) > 0 { + parametersString = "?" + for k := range parameters { + parametersString = fmt.Sprintf("%v%v=%v&", parametersString, k, parameters[k]) + } + } + return fmt.Sprintf("https://%s:%s@%s%s/%s.json%s", shopify.apiKey, shopify.pass, shopify.store, domain, endpoint, parametersString) +} diff --git a/shopify/utils.go b/shopify/utils.go new file mode 100644 index 0000000..aa34c88 --- /dev/null +++ b/shopify/utils.go @@ -0,0 +1,22 @@ +package shopify + +import "encoding/json" + +// getJSONBytesFromMap Extracts Json Bytes from map[string]interface +func getJSONBytesFromMap(data interface{}) ([]byte, error) { + jsonData, err := json.Marshal(data) + if err != nil { + return nil, err + } + return jsonData, nil +} + +func unmarshal(responseData []byte, responseErrors []error, output interface{}) []error { + if len(responseErrors) > 0 { + return responseErrors + } + if err := json.Unmarshal(responseData, output); err != nil { + return []error{err} + } + return nil +} diff --git a/shopify/wrapper.go b/shopify/wrapper.go deleted file mode 100644 index f95a75d..0000000 --- a/shopify/wrapper.go +++ /dev/null @@ -1,106 +0,0 @@ -package shopify - -import ( - "encoding/json" - "fmt" -) - -//GetOrders returns all the orders -func (shopify *Shopify) GetOrders() ([]Order, []error) { - var orders OrdersResponse - response, errors := shopify.Get("orders") - if err := unmarshal(response, errors, &orders); len(err) > 0 { - return nil, err - } - return orders.Orders, nil -} - -//GetOrder returns a order given its id -func (shopify *Shopify) GetOrder(orderID int64) (*Order, []error) { - var orderResponse OrderResponse - response, errors := shopify.Get(fmt.Sprintf("orders/%v", orderID)) - if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { - return nil, err - } - return &orderResponse.Order, nil -} - -//GetOrderTransactions returns the order's transactions -func (shopify *Shopify) GetOrderTransactions(orderID int64) ([]Transaction, []error) { - var transactionsResponse TransactionsResponse - response, errors := shopify.Get(fmt.Sprintf("orders/%v/transactions", orderID)) - if err := unmarshal(response, errors, &transactionsResponse); len(err) > 0 { - return nil, err - } - return transactionsResponse.Transactions, nil -} - -//GetOrderTransactionsCount returns the order's transactions count -func (shopify *Shopify) GetOrderTransactionsCount(orderID int64) (int, []error) { - var count CountResponse - response, errors := shopify.Get(fmt.Sprintf("orders/%v/transactions/count", orderID)) - if err := unmarshal(response, errors, &count); len(err) > 0 { - return 0, err - } - return count.Count, nil -} - -//GetOrdersCount returns all the products -func (shopify *Shopify) GetOrdersCount() (int, []error) { - var ordersCount CountResponse - response, errors := shopify.Get("orders/count") - if err := unmarshal(response, errors, &ordersCount); len(err) > 0 { - return 0, err - } - return ordersCount.Count, nil -} - -//GetProducts returns all the orders -func (shopify *Shopify) GetProducts() ([]Product, []error) { - var products ProductsResponse - response, errors := shopify.Get("products") - if err := unmarshal(response, errors, &products); len(err) > 0 { - return nil, err - } - return products.Products, nil -} - -//GetProduct returns all the orders -func (shopify *Shopify) GetProduct(productID int64) (*Product, []error) { - var product ProductResponse - response, errors := shopify.Get(fmt.Sprintf("products/%v", productID)) - if err := unmarshal(response, errors, &product); len(err) > 0 { - return nil, err - } - return &product.Product, nil -} - -//GetProductImages returns all the orders -func (shopify *Shopify) GetProductImages(productID int64) ([]ProductImage, []error) { - var images ImagesResponse - response, errors := shopify.Get(fmt.Sprintf("products/%v/images", productID)) - if err := unmarshal(response, errors, &images); len(err) > 0 { - return nil, err - } - return images.Images, nil -} - -//GetProductVariants returns all the product variants -func (shopify *Shopify) GetProductVariants(productID int64) ([]Variant, []error) { - var variants VariantsResponse - response, errors := shopify.Get(fmt.Sprintf("products/%v/variants", productID)) - if err := unmarshal(response, errors, &variants); len(err) > 0 { - return nil, err - } - return variants.Variants, nil -} - -func unmarshal(responseData []byte, responseErrors []error, output interface{}) []error { - if len(responseErrors) > 0 { - return responseErrors - } - if err := json.Unmarshal(responseData, output); err != nil { - return []error{err} - } - return nil -} From 8e3bc2b937a7637542d08ce82bf6668a193af834 Mon Sep 17 00:00:00 2001 From: alecha Date: Mon, 1 Feb 2016 23:21:00 +0100 Subject: [PATCH 7/9] completed order's wrappers --- shopify/model.go | 83 +++++++++++++++++++++++++----------------------- shopify/order.go | 63 ++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 40 deletions(-) diff --git a/shopify/model.go b/shopify/model.go index f6ab789..a40de1f 100644 --- a/shopify/model.go +++ b/shopify/model.go @@ -125,46 +125,49 @@ type NoteAttribute struct { //Order is a product type Order struct { - BillingAddress BillingAddress `json:"billing_address"` - BrowserIP string `json:"browser_ip"` - BuyerAcceptsMarketing bool `json:"buyer_accepts_marketing"` - CancelReason *string `json:"cancel_reason"` - CancelledAt *time.Time `json:"cancelled_at"` - ClientDetails ClientDetails `json:"client_details"` - ClosedAt *time.Time `json:"closed_at"` - CreatedAt time.Time `json:"created_at"` - Currency string `json:"currency"` - Customer *Customer `json:"customer"` - DiscountCodes []DiscountCode `json:"discount_codes"` - Email string `json:"email"` - FinancialStatus string `json:"financial_status"` - Fulfillments []Fulfillment `json:"fulfillments"` - FulfillmentStatus string `json:"fulfillment_status"` - Tags string `json:"tags"` - ID int64 `json:"id"` - LandingSite string `json:"landing_site"` - LineItems []LineItem `json:"line_items"` - Name string `json:"name"` - Note *string `json:"note"` - NoteAttributes []NoteAttribute `json:"note_attributes"` - Number int64 `json:"number"` - OrderNumber int64 `json:"order_number"` - PaymentGatewayNames []string `json:"payment_gateway_names"` - ProcessedAt time.Time `json:"processed_at"` - ProcessingMethod string `json:"processing_method"` - ReferringSite string `json:"referring_site"` - Refunds []Refund `json:"refunds"` - ShippingAddress ShippingAddress `json:"shipping_address"` - ShippingLines []ShippingLine `json:"shipping_lines"` - SourceName string `json:"source_name"` - SubtotalPrice string `json:"subtotal_price"` - TaxLines []TaxLine `json:"tax_lines"` - TaxesIncluded bool `json:"taxes_included"` - TotalDiscounts string `json:"total_discounts"` - TotalPrice string `json:"total_price"` - TotalTax string `json:"total_tax"` - TotalWeight float64 `json:"total_weight"` - UpdatedAt time.Time `json:"updatedAt"` + BillingAddress *BillingAddress `json:"billing_address"` + BrowserIP string `json:"browser_ip"` + BuyerAcceptsMarketing bool `json:"buyer_accepts_marketing"` + CancelReason *string `json:"cancel_reason"` + CancelledAt *time.Time `json:"cancelled_at"` + ClientDetails *ClientDetails `json:"client_details"` + ClosedAt *time.Time `json:"closed_at"` + CreatedAt time.Time `json:"created_at"` + Currency string `json:"currency"` + Customer *Customer `json:"customer"` + DiscountCodes *[]DiscountCode `json:"discount_codes"` + Email string `json:"email"` + FinancialStatus string `json:"financial_status"` + Fulfillments *[]Fulfillment `json:"fulfillments"` + FulfillmentStatus string `json:"fulfillment_status"` + Tags string `json:"tags"` + ID int64 `json:"id"` + InventoryBehaviour string `json:"inventory_behaviour"` //used only in create + LandingSite string `json:"landing_site"` + LineItems []LineItem `json:"line_items"` + Name string `json:"name"` + Note *string `json:"note"` + NoteAttributes *[]NoteAttribute `json:"note_attributes"` + Number int64 `json:"number"` + OrderNumber int64 `json:"order_number"` + PaymentGatewayNames []string `json:"payment_gateway_names"` + ProcessedAt time.Time `json:"processed_at"` + ProcessingMethod string `json:"processing_method"` + ReferringSite string `json:"referring_site"` + Refunds *[]Refund `json:"refunds"` + SendReceipt bool `json:"send_receipt"` //used only in create + SendFulfillmentReceipt bool `json:"send_fulfillment_receipt"` //used only in create + ShippingAddress *ShippingAddress `json:"shipping_address"` + ShippingLines *[]ShippingLine `json:"shipping_lines"` + SourceName string `json:"source_name"` + SubtotalPrice string `json:"subtotal_price"` + TaxLines *[]TaxLine `json:"tax_lines"` + TaxesIncluded bool `json:"taxes_included"` + TotalDiscounts string `json:"total_discounts"` + TotalPrice string `json:"total_price"` + TotalTax string `json:"total_tax"` + TotalWeight float64 `json:"total_weight"` + UpdatedAt time.Time `json:"updatedAt"` } //PaymentDetails are the details about a payment diff --git a/shopify/order.go b/shopify/order.go index 185eda9..8bd5d9e 100644 --- a/shopify/order.go +++ b/shopify/order.go @@ -2,6 +2,8 @@ package shopify import "fmt" +var emptyBody = make(map[string]string) + //GetOrders returns all the orders func (shopify *Shopify) GetOrders(parameters map[string]string) ([]Order, []error) { var orders OrdersResponse @@ -22,6 +24,67 @@ func (shopify *Shopify) GetOrder(orderID int64) (*Order, []error) { return &orderResponse.Order, nil } +//CloseOrder closes an order +func (shopify *Shopify) CloseOrder(orderID int64) (*Order, []error) { + var orderResponse OrderResponse + response, errors := shopify.Post(fmt.Sprintf("orders/%v/close", orderID), emptyBody) + if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { + return nil, err + } + return &orderResponse.Order, nil +} + +//OpenOrder re-opens an order +func (shopify *Shopify) OpenOrder(orderID int64) (*Order, []error) { + var orderResponse OrderResponse + response, errors := shopify.Post(fmt.Sprintf("orders/%v/order", orderID), emptyBody) + if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { + return nil, err + } + return &orderResponse.Order, nil +} + +//CancelOrder cancel an order +func (shopify *Shopify) CancelOrder(orderID int64) (*Order, []error) { + var orderResponse OrderResponse + response, errors := shopify.Post(fmt.Sprintf("orders/%v/cancel", orderID), emptyBody) + if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { + return nil, err + } + return &orderResponse.Order, nil +} + +//CreateOrder creates an order +func (shopify *Shopify) CreateOrder(order map[string]interface{}) (*Order, []error) { + var orderResponse OrderResponse + response, errors := shopify.Post("orders", order) + if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { + return nil, err + } + return &orderResponse.Order, nil +} + +//EditOrder edits an existing +func (shopify *Shopify) EditOrder(orderID int64, order map[string]interface{}) (*Order, []error) { + var orderResponse OrderResponse + order["id"] = orderID + response, errors := shopify.Post(fmt.Sprintf("orders/%v", orderID), order) + if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { + return nil, err + } + return &orderResponse.Order, nil +} + +//DeleteOrder cancel an order +func (shopify *Shopify) DeleteOrder(orderID int64) (*Order, []error) { + var orderResponse OrderResponse + response, errors := shopify.Delete(fmt.Sprintf("orders/%v", orderID)) + if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { + return nil, err + } + return &orderResponse.Order, nil +} + //GetOrderTransactions returns the order's transactions func (shopify *Shopify) GetOrderTransactions(orderID int64) ([]Transaction, []error) { var transactionsResponse TransactionsResponse From 977d8a0fe1c5af1c86bfd9313468d212f62478cf Mon Sep 17 00:00:00 2001 From: alecha Date: Tue, 31 Oct 2017 09:55:54 +0100 Subject: [PATCH 8/9] add support for discount code --- shopify/discountCode.go | 17 ++++++++++++++++ shopify/model.go | 42 ++++++++++++++++++++------------------- shopify/order.go | 44 ++++++++++++++++++++--------------------- shopify/refund.go | 15 ++++++++++++++ shopify/responses.go | 9 +++++++++ 5 files changed, 85 insertions(+), 42 deletions(-) create mode 100644 shopify/discountCode.go create mode 100644 shopify/refund.go diff --git a/shopify/discountCode.go b/shopify/discountCode.go new file mode 100644 index 0000000..5be2220 --- /dev/null +++ b/shopify/discountCode.go @@ -0,0 +1,17 @@ +package shopify + +import ( + "fmt" +) + +//CreateDiscountCode creates a discount code +func (shop *Shopify) CreateDiscountCode(priceRule string, discountCode DiscountCode) (*DiscountCode, []error) { + var discountCodeResponse DiscountCodeResponse + response, errors := shop.Post(fmt.Sprintf("price_rules/%v/discount_codes", priceRule), map[string]interface{}{ + "discount_code": discountCode, + }) + if err := unmarshal(response, errors, &discountCodeResponse); len(err) > 0 { + return nil, err + } + return &discountCodeResponse.DiscountCode, nil +} diff --git a/shopify/model.go b/shopify/model.go index a40de1f..a72844a 100644 --- a/shopify/model.go +++ b/shopify/model.go @@ -79,9 +79,9 @@ type Discount struct { //DiscountCode is a discount code type DiscountCode struct { - Amount string `json:"amount"` + Amount string `json:"amount,omitempty"` Code string `json:"code"` - Type string `json:"type"` + Type string `json:"type,omitempty"` } //Fulfillment is a fulfillment @@ -218,8 +218,9 @@ type Refund struct { Note string `json:"note"` RefundLineItems []RefundLineItem `json:"refund_line_items"` Restock bool `json:"restock"` - Transactions []Transaction `json:"transactions"` - UserID int64 `json:"user_id"` + //Transactions []Transaction `json:"transactions"` + UserID int64 `json:"user_id"` + OrderID int64 `json:"order_id"` } //RefundLineItem is a refund line item @@ -267,22 +268,23 @@ type TaxLine struct { //Transaction is a transaction type Transaction struct { - Amount string `json:"amount"` - Authorization *string `json:"authorization"` - CreatedAt time.Time `json:"created_at"` - DeviceID *string `json:"device_id"` - Gateway string `json:"gateway"` - SourceName string `json:"source_name"` - PaymentDetails PaymentDetails `json:"payment_details"` - ID int64 `json:"id"` - Kind string `json:"kind"` - OrderID int64 `json:"orderId"` - Receipt string `json:"receipt"` - ErrorCode string `json:"error_code"` - Status string `json:"status"` - Test bool `json:"test"` - UserID *int64 `json:"userId"` - Currency string `json:"currency"` + ID int64 `json:"id"` + OrderID int64 `json:"orderId"` + Amount string `json:"amount"` + Kind string `json:"kind"` + Authorization *string `json:"authorization"` + Message string `json:"message"` + CreatedAt time.Time `json:"created_at"` + DeviceID *string `json:"device_id"` + Gateway string `json:"gateway"` + SourceName string `json:"source_name"` + //PaymentDetails PaymentDetails `json:"payment_details"` + Receipt string `json:"receipt"` + ErrorCode string `json:"error_code"` + Status string `json:"status"` + Test bool `json:"test"` + UserID *int64 `json:"userId"` + Currency string `json:"currency"` } //Variant is a product's variant diff --git a/shopify/order.go b/shopify/order.go index 8bd5d9e..e7dee9f 100644 --- a/shopify/order.go +++ b/shopify/order.go @@ -5,9 +5,9 @@ import "fmt" var emptyBody = make(map[string]string) //GetOrders returns all the orders -func (shopify *Shopify) GetOrders(parameters map[string]string) ([]Order, []error) { +func (shop *Shopify) GetOrders(parameters map[string]string) ([]Order, []error) { var orders OrdersResponse - response, errors := shopify.GetWithParameters("orders", parameters) + response, errors := shop.GetWithParameters("orders", parameters) if err := unmarshal(response, errors, &orders); len(err) > 0 { return nil, err } @@ -15,9 +15,9 @@ func (shopify *Shopify) GetOrders(parameters map[string]string) ([]Order, []erro } //GetOrder returns a order given its id -func (shopify *Shopify) GetOrder(orderID int64) (*Order, []error) { +func (shop *Shopify) GetOrder(orderID int64) (*Order, []error) { var orderResponse OrderResponse - response, errors := shopify.Get(fmt.Sprintf("orders/%v", orderID)) + response, errors := shop.Get(fmt.Sprintf("orders/%v", orderID)) if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { return nil, err } @@ -25,9 +25,9 @@ func (shopify *Shopify) GetOrder(orderID int64) (*Order, []error) { } //CloseOrder closes an order -func (shopify *Shopify) CloseOrder(orderID int64) (*Order, []error) { +func (shop *Shopify) CloseOrder(orderID int64) (*Order, []error) { var orderResponse OrderResponse - response, errors := shopify.Post(fmt.Sprintf("orders/%v/close", orderID), emptyBody) + response, errors := shop.Post(fmt.Sprintf("orders/%v/close", orderID), emptyBody) if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { return nil, err } @@ -35,9 +35,9 @@ func (shopify *Shopify) CloseOrder(orderID int64) (*Order, []error) { } //OpenOrder re-opens an order -func (shopify *Shopify) OpenOrder(orderID int64) (*Order, []error) { +func (shop *Shopify) OpenOrder(orderID int64) (*Order, []error) { var orderResponse OrderResponse - response, errors := shopify.Post(fmt.Sprintf("orders/%v/order", orderID), emptyBody) + response, errors := shop.Post(fmt.Sprintf("orders/%v/order", orderID), emptyBody) if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { return nil, err } @@ -45,9 +45,9 @@ func (shopify *Shopify) OpenOrder(orderID int64) (*Order, []error) { } //CancelOrder cancel an order -func (shopify *Shopify) CancelOrder(orderID int64) (*Order, []error) { +func (shop *Shopify) CancelOrder(orderID int64) (*Order, []error) { var orderResponse OrderResponse - response, errors := shopify.Post(fmt.Sprintf("orders/%v/cancel", orderID), emptyBody) + response, errors := shop.Post(fmt.Sprintf("orders/%v/cancel", orderID), emptyBody) if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { return nil, err } @@ -55,9 +55,9 @@ func (shopify *Shopify) CancelOrder(orderID int64) (*Order, []error) { } //CreateOrder creates an order -func (shopify *Shopify) CreateOrder(order map[string]interface{}) (*Order, []error) { +func (shop *Shopify) CreateOrder(order map[string]interface{}) (*Order, []error) { var orderResponse OrderResponse - response, errors := shopify.Post("orders", order) + response, errors := shop.Post("orders", order) if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { return nil, err } @@ -65,10 +65,10 @@ func (shopify *Shopify) CreateOrder(order map[string]interface{}) (*Order, []err } //EditOrder edits an existing -func (shopify *Shopify) EditOrder(orderID int64, order map[string]interface{}) (*Order, []error) { +func (shop *Shopify) EditOrder(orderID int64, order map[string]interface{}) (*Order, []error) { var orderResponse OrderResponse order["id"] = orderID - response, errors := shopify.Post(fmt.Sprintf("orders/%v", orderID), order) + response, errors := shop.Post(fmt.Sprintf("orders/%v", orderID), order) if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { return nil, err } @@ -76,9 +76,9 @@ func (shopify *Shopify) EditOrder(orderID int64, order map[string]interface{}) ( } //DeleteOrder cancel an order -func (shopify *Shopify) DeleteOrder(orderID int64) (*Order, []error) { +func (shop *Shopify) DeleteOrder(orderID int64) (*Order, []error) { var orderResponse OrderResponse - response, errors := shopify.Delete(fmt.Sprintf("orders/%v", orderID)) + response, errors := shop.Delete(fmt.Sprintf("orders/%v", orderID)) if err := unmarshal(response, errors, &orderResponse); len(err) > 0 { return nil, err } @@ -86,9 +86,9 @@ func (shopify *Shopify) DeleteOrder(orderID int64) (*Order, []error) { } //GetOrderTransactions returns the order's transactions -func (shopify *Shopify) GetOrderTransactions(orderID int64) ([]Transaction, []error) { +func (shop *Shopify) GetOrderTransactions(orderID int64) ([]Transaction, []error) { var transactionsResponse TransactionsResponse - response, errors := shopify.Get(fmt.Sprintf("orders/%v/transactions", orderID)) + response, errors := shop.Get(fmt.Sprintf("orders/%v/transactions", orderID)) if err := unmarshal(response, errors, &transactionsResponse); len(err) > 0 { return nil, err } @@ -96,9 +96,9 @@ func (shopify *Shopify) GetOrderTransactions(orderID int64) ([]Transaction, []er } //GetOrderTransactionsCount returns the order's transactions count -func (shopify *Shopify) GetOrderTransactionsCount(orderID int64) (int, []error) { +func (shop *Shopify) GetOrderTransactionsCount(orderID int64) (int, []error) { var count CountResponse - response, errors := shopify.Get(fmt.Sprintf("orders/%v/transactions/count", orderID)) + response, errors := shop.Get(fmt.Sprintf("orders/%v/transactions/count", orderID)) if err := unmarshal(response, errors, &count); len(err) > 0 { return 0, err } @@ -106,9 +106,9 @@ func (shopify *Shopify) GetOrderTransactionsCount(orderID int64) (int, []error) } //GetOrdersCount returns all the products -func (shopify *Shopify) GetOrdersCount() (int, []error) { +func (shop *Shopify) GetOrdersCount() (int, []error) { var ordersCount CountResponse - response, errors := shopify.Get("orders/count") + response, errors := shop.Get("orders/count") if err := unmarshal(response, errors, &ordersCount); len(err) > 0 { return 0, err } diff --git a/shopify/refund.go b/shopify/refund.go new file mode 100644 index 0000000..a2b2784 --- /dev/null +++ b/shopify/refund.go @@ -0,0 +1,15 @@ +package shopify + +import "fmt" + +///admin/orders/#{id}/refunds.json + +//GetOrders returns all the orders +func (shopify *Shopify) GetOrderRefunds(orderID int64, parameters map[string]string) ([]Refund, []error) { + var refunds RefundsResponse + response, errors := shopify.Get(fmt.Sprintf("orders/%v/refunds", orderID)) + if err := unmarshal(response, errors, &refunds); len(err) > 0 { + return nil, err + } + return refunds.Refunds, nil +} diff --git a/shopify/responses.go b/shopify/responses.go index 3aa3060..4b553b2 100644 --- a/shopify/responses.go +++ b/shopify/responses.go @@ -1,5 +1,10 @@ package shopify +//DiscountCodeResponse is a response to /discount_codes endpoint +type DiscountCodeResponse struct { + DiscountCode DiscountCode `json:"discount_code"` +} + //OrdersResponse is a response to /orders endpoint type OrdersResponse struct { Orders []Order `json:"orders"` @@ -15,6 +20,10 @@ type TransactionsResponse struct { Transactions []Transaction `json:"transactions"` } +type RefundsResponse struct { + Refunds []Refund `json:"refunds"` +} + //CountResponse is a response to counts endpoint type CountResponse struct { Count int `json:"count"` From 0a7a07fed634661791cdae59cee159b600faabef Mon Sep 17 00:00:00 2001 From: alecha Date: Fri, 22 Dec 2017 12:30:48 +0100 Subject: [PATCH 9/9] update model --- LICENSE | 0 README.md | 0 shopify/discountCode.go | 0 shopify/model.go | 1 + shopify/order.go | 0 shopify/product.go | 0 shopify/refund.go | 0 shopify/responses.go | 0 shopify/utils.go | 0 9 files changed, 1 insertion(+) mode change 100644 => 100755 LICENSE mode change 100644 => 100755 README.md mode change 100644 => 100755 shopify/discountCode.go mode change 100644 => 100755 shopify/model.go mode change 100644 => 100755 shopify/order.go mode change 100644 => 100755 shopify/product.go mode change 100644 => 100755 shopify/refund.go mode change 100644 => 100755 shopify/responses.go mode change 100644 => 100755 shopify/utils.go diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/shopify/discountCode.go b/shopify/discountCode.go old mode 100644 new mode 100755 diff --git a/shopify/model.go b/shopify/model.go old mode 100644 new mode 100755 index a72844a..700a9cb --- a/shopify/model.go +++ b/shopify/model.go @@ -79,6 +79,7 @@ type Discount struct { //DiscountCode is a discount code type DiscountCode struct { + ID int `json:"id"` Amount string `json:"amount,omitempty"` Code string `json:"code"` Type string `json:"type,omitempty"` diff --git a/shopify/order.go b/shopify/order.go old mode 100644 new mode 100755 diff --git a/shopify/product.go b/shopify/product.go old mode 100644 new mode 100755 diff --git a/shopify/refund.go b/shopify/refund.go old mode 100644 new mode 100755 diff --git a/shopify/responses.go b/shopify/responses.go old mode 100644 new mode 100755 diff --git a/shopify/utils.go b/shopify/utils.go old mode 100644 new mode 100755