Skip to content

Commit

Permalink
Adding webhook creation to installation script.
Browse files Browse the repository at this point in the history
  • Loading branch information
Darren Peters committed Mar 16, 2015
1 parent 3c4378c commit 8cba338
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
11 changes: 6 additions & 5 deletions config.go
Expand Up @@ -3,9 +3,10 @@ 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
InstallationURI string // The URI the user is redirected to in order to install your app
CallbackURI string // The base application URI the user is typically redirected to
HelpURI string // The URI the user is redirected to in order to view your app help page
ClientID string // Your app's API key
SharedSecret string // Your app's shared secret
InstallationURI string // The URI the user is redirected to in order to install your app
CallbackURI string // The base application URI the user is typically redirected to
HelpURI string // The URI the user is redirected to in order to view your app help page
Webhooks map[string]string // Webhooks that should be created on installation.
}
3 changes: 3 additions & 0 deletions errors.go
Expand Up @@ -18,6 +18,9 @@ var (
// ErrInvalidResponseData is returned when a json.Unmarshal fails on a
// response from shopify
ErrInvalidResponseData = errors.New("The response data returned from shopify was in an unexpected format.")
// ErrBadPersistence is returned when the implementation of persistence
// passed to shoauth fails for some reason.
ErrBadPersistence = errors.New("The persistence passed to shoauth failed to perform an action.")
)

// ErrShopifyHTTPRequestFailed is returned when an HTTP request to a
Expand Down
4 changes: 3 additions & 1 deletion handler.go
Expand Up @@ -18,9 +18,10 @@ type shopifyOauthHandler struct {
func NewShopifyOauthHandler(successHandler http.Handler, failureHandler http.Handler, persistence ShopifyPersistence, configOptions ...func(*ShopifyConfig)) *shopifyOauthHandler {
// Set some sensible defaults.
config := &ShopifyConfig{
InstallationURI: "/install",
InstallationURI: "/",
CallbackURI: "/",
HelpURI: "/help",
Webhooks: make(map[string]string),
}

// Apply the custom config functions passed.
Expand All @@ -37,6 +38,7 @@ func NewShopifyOauthHandler(successHandler http.Handler, failureHandler http.Han
}

func (s *shopifyOauthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

// If the user has authenticated via the initial app Callback, the app
// should have registered a valid session for the user. As long as that
// session is active, we do not need to validate requests from said user.
Expand Down
13 changes: 12 additions & 1 deletion installation.go
Expand Up @@ -43,5 +43,16 @@ func (s *shopifyOauthHandler) performInstallation(shop, code string) error {
return ErrInvalidResponseData
}

return s.ShopifyPersistence.CreateInstallation(shop, responseData.AccessToken)
if err = s.ShopifyPersistence.CreateInstallation(shop, responseData.AccessToken); err != nil {
return ErrBadPersistence
}

// Create our webhooks
for webhook, address := range s.config.Webhooks {
if err = createWebhook(shop, responseData.AccessToken, webhook, address); err != nil {
return &ErrShopifyHTTPRequestFailed{err: err}
}
}

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

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

type storeWebhookRequestContainer struct {
Webhook storeWebhookRequest `json:"webhook"`
}

type storeWebhookRequest struct {
Topic string `json:"topic"`
Address string `json:"address"`
Format string `json:"format"`
}

func createWebhook(shop, accessToken, webhook, address string) error {
client := http.Client{}
var requestData storeWebhookRequestContainer
requestData.Webhook = storeWebhookRequest{
Topic: webhook,
Address: address,
Format: "json",
}
requestDataString, err := json.Marshal(requestData)
if err != nil {
return ErrInvalidRequestData
}
req, err := http.NewRequest("POST", "https://"+shop+"/admin/webhooks.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 8cba338

Please sign in to comment.