Skip to content
This repository has been archived by the owner on Sep 7, 2022. It is now read-only.

Commit

Permalink
Fixes demo
Browse files Browse the repository at this point in the history
A working demo.
- Sources API Key, username and shortcode from environment
- Removes .idea/ from git
- Refactors duplicated logic in host generation

Fixes #6
  • Loading branch information
peteretelej committed Feb 15, 2018
1 parent 3b89c8e commit acf1d9a
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea/*
.idea/
demo/demo
demo/secrets.go
demo/secrets.go
6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ go get github.com/AndroidStudyOpenSource/africastalking-go/account
go get github.com/AndroidStudyOpenSource/africastalking-go/payment
```
## Demo
In order to run the demo, export the the following values to your environment. They can be found/generated at the Africa's Talking Dashboard.

``` sh
export AT_APIKEY=Your-AfricasTalking-API-KEY
export AT_USERNAME=Your-AfricasTalking-APP-USERNAME
export AT_SHORTCODE=Your-AfricasTalking-APP-Shortcode
```

That's all's that required. Assuming its a sandbox app, here's to run the demo:

``` sh
cd demo
go run main.go -e sandbox -m "Hello gopher!" -r "+254700000000"
```

## Usage
The package needs to be configured with your app username and API key (which you can get from the dashboard). You can also declare if you are running in production or in sandbox.
Expand Down Expand Up @@ -59,7 +74,7 @@ import (
"fmt"
"log"
africastkng "github.com/AndroidStudyOpenSource/africastalking-go/sms"
"github.com/AndroidStudyOpenSource/africastalking-go/sms"
)
const (
Expand Down
3 changes: 2 additions & 1 deletion account/account.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package account

import (
"africastalking/util"
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/AndroidStudyOpenSource/africastalking-go/util"
)

// Response is a model
Expand Down
31 changes: 18 additions & 13 deletions demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"log"
"os"

"github.com/AndroidStudyOpenSource/africastalking-go/account"
"github.com/AndroidStudyOpenSource/africastalking-go/sms"
"africastalking/account"
)

const (
apiKey = ""
username = ""
var (
apiKey = os.Getenv("AT_APIKEY")
username = os.Getenv("AT_USERNAME")
shortcode = os.Getenv("AT_SHORTCODE")
)

func main() {
Expand All @@ -22,24 +23,28 @@ func main() {

flag.Parse()
if *recipient == "" || *message == "" {
log.Println("please enter recipient and message")
os.Exit(1)
log.Fatal("please enter all required arguments. see --help")
}

if apiKey == "" || username == "" {
log.Fatal("missing required environment variables: AT_APIKEY, AT_USERNAME")
}

smsService := sms.NewService(username, apiKey, *env)
// Entered at the commandline
sendResponse, err := smsService.Send("Me4u", *recipient, *message)

sendResponse, err := smsService.Send(shortcode, *recipient, *message)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(sendResponse)
fmt.Printf("Failed to send sms: %v", err)
}
fmt.Printf("SMS Send reponse: %v\n", sendResponse)

accountService := account.NewService(username, apiKey, *env)
user, err := accountService.GetUser()
if err != nil {
fmt.Println(err)
} else {
fmt.Println(user)
return
}

fmt.Printf("User: %v\n", user)

}
19 changes: 9 additions & 10 deletions sms/sms.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package sms

import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -55,12 +55,11 @@ func (service Service) Send(from, to, message string) (*SendMessageResponse, err
if err != nil {
return nil, err
}

var smsMessageResponse SendMessageResponse
json.NewDecoder(res.Body).Decode(&smsMessageResponse)
log.Println(res.Body)
defer res.Body.Close()

var smsMessageResponse SendMessageResponse
if err := json.NewDecoder(res.Body).Decode(&smsMessageResponse); err != nil {
return nil, errors.New("unable to parse sms response")
}
return &smsMessageResponse, nil
}

Expand Down Expand Up @@ -88,17 +87,17 @@ func (service Service) SendBulk(from, to, message string, bulkMode int, enqueue
if err != nil {
return nil, err
}
defer res.Body.Close()

var smsMessageResponse SendMessageResponse
json.NewDecoder(res.Body).Decode(&smsMessageResponse)
defer res.Body.Close()

return &smsMessageResponse, nil
}

// SendPremium - POST
func (service Service) SendPremium(username, to, from, message, keyword,
linkID, retryDurationInHours string, bulkMode int) (*SendMessageResponse, error) {
linkID, retryDurationInHours string, bulkMode int) (*SendMessageResponse, error) {
values := url.Values{}
values.Set("username", username)
values.Set("to", to)
Expand All @@ -117,10 +116,10 @@ linkID, retryDurationInHours string, bulkMode int) (*SendMessageResponse, error)
if err != nil {
return nil, err
}
defer res.Body.Close()

var smsMessageResponse SendMessageResponse
json.NewDecoder(res.Body).Decode(&smsMessageResponse)
defer res.Body.Close()

return &smsMessageResponse, nil
}
Expand All @@ -136,10 +135,10 @@ func (service Service) FetchMessage(username, lastReceivedID string) (*FetchMess
if err != nil {
return nil, fmt.Errorf("could not get response: %v", err)
}
defer res.Body.Close()

var fmr FetchMessageResponse
json.NewDecoder(res.Body).Decode(&fmr)
defer res.Body.Close()

return &fmr, nil
}
Expand Down
36 changes: 23 additions & 13 deletions util/utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package util

import "fmt"

// Constants for responses
const (
PendingConfirmation = "PendingConfirmation"
PendingValidation = "PendingValidation"
Expand All @@ -13,56 +16,63 @@ const (

// GetAPIHost returns either sandbox or prod
func GetAPIHost(env string) string {
if env == "sandbox" {
return "https://api.sandbox.africastalking.com"
}
// env == "production"
return "https://api.africastalking.com"
return getHost(env, "api")
}

// GetSmsURL is the sms endpoint
func GetSmsURL(env string) string {
return GetAPIHost(env) + "/version1/messaging"
}

// GetPaymentHost returns the payments domain
func GetPaymentHost(env string) string {
if env == "sandbox" {
return "https://payments.sandbox.africastalking.com"
}
return "https://payments.africastalking.com"
return getHost(env, "payments")
}

// GetVoiceHost returns the voice host domain
func GetVoiceHost(env string) string {
if env == "sandbox" {
return "https://voice.sandbox.africastalking.com"
}
return "https://voice.africastalking.com"
return getHost(env, "voice")
}

// GetVoiceUrl returns the voice endpoint
func GetVoiceUrl(env string) string {
return GetVoiceHost(env)
}

// GetSubscriptionUrl returns the Subscription endpoint
func GetSubscriptionUrl(env string) string {
return GetAPIHost(env) + "/version1/subscription"
}

// GetUserDataUrl returns the user data endpoint
func GetUserDataUrl(env string) string {
return GetAPIHost(env) + "/version1/user"
}

// GetAirtimeUrl returns the airtime endpoint
func GetAirtimeUrl(env string) string {
return GetAPIHost(env) + "/version1/airtime"
}

// GetMobilePaymentCheckoutUrl returns the mobile payments checkout endpoint
func GetMobilePaymentCheckoutUrl(env string) string {
return GetPaymentHost(env) + "/mobile/checkout/request"
}

// GetMobilePaymentB2BUrl returns the Mobile Payments B2B endpoint
func GetMobilePaymentB2BUrl(env string) string {
return GetPaymentHost(env) + "/mobile/b2b/request"
}

// GetMobilePaymentB2CUrl returns the Mobile Payment B2C endpoint
func GetMobilePaymentB2CUrl(env string) string {
return GetPaymentHost(env) + "/mobile/b2c/request"
}

func getHost(env, service string) string {
if env != "sandbox" {
return fmt.Sprintf("https://%s.africastalking.com", service)
}
return fmt.Sprintf("https://%s.sandbox.africastalking.com", service)

}

0 comments on commit acf1d9a

Please sign in to comment.