-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support email received webhook (#271)
- Loading branch information
Showing
4 changed files
with
115 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package webhook | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/harryzcy/mailbox/internal/env" | ||
) | ||
|
||
const ( | ||
EventEmail = "email" | ||
ActionReceived = "received" | ||
) | ||
|
||
type Webhook struct { | ||
Event string `json:"event"` | ||
Action string `json:"action"` | ||
Email Email | ||
} | ||
|
||
type Email struct { | ||
ID string `json:"id"` // message id | ||
} | ||
|
||
func Enabled() bool { | ||
return env.WebhookURL != "" | ||
} | ||
|
||
func SendWebhook(ctx context.Context, data *Webhook) error { | ||
client := http.Client{ | ||
Timeout: 5 * time.Second, | ||
} | ||
|
||
body, err := json.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
req, err := http.NewRequestWithContext(ctx, "POST", env.WebhookURL, bytes.NewReader(body)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
res, err := client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer res.Body.Close() | ||
|
||
return nil | ||
} |
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 webhook | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/harryzcy/mailbox/internal/env" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSendWebhook(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
var webhook Webhook | ||
err := json.NewDecoder(req.Body).Decode(&webhook) | ||
assert.Nil(t, err) | ||
assert.Equal(t, EventEmail, webhook.Event) | ||
assert.Equal(t, ActionReceived, webhook.Action) | ||
assert.Equal(t, "123", webhook.Email.ID) | ||
|
||
_, err = rw.Write([]byte("OK")) | ||
assert.Nil(t, err) | ||
})) | ||
defer server.Close() | ||
|
||
env.WebhookURL = server.URL | ||
err := SendWebhook(context.Background(), &Webhook{ | ||
Event: EventEmail, | ||
Action: ActionReceived, | ||
Email: Email{ID: "123"}, | ||
}) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestSendWebhook_Error(t *testing.T) { | ||
env.WebhookURL = "" | ||
err := SendWebhook(context.Background(), &Webhook{ | ||
Event: EventEmail, | ||
Action: ActionReceived, | ||
Email: Email{ID: "123"}, | ||
}) | ||
assert.NotNil(t, err) | ||
} |