Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding webhook creation to installation script.
- Loading branch information
Darren Peters
committed
Mar 16, 2015
1 parent
3c4378c
commit 8cba338
Showing
5 changed files
with
69 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |