Skip to content

split out fiiles #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
load("@io_bazel_rules_go//go:def.bzl", "go_path")
load("@bazel_gazelle//:def.bzl", "gazelle")

# gazelle:prefix k9bookshelf
gazelle(name = "gazelle")

go_path(
name = "gopath",
mode = "link",
deps = [
"//syncdata/cmd",
],
)

config_setting(
name = "darwin",
values = {"cpu": "darwin"},
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,18 @@ bin/syncdata: $(GO_FILES) WORKSPACE
$(BZL) build //syncdata/cmd:all
cp -f $(BZL_BIN)/syncdata/cmd/cmd_/cmd bin/syncdata

.PHONY: setup
setup: WORKSPACE */BUILD.bazel GOPATH

.PHONY: syncdata/BUILD.bazel gqlgenc/BUILD.bazel

*/BUILD.bazel: $(GO_FILES)
$(BZL) run //:gazelle

WORKSPACE: go.mod syncdata/BUILD.bazel
$(BZL) run //:gazelle -- update-repos -from_file=go.mod

.PHONY: GOPATH

GOPATH:
$(BZL) build //:gopath
6 changes: 6 additions & 0 deletions contents/blogs/news/2020-11-25-新着入荷.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
以下の書籍が入荷しました。

* [Language Implementation Patterns: Create Your Own Domain-Specific and General Programming Languages](https://k9bookshelf.com/products/language-implementation-patterns-create-your-own-domain-specific-and-general-programming-languages)
* [Node.js Design Patterns - Third edition: Design and implement production-grade Node.js applications using proven patterns and techniques](https://k9bookshelf.com/products/node-js-design-patterns-third-edition-design-and-implement-production-grade-node-js-applications-using-proven-patterns-and-techniques)
* [The Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi](https://k9bookshelf.com/products/the-makers-guide-to-the-zombie-apocalypse-defend-your-base-with-simple-circuits-arduino-and-raspberry-pi)
* [Programming Webassembly with Rust: Unified Development for Web, Mobile, and Embedded Applications (1ST ed.)](https://k9bookshelf.com/products/programming-webassembly-with-rust)
6 changes: 0 additions & 6 deletions contents/blogs/news/新着入荷.md

This file was deleted.

1 change: 0 additions & 1 deletion contents/pages/contact.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
k9bookshelfに関するお問い合わせはこちらからお願いいたします。

3 changes: 3 additions & 0 deletions syncdata/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ go_library(
name = "go_default_library",
srcs = [
"article.go",
"client.go",
"deploy.go",
"download.go",
"syncdata.go",
],
importpath = "k9bookshelf/syncdata",
Expand Down
41 changes: 28 additions & 13 deletions syncdata/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,31 @@ import (
// Article is documented at https://shopify.dev/docs/admin-api/rest/reference/online-store/article
type Article struct {
ID int64 `json:"id"`
Title string `json:"title"`
CreatedAt *time.Time `json:"created_at"`
BodyHTML string `json:"body_html"`
BlogID int64 `json:"blog_id"`
Author string `json:"author"`
UserID int64 `json:"user_id"`
PublishedAt *time.Time `json:"published_at"`
UpdatedAt *time.Time `json:"updated_at"`
SummaryHTML *string `json:"summary_html"`
TemplateSuffix *string `json:"template_suffix"`
Handle string `json:"handle"`
Tags string `json:"tags"`
AdminGraphqlAPIID string `json:"admin_graphql_api_id"`
Title string `json:"title,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
BodyHTML string `json:"body_html,omitempty"`
BlogID int64 `json:"blog_id,omitempty"`
Author string `json:"author,omitempty"`
UserID int64 `json:"user_id,omitempty"`
PublishedAt *time.Time `json:"published_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
SummaryHTML *string `json:"summary_html,omitempty"`
TemplateSuffix *string `json:"template_suffix,omitempty"`
Handle string `json:"handle,omitempty"`
Tags string `json:"tags,omitempty"`
AdminGraphqlAPIID string `json:"admin_graphql_api_id,omitempty"`
}

// Articles is not documented yet.
type Articles struct {
Articles []Article `json:"articles"`
}

// ArticlePayload is not documented yet.
type ArticlePayload struct {
Article Article `json:"article"`
}

// ArticleResource is not documented yet.
type ArticleResource struct {
client *shopify.Client
Expand All @@ -50,3 +55,13 @@ func (a *ArticleResource) List(blogID int64) (*Articles, error) {
}
return &articles, nil
}

// Put update article
func (a *ArticleResource) Put(article Article) (*Article, error) {
var response Article
err := a.client.Put(path.Join("admin", "api", apiVersion, "blogs", fmt.Sprint(article.BlogID), "articles", fmt.Sprintf("%d.json", article.ID)), ArticlePayload{Article: article}, &response)
if err != nil {
return nil, err
}
return &response, nil
}
57 changes: 57 additions & 0 deletions syncdata/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package syncdata

import (
"context"
"fmt"
"k9bookshelf/generated"
"net/http"

"github.com/Yamashou/gqlgenc/client"
shopify "github.com/bold-commerce/go-shopify"
)

func establishGqlClient() (*generated.Client, context.Context) {
authHeader := func(req *http.Request) {
req.Header.Set("X-Shopify-Access-Token", appSecret)
}

return &generated.Client{
Client: client.NewClient(http.DefaultClient,
fmt.Sprintf("https://%s/admin/api/%s/graphql.json", shopDomain, apiVersion),
authHeader),
}, context.Background()
}

func establishRestClient() *shopify.Client {
app := shopify.App{
ApiKey: appKey,
ApiSecret: appSecret,
}

return shopify.NewClient(app, shopDomain, appSecret, shopify.WithVersion(apiVersion))
}

func fetchProducts(ctx context.Context, adminClient *generated.Client) (*generated.Products, error) {
var cursor *string
var res *generated.Products

for {
tmpRes, err := adminClient.Products(ctx, 10, cursor)
if err != nil {
return nil, err
}
if res == nil {
res = tmpRes
} else {
res.Products.Edges = append(res.Products.Edges, tmpRes.Products.Edges...)
}

if !tmpRes.Products.PageInfo.HasNextPage {
break
} else {
last := tmpRes.Products.Edges[len(tmpRes.Products.Edges)-1]
cursor = &last.Cursor
}
}
return res, nil
}
10 changes: 4 additions & 6 deletions syncdata/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"
"os"

"k9bookshelf/syncdata"
Expand All @@ -25,8 +26,7 @@ var deployCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
err := syncdata.Deploy(cmd.Flag("input").Value.String())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
log.Fatal(err)
}
},
}
Expand All @@ -37,8 +37,7 @@ var downloadCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
err := syncdata.Download(cmd.Flag("output").Value.String())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
log.Fatal(err)
}
},
}
Expand All @@ -55,7 +54,6 @@ func main() {
rootCmd.AddCommand(deployCmd)

if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
log.Fatal(err)
}
}
Loading