Skip to content

Commit

Permalink
Adding script tag insertion upon app installation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Darren Peters committed Apr 4, 2015
1 parent 46abde1 commit 75ce986
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
15 changes: 8 additions & 7 deletions config.go
Expand Up @@ -3,11 +3,12 @@ package shoauth
// ShopifyConfig is a structure that contains variables specific to the app
// that the developer is creating.
type ShopifyConfig struct {
ClientID string // Your app's API key
SharedSecret string // Your app's shared secret
RedirectURI string // If you want a different URL other than the callback on installation, put it here
HelpURI string // The URI the user is redirected to in order to view your app help page
Scopes []string // Your app's required scopes
IsEmbedded bool // If your app is embedded
Webhooks map[string]string // Webhooks that should be created on installation.
ClientID string // Your app's API key
SharedSecret string // Your app's shared secret
RedirectURI string // If you want a different URL other than the callback on installation, put it here
HelpURI string // The URI the user is redirected to in order to view your app help page
Scopes []string // Your app's required scopes
IsEmbedded bool // If your app is embedded
Webhooks map[string]string // Webhooks that should be created on installation.
Scripts map[string][]string // Script tags that should be created on installation. Map is map[event][]sources
}
1 change: 1 addition & 0 deletions handler.go
Expand Up @@ -24,6 +24,7 @@ func NewShopifyOauthHandler(successHandler http.Handler, failureHandler http.Han
RedirectURI: "",
HelpURI: "/help",
Webhooks: make(map[string]string),
Scripts: make(map[string][]string),
}

// Apply the custom config functions passed.
Expand Down
8 changes: 8 additions & 0 deletions installation.go
Expand Up @@ -54,5 +54,13 @@ func (s *shopifyOauthHandler) performInstallation(shop, code string) error {
}
}

for event, sources := range s.config.Scripts {
for _, source := range sources {
if err = createScriptTag(shop, responseData.AccessToken, event, source); err != nil {
return &ErrShopifyHTTPRequestFailed{err: err}
}
}
}

return nil
}
43 changes: 43 additions & 0 deletions script_tags.go
@@ -0,0 +1,43 @@
package shoauth

import (
"bytes"
"encoding/json"
"net/http"
)

type storeScriptTagRequestContainer struct {
ScriptTag storeScriptTagRequest `json:"script_tag"`
}

type storeScriptTagRequest struct {
Event string `json:"event"`
Source string `json:"src"`
}

func createScriptTag(shop, accessToken, event, source string) error {
client := http.Client{}
var requestData storeScriptTagRequestContainer
requestData.ScriptTag = storeScriptTagRequest{
Event: event,
Source: source,
}
requestDataString, err := json.Marshal(requestData)
if err != nil {
return ErrInvalidRequestData
}
req, err := http.NewRequest("POST", "https://"+shop+"/admin/script_tags.json", bytes.NewReader(requestDataString))
if err != nil {
return err
}
req.Header.Add("Content-type", "application/json")
req.Header.Add("X-Shopify-Access-Token", accessToken)
resp, err := client.Do(req)
if err != nil {
return &ErrShopifyHTTPRequestFailed{err: err}
} else if resp.StatusCode != 201 {
return &ErrShopifyHTTPRequestFailed{statusCode: resp.StatusCode}
}

return nil
}

0 comments on commit 75ce986

Please sign in to comment.