Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding script tag insertion upon app installation.
- Loading branch information
Darren Peters
committed
Apr 4, 2015
1 parent
46abde1
commit 75ce986
Showing
4 changed files
with
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |