Skip to content

Commit

Permalink
v0.1.2: Renamed NewClient with New + ReadeMe update.
Browse files Browse the repository at this point in the history
  • Loading branch information
kayode0x committed Apr 21, 2023
1 parent 1aeac9a commit 421026b
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 24 deletions.
137 changes: 135 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,135 @@
# plunk
[unofficial] Go SDK for Plunk.
# Plunk with Go

[![GoDoc](https://godoc.org/github.com/kayode0x/plunk?status.svg)](https://godoc.org/github.com/kayode0x/plunk)
[![Go Report Card](https://goreportcard.com/badge/github.com/kayode0x/plunk)](https://goreportcard.com/report/github.com/kayode0x/plunk)
[![Coverage Status](https://coveralls.io/repos/github/kayode0x/plunk/badge.svg?branch=main)](https://coveralls.io/github/kayode0x/plunk?branch=main)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/kayode0x/plunk/main/LICENSE)

<!-- FEATURES -->
## Features
The Plunk Go SDK includes the following features:

Transactional Emails: Use the SendTransactionalEmail method to send one or more emails to your subscribers.

Events: Trigger events and creates it if it doesn't exist. You can also delete events.

Contacts: Create, update, and delete contacts. You can also get a list of contacts, as well as the number of contacts in your account.

Easy integration: The Plunk Go SDK is easy to integrate into your Go applications, with a simple and intuitive API.

<!-- GETTING STARTED -->
## Getting started

To get started with the Plunk Go SDK, simply install it using the go get command:

``` go get github.com/kayode0x/plunk ```

Then, create a new Plunk object with your Plunk API key and start using the SDK to send and track emails.

Example
Here's an example of how to use the Plunk Go SDK to send a simple email:

```go
package main

import (
"fmt"
"github.com/kayode0x/plunk"
)

func main() {
// create a new Plunk object with your API key
p, _ := plunk.New("YOUR_API_KEY")

// optionally, you can set some configuration options.
config := plunk.Config{
BaseUrl: "https://api.useplunk.com",
Debug: true,
Client: &http.Client{
Timeout: 10 * time.Second,
},
}

// create a new Plunk object with your API key and config
p, err := plunk.New("YOUR_API_KEY", config)
if err != nil {
fmt.Printf("Error creating Plunk object: %v", err)
return
}

// create a new email payload
payload := &TransactionalEmailPayload{
To: "test@example.com",
Subject: "Test Subject",
Body: "Test Body",
}

// send the email
response, err := plunk.SendTransactionalEmail(payload)
if err != nil {
fmt.Printf("Error sending email: %v", err)
return
}

fmt.Printf("Email sent: %v", response)
}

```

<!-- ROADMAP -->
## Roadmap

- [x] Send transactional emails

- [x] Trigger events

- [x] CRUD contacts

- [x] Get contacts, and number of contacts.

- [ ] Your awesome feature 😉

See the [open issues](https://github.com/kayode0x/plunk/issues) for a full list of proposed features (and known issues).

<!-- CONTRIBUTING -->
## Contributing

Any contributions you make are **greatly appreciated**.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".

Don't forget to give the project a star! Thanks again!

1. Fork the Project

2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)

3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)

4. Push to the Branch (`git push origin feature/AmazingFeature`)

5. Open a Pull Request

<!-- TESTING -->
## Testing
To run tests, you need a Plunk API key. You can get one by signing up for a free account at https://useplunk.com.

Add the environment variable PLUNK_SECRET_KEY in your .env file with your Plunk API key.

Then, run the tests using the following command:

``` go test -v ```

If you receive an error that says contact already exists, you can manually delete the contact from your Plunk account, and then run the tests again.

<!-- SUPPORT -->
## Support

Ping me here, or feel free to reach out on twitter [@kayode0x](https://twitter.com/kayode0x). For Plunk specific issues, please reach out to [Plunk](https://useplunk.com).

<!-- LICENSE -->
## License

Distributed under the MIT License. See `LICENSE.txt` for more information.

<p align="right">(<a href="#top">back to top</a>)</p>
14 changes: 7 additions & 7 deletions contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
)

func TestGetContact(t *testing.T) {
p, err := NewClient(secretKey, opts)
p, err := New(secretKey, opts)
assert.Nil(t, err)

payload := &CreateContactPayload{
Expand All @@ -49,7 +49,7 @@ func TestGetContact(t *testing.T) {
}

func TestGetContacts(t *testing.T) {
p, err := NewClient(secretKey, opts)
p, err := New(secretKey, opts)
assert.Nil(t, err)

contacts, err := p.GetContacts()
Expand All @@ -58,7 +58,7 @@ func TestGetContacts(t *testing.T) {
}

func TestGetContactsCount(t *testing.T) {
p, err := NewClient(secretKey, opts)
p, err := New(secretKey, opts)
assert.Nil(t, err)

count, err := p.GetContactsCount()
Expand All @@ -67,7 +67,7 @@ func TestGetContactsCount(t *testing.T) {
}

func TestCreateContact(t *testing.T) {
p, err := NewClient(secretKey, opts)
p, err := New(secretKey, opts)
assert.Nil(t, err)

data := map[string]interface{}{
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestCreateContact(t *testing.T) {
}

func TestUpdateContact(t *testing.T) {
p, err := NewClient(secretKey, opts)
p, err := New(secretKey, opts)
assert.Nil(t, err)

data := map[string]interface{}{
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestUpdateContact(t *testing.T) {
}

func TestDeleteContact(t *testing.T) {
p, err := NewClient(secretKey, opts)
p, err := New(secretKey, opts)
assert.Nil(t, err)

payload := &CreateContactPayload{
Expand All @@ -168,7 +168,7 @@ func TestDeleteContact(t *testing.T) {
}

func TestSubOrUnsubscribeContact(t *testing.T) {
p, err := NewClient(secretKey, opts)
p, err := New(secretKey, opts)
assert.Nil(t, err)

payload := &CreateContactPayload{
Expand Down
4 changes: 2 additions & 2 deletions events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
)

func TestTriggerEvent(t *testing.T) {
p, err := NewClient(secretKey, opts)
p, err := New(secretKey, opts)
assert.Nil(t, err)

payload := &EventPayload{
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestTriggerEvent(t *testing.T) {
}

func TestDeleteEvent(t *testing.T) {
p, err := NewClient(secretKey, opts)
p, err := New(secretKey, opts)
assert.Nil(t, err)

payload := &EventPayload{
Expand Down
10 changes: 5 additions & 5 deletions plunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ type Plunk struct {
*Config
}

// NewClient returns a new Plunk client.
func NewClient(apiKey string, opts ...func(*Config)) (*Plunk, error) {
// New returns a new Plunk client.
func New(apiKey string, opts ...func(*Config)) (*Plunk, error) {
if apiKey == "" {
return nil, ErrNoAPIKey
}
Expand All @@ -59,14 +59,14 @@ func NewClient(apiKey string, opts ...func(*Config)) (*Plunk, error) {
}, nil
}

// NewClientFromEnv returns a new Plunk client using the PLUNK_API_KEY environment variable.
func NewClientFromEnv() (*Plunk, error) {
// NewFromEnv returns a new Plunk client using the PLUNK_API_KEY environment variable.
func NewFromEnv() (*Plunk, error) {
apiKey := os.Getenv("PLUNK_API_KEY")
if apiKey == "" {
return nil, ErrNoAPIKey
}

return NewClient(apiKey)
return New(apiKey)
}

// Append the endpoint to the base URL.
Expand Down
2 changes: 1 addition & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestDefaultReqConfig(t *testing.T) {

func TestSendRequest(t *testing.T) {
// create a new Plunk object with a mocked http.Client
p, err := NewClient(secretKey, opts)
p, err := New(secretKey, opts)
assert.Nil(t, err)

// create a SendConfig object with a GET method and a mocked response body
Expand Down
14 changes: 7 additions & 7 deletions transactional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
)

func TestSendTransactionalEmail(t *testing.T) {
p, err := NewClient(secretKey, opts)
p, err := New(secretKey, opts)
assert.Nil(t, err)

payload := &TransactionalEmailPayload{
To: "khizzyjr@gmail.com",
To: "test@example.com",
Subject: "Test Subject",
Body: "Test Body",
}
Expand All @@ -24,12 +24,12 @@ func TestSendTransactionalEmail(t *testing.T) {
}

func TestSendMultipleTransactionalEmails(t *testing.T) {
p, err := NewClient(secretKey, opts)
p, err := New(secretKey, opts)
assert.Nil(t, err)

payload := []*TransactionalEmailPayload{
{
To: testEmail,
To: "test@example.com",
Subject: "Test Subject",
Body: "Test Body",
},
Expand All @@ -53,7 +53,7 @@ func TestSendMultipleTransactionalEmails(t *testing.T) {
}

func TestSendTransactionalEmailWithInvalidPayload(t *testing.T) {
p, err := NewClient(secretKey, opts)
p, err := New(secretKey, opts)
assert.Nil(t, err)

testCases := []struct {
Expand All @@ -70,15 +70,15 @@ func TestSendTransactionalEmailWithInvalidPayload(t *testing.T) {
},
{
payload: &TransactionalEmailPayload{
To: testEmail,
To: "test@example.com",
Subject: "",
Body: "Test Body",
},
err: ErrMissingSubject,
},
{
payload: &TransactionalEmailPayload{
To: testEmail,
To: "test@example.com",
Subject: "Test Subject",
Body: "",
},
Expand Down

0 comments on commit 421026b

Please sign in to comment.