Skip to content

Instantly share code, notes, and snippets.

@devboy
Created January 12, 2016 12:30
Show Gist options
  • Save devboy/57a07653e5c2a061620e to your computer and use it in GitHub Desktop.
Save devboy/57a07653e5c2a061620e to your computer and use it in GitHub Desktop.
module Shopify.Product.Model where
type alias Product =
{ created_at : String,
id : Int,
product_type : String,
published_at : String,
published_scope : String,
title : String,
updated_at : String,
vendor : String,
images : List ProductImage }
type alias ProductImage =
{ created_at : String,
id : Int,
position : Int,
product_id : Int,
variant_ids : List Int,
src : String,
updated_at : String }
module Shopify.Products.Actions where
import Shopify.Products.Transit as Transit
import Shopify.Products.Model as Model
import Effects exposing ( Effects )
import Task exposing ( Task )
import Debug
type Action =
RequestProducts Model.Products
effectFromTask : Task x t -> t -> Effects t
effectFromTask a d =
Task.toMaybe a
|> Task.map (Maybe.withDefault d)
|> Effects.task
requestProducts : Effects Action
requestProducts =
effectFromTask Transit.requestPages []
|> Effects.map RequestProducts
update : Action -> Model.Products -> (Model.Products,Effects Action)
update a s =
case a of
RequestProducts ps -> (ps, Effects.none)
module Shopify.Products.Model where
import Shopify.Product.Model exposing ( Product )
import Signal
import Effects
type alias Products = List Product
module Shopify.Products.Transit where
import Shopify.Product.Transit as Transit exposing ( decode, encode )
import Json.Decode as D
import Shopify.Settings exposing ( path )
import Task exposing ( andThen )
import Http
decode =
D.list Transit.decode
|> D.at ["products"]
decodeCount =
D.at ["count"] D.int
getJson resource page decoder =
"admin/" ++ resource ++ ".json" ++ page
|> path
|> Http.get decoder
countPages count =
(floor ((toFloat count)/50))
pagination i =
"?page=" ++ (toString i)
collectPages =
countPages
>> (\t -> [1..t])
>> (List.map requestPage)
>> Task.sequence
>> Task.map List.concat
countProducts =
getJson "products/count" "" decodeCount
requestPage p =
getJson "products" (pagination p) decode
requestPages =
countProducts `andThen` collectPages
module Shopify.Products.View where
import Shopify.Products.Model
import Shopify.Product.View
import Shopify.Product.Model
import Html exposing ( .. )
import Html.Attributes exposing ( .. )
render : Shopify.Products.Model.Products -> Html
render ps = div [] (List.map Shopify.Product.View.render ps)
module Shopify.Product.Transit where
import Shopify.Product.Model exposing ( Product, ProductImage )
import Json.Decode as D exposing ( (:=) )
import Json.Decode.Extra exposing ( (|:) )
import Json.Encode as E
decode : D.Decoder Product
decode =
D.succeed Product
|: ("created_at" := D.string)
|: ("id" := D.int)
|: ("product_type" := D.string)
|: ("published_at" := D.string)
|: ("published_scope" := D.string)
|: ("title" := D.string)
|: ("updated_at" := D.string)
|: ("vendor" := D.string)
|: ("images" := D.list decodeImage)
decodeImage : D.Decoder ProductImage
decodeImage =
D.succeed ProductImage
|: ("created_at" := D.string)
|: ("id" := D.int)
|: ("position" := D.int)
|: ("product_id" := D.int)
|: ("variant_ids" := D.list D.int)
|: ("src" := D.string)
|: ("updated_at" := D.string)
encode : Product -> E.Value
encode p =
E.object
[ ("created_at" , E.string p.created_at),
("id", E.int p.id),
("product_type" , E.string p.product_type),
("published_at" , E.string p.published_at),
("published_scope" , E.string p.published_scope),
("title", E.string p.title),
("updated_at" , E.string p.updated_at),
("vendor" , E.string p.vendor) ]
module Shopify.Product.View where
import Shopify.Product.Model
import Html exposing ( .. )
import Html.Attributes exposing ( .. )
render : Shopify.Product.Model.Product -> Html
render p = Html.div [] []
@rvcas
Copy link

rvcas commented Sep 26, 2016

very cool, how does one authenticate?
The reason I found this is because I was considering doing something like the shopify-buy javascript sdk. Ideally interfacing with the shopify api will happen with an apiKey, domain, appId. Unless I'm misunderstanding how I am allowed to interact with the api. Because if I can just toss some credentials in the header using the /admin/ route and still be able to sell things that would be cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment