-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from MikeMwita/code-structure-reorganization
Code Refactor
- Loading branch information
Showing
4 changed files
with
136 additions
and
121 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,34 @@ | ||
package sms_sender | ||
|
||
import ( | ||
"fmt" | ||
"context" | ||
"github.com/MikeMwita/africastalking-go/pkg/sms" | ||
"log" | ||
"net/http" | ||
"os" | ||
"time" | ||
) | ||
|
||
func main() { | ||
// Example usage | ||
sender := sms.SmsSender{ | ||
ApiKey: "your_api_key", | ||
ApiUser: "your_api_user", | ||
apiKey := os.Getenv("API_KEY") | ||
apiUser := os.Getenv("API_USER") | ||
|
||
client := sms.NewClient(&http.Client{}, apiKey, apiUser) | ||
sender := &sms.SmsSender{ | ||
Client: client, | ||
Recipients: []string{"+1234567890"}, | ||
Message: "Hello, world!", | ||
Message: "Test message", | ||
Sender: "YourSenderID", | ||
SmsKey: "unique_sms_key", | ||
} | ||
|
||
response, err := sender.SendSMS() | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
resp, err := sender.RetrySendSMS(ctx, 3) | ||
if err != nil { | ||
log.Fatal(err) | ||
log.Fatalf("Failed to send SMS: %v", err) | ||
} | ||
|
||
fmt.Printf("Response: %+v\n", response) | ||
log.Printf("SMS Response: %+v", resp) | ||
} |
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,28 @@ | ||
package sms | ||
|
||
import "net/http" | ||
|
||
const ( | ||
DefaultAPIURL = "https://api.africastalking.com/version1/messaging" | ||
sandboxAPIURL = "https://api.sandbox.africastalking.com/version1/messaging" | ||
) | ||
|
||
type Doer interface { | ||
Do(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
type Client struct { | ||
apiURL string | ||
apiKey string | ||
apiUser string | ||
client Doer | ||
} | ||
|
||
func NewClient(client Doer, apiKey, apiUser string) *Client { | ||
return &Client{ | ||
apiURL: DefaultAPIURL, | ||
apiKey: apiKey, | ||
apiUser: apiUser, | ||
client: client, | ||
} | ||
} |
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,35 @@ | ||
package sms | ||
|
||
type SmsSender struct { | ||
Client *Client | ||
Recipients []string `json:"recipients"` | ||
Message string `json:"message"` | ||
Sender string `json:"sender"` | ||
SmsKey string `json:"sms_key"` | ||
} | ||
|
||
type Recipient struct { | ||
Key string `json:"key"` | ||
Cost string `json:"cost"` | ||
SmsKey string `json:"sms_key"` | ||
MessageId string `json:"message_id"` | ||
MessagePart int `json:"message_part"` | ||
Number string `json:"number"` | ||
Status string `json:"status"` | ||
StatusCode string `json:"status_code"` | ||
} | ||
|
||
type SmsMessageData struct { | ||
Message string `json:"message"` | ||
Cost string `json:"cost"` | ||
Recipients []Recipient `json:"recipients"` | ||
} | ||
type ErrorResponse struct { | ||
HasError bool `json:"has_error"` | ||
Message string `json:"message"` | ||
} | ||
|
||
type SmsSenderResponse struct { | ||
ErrorResponse ErrorResponse `json:"error_response"` | ||
SmsMessageData SmsMessageData `json:"sms_message_data"` | ||
} |