Skip to content

Commit

Permalink
feat: add idem key
Browse files Browse the repository at this point in the history
  • Loading branch information
danvixent committed Jun 20, 2023
1 parent 8d73310 commit ae9c5f6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions emit.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ func main() {
func parsePersistentArgs(cmd *cobra.Command) {
var url string
var secret string
var idemKey string
cmd.PersistentFlags().StringVar(&url, "url", "", "URL to emit event to")
cmd.PersistentFlags().StringVar(&secret, "secret", "", "secret to use to encode hmac header")
cmd.PersistentFlags().StringVar(&idemKey, "idem-key", "", "Idempotency Key")

cmd.AddCommand(addShopifyCommand())
cmd.AddCommand(addGithubCommand())
Expand Down
11 changes: 9 additions & 2 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ func addGithubCommand() *cobra.Command {
return err
}

idemKey, err := cmd.Flags().GetString("idem-key")
if err != nil {
return err
}

if url == "" || secret == "" {
return errors.New("url and secret are required to emit github event")
}
fmt.Println("url", url)
fmt.Println("idem key", idemKey)
fmt.Println("secret", secret)

b, err := json.Marshal(body)
Expand All @@ -53,7 +59,7 @@ func addGithubCommand() *cobra.Command {
return fmt.Errorf("failed to generate hmac header new http request: %v", err)
}

setGithubHeaders(r, hmacHeader)
setGithubHeaders(r, hmacHeader, idemKey)

resp, err := http.DefaultClient.Do(r)
if err != nil {
Expand All @@ -80,6 +86,7 @@ func generateGithubHMAC(secret string, payload []byte) (string, error) {
return generateHMAC(secret, payload, "hex")
}

func setGithubHeaders(r *http.Request, hmacHeader string) {
func setGithubHeaders(r *http.Request, hmacHeader, idemKey string) {
r.Header.Set("X-Hub-Signature-256", "sha256="+hmacHeader)
r.Header.Set("X-Idempotency-Key", idemKey)
}
11 changes: 9 additions & 2 deletions shopify.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func addShopifyCommand() *cobra.Command {
return err
}

idemKey, err := cmd.Flags().GetString("idem-key")
if err != nil {
return err
}

secret, err := cmd.Flags().GetString("secret")
if err != nil {
return err
Expand All @@ -40,6 +45,7 @@ func addShopifyCommand() *cobra.Command {
return errors.New("url and secret are required to emit shopify event")
}
fmt.Println("url", url)
fmt.Println("idem key", idemKey)
fmt.Println("secret", secret)

b, err := json.Marshal(body)
Expand All @@ -58,7 +64,7 @@ func addShopifyCommand() *cobra.Command {
return fmt.Errorf("failed to generate hmac header new http request: %v", err)
}

setShopifyHeaders(r, hmacHeader)
setShopifyHeaders(r, hmacHeader, idemKey)

resp, err := http.DefaultClient.Do(r)
if err != nil {
Expand Down Expand Up @@ -102,8 +108,9 @@ func generateHMAC(secret string, payload []byte, encoding string) (string, error
}
}

func setShopifyHeaders(r *http.Request, hmacHeader string) {
func setShopifyHeaders(r *http.Request, hmacHeader string, idemKey string) {
r.Header.Set("X-Shopify-Topic", "orders/create")
r.Header.Set("X-Idempotency-Key", idemKey)
r.Header.Set("X-Shopify-Hmac-SHA256", hmacHeader)
r.Header.Set("X-Shopify-Shop-Domain", "emit-test-domain")
r.Header.Set("X-Shopify-API-Version", "2022-07")
Expand Down

0 comments on commit ae9c5f6

Please sign in to comment.