Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Commit

Permalink
Mostly generated code. CreateCall working.
Browse files Browse the repository at this point in the history
  • Loading branch information
judy2k committed Apr 23, 2017
1 parent 3313a99 commit 6d34c46
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 98 deletions.
21 changes: 21 additions & 0 deletions application_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package nexmo

import "github.com/dghubble/sling"

type ApplicationService struct {
sling *sling.Sling
authSet *AuthSet
}

func newApplicationService(base *sling.Sling, authSet *AuthSet) *ApplicationService {
sling := base.Base("https://api.nexmo.com/v1/applications")
return &ApplicationService{
sling: sling,
authSet: authSet,
}
}

func (c *ApplicationService) SetBaseURL(baseURL string) {
c.sling.Base(baseURL)
}

62 changes: 24 additions & 38 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@ func NewAuthSet() *AuthSet {
return new(AuthSet)
}

func (a *AuthSet) SetAPISecret(apiKey, apiSecret string) {
a.apiSecret = &apiSecretAuth{
apiKey: apiKey,
apiSecret: apiSecret,
func (a *AuthSet) ApplyAPICredentials(request apiSecretRequest) {
a.apiSecret.apply(request)
}

func (a *AuthSet) ApplyJWT(sling *sling.Sling) error {
token, err := a.GenerateToken()
if err != nil {
return err
}
sling.Set("Authorization", fmt.Sprintf("Bearer %s", token))
return nil
}

func (a *AuthSet) GenerateToken() (string, error) {
Expand All @@ -47,6 +53,13 @@ func (a *AuthSet) GenerateToken() (string, error) {
return a.appAuth.generateToken()
}

func (a *AuthSet) SetAPISecret(apiKey, apiSecret string) {
a.apiSecret = &apiSecretAuth{
apiKey: apiKey,
apiSecret: apiSecret,
}
}

func (a *AuthSet) SetApplicationAuth(appID string, key []byte) error {
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(key)
if err != nil {
Expand All @@ -60,32 +73,8 @@ func (a *AuthSet) SetApplicationAuth(appID string, key []byte) error {
return nil
}

func (a *AuthSet) ApplyAuth(acceptableAuths []AuthType, sling *sling.Sling, request apiSecretRequest) error {
for _, acceptableAuth := range acceptableAuths {
switch acceptableAuth {
case ApiSecretAuth:
if a.apiSecret != nil {
return a.apiSecret.apply(request)
}
case ApiSecretPathAuth:
if a.apiSecret != nil {
sling.Path(fmt.Sprintf("%s/%s", a.apiSecret.apiKey, a.apiSecret.apiSecret))
}
case JwtAuth:
token, err := a.appAuth.generateToken()
if err != nil {
return err
}
if a.appAuth != nil {
sling.Set("Authorization", fmt.Sprintf("Bearer %v", token))
}
}
}
return fmt.Errorf("AuthSet not configured with credentials for %x", acceptableAuths)
}

type apiSecretRequest interface {
applyAPISecret(apiKey, apiSecret string)
setApiCredentials(apiKey, apiSecret string)
}

type apiSecretAuth struct {
Expand All @@ -94,24 +83,21 @@ type apiSecretAuth struct {
}

func (a apiSecretAuth) apply(request apiSecretRequest) error {
if request == nil {
return fmt.Errorf("cannot apply api_key and api_secret to a nil request")
}
request.applyAPISecret(a.apiKey, a.apiSecret)
request.setApiCredentials(a.apiKey, a.apiSecret)
return nil
}

type jwtClaims struct {
ApplicationID string `json:"application_id"`
jwt.StandardClaims
}

type applicationAuth struct {
appID string
privateKey *rsa.PrivateKey
r RandomProvider
}

type jwtClaims struct {
ApplicationID string `json:"application_id"`
jwt.StandardClaims
}

func (a applicationAuth) generateToken() (string, error) {
claims := jwtClaims{
a.appID,
Expand Down
21 changes: 21 additions & 0 deletions call_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package nexmo

import "github.com/dghubble/sling"

type CallService struct {
sling *sling.Sling
authSet *AuthSet
}

func newCallService(base *sling.Sling, authSet *AuthSet) *CallService {
sling := base.Base("https://api.nexmo.com/v1/calls")
return &CallService{
sling: sling,
authSet: authSet,
}
}

func (c *CallService) SetBaseURL(baseURL string) {
c.sling.Base(baseURL)
}

29 changes: 29 additions & 0 deletions calls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package nexmo

import (
"fmt"
"net/http"
)

func (c *CallErrorResponse) Error() string {
return fmt.Sprintf("%s: %s", c.ErrorTitle, c.Type)
}

func (c *CallService) CreateCall(request CreateCallRequest) (*CreateCallResponse, *http.Response, error) {
sling := c.sling.New().Post("").BodyJSON(request)
err := c.authSet.ApplyJWT(sling)
if err != nil {
return nil, nil, fmt.Errorf("%s - error applying JWT", err)
}

callResponse := new(CreateCallResponse)
errorResponse := new(CallErrorResponse)
httpResponse, err := sling.Receive(callResponse, errorResponse)
if err != nil {
return nil, httpResponse, fmt.Errorf("%s - error receiving data from server", err)
}
if errorResponse.Type != "" {
return nil, httpResponse, errorResponse
}
return callResponse, httpResponse, nil
}
21 changes: 21 additions & 0 deletions developer_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package nexmo

import "github.com/dghubble/sling"

type DeveloperService struct {
sling *sling.Sling
authSet *AuthSet
}

func newDeveloperService(base *sling.Sling, authSet *AuthSet) *DeveloperService {
sling := base.Base("https://rest.nexmo.com/account/")
return &DeveloperService{
sling: sling,
authSet: authSet,
}
}

func (c *DeveloperService) SetBaseURL(baseURL string) {
c.sling.Base(baseURL)
}

56 changes: 2 additions & 54 deletions insight.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,8 @@ import (
"net/http"

"fmt"

"github.com/dghubble/sling"
)

type InsightService struct {
sling *sling.Sling
authSet *AuthSet
}

type BasicInsightRequest struct {
Number string
Country string
Cnam *bool
}

type basicInsightJSONRequest struct {
APIKey string `json:"api_key"`
APISecret string `json:"api_secret"`
Number string `json:"number,omitempty"`
Country string `json:"country,omitempty"`
}

type BasicInsightResponse struct {
Status int64 `json:"status"`
StatusMessage string `json:"status_message,omitempty"`
ErrorText string `json:"error_text,omitempty"`
InternationalFormatNumber string `json:"international_format_number,omitempty"`
NationalFormatNumber string `json:"national_format_number,omitempty"`
CountryCode string `json:"country_code,omitempty"`
CountryCodeIso3 string `json:"country_code_iso3,omitempty"`
CountryName string `json:"country_name,omitempty"`
CountryPrefix string `json:"country_prefix,omitempty"`
}

func (r *BasicInsightResponse) responseError() error {
if r.Status != 0 {
if r.StatusMessage != "" {
Expand All @@ -48,34 +16,14 @@ func (r *BasicInsightResponse) responseError() error {
return nil
}

func newInsightService(base *sling.Sling, authSet *AuthSet) *InsightService {
sling := base.Base("https://api.nexmo.com/ni/")
return &InsightService{
sling: sling,
authSet: authSet,
}
}

func (c *InsightService) GetBasicInsight(request BasicInsightRequest) (BasicInsightResponse, *http.Response, error) {
if c.authSet.apiSecret == nil {
return BasicInsightResponse{}, nil, fmt.Errorf("Cannot call GetBasicInsight without providing APISecretAuth")
}
jsonRequest := basicInsightJSONRequest{
APIKey: c.authSet.apiSecret.apiKey,
APISecret: c.authSet.apiSecret.apiSecret,
Number: request.Number,
Country: request.Country,
}
c.authSet.ApplyAPICredentials(&request)

insightResponse := new(BasicInsightResponse)
resp, err := c.sling.New().Post("basic/json").BodyJSON(jsonRequest).ReceiveSuccess(insightResponse)
resp, err := c.sling.New().Post("basic/json").BodyJSON(request).ReceiveSuccess(insightResponse)
if err != nil {
return *insightResponse, resp, err
}
err = insightResponse.responseError()
return *insightResponse, resp, err
}

func (c *InsightService) SetBaseURL(baseURL string) {
c.sling.Base(baseURL)
}
21 changes: 21 additions & 0 deletions insight_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package nexmo

import "github.com/dghubble/sling"

type InsightService struct {
sling *sling.Sling
authSet *AuthSet
}

func newInsightService(base *sling.Sling, authSet *AuthSet) *InsightService {
sling := base.Base("https://api.nexmo.com/ni/")
return &InsightService{
sling: sling,
authSet: authSet,
}
}

func (c *InsightService) SetBaseURL(baseURL string) {
c.sling.Base(baseURL)
}

20 changes: 14 additions & 6 deletions nexmo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@ import (
)

type Client struct {
sling *sling.Sling
Insight *InsightService
//SMS *SMSService
sling *sling.Sling
Insight *InsightService
SMS *SMSService
Call *CallService
Verify *VerifyService
Developer *DeveloperService
Application *ApplicationService
}

func NewClient(httpClient *http.Client, authSet *AuthSet) *Client {
base := sling.New().
Client(httpClient).
Set("User-Agent", "nexmo-go/2.0 (judy2k)")
return &Client{
sling: base,
Insight: newInsightService(base.New(), authSet),
//SMS: newSMSService(base.New(), authSet),
sling: base,
Insight: newInsightService(base.New(), authSet),
SMS: newSMSService(base.New(), authSet),
Call: newCallService(base.New(), authSet),
Verify: newVerifyService(base.New(), authSet),
Developer: newDeveloperService(base.New(), authSet),
Application: newApplicationService(base.New(), authSet),
}
}

Expand Down
21 changes: 21 additions & 0 deletions sms_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package nexmo

import "github.com/dghubble/sling"

type SMSService struct {
sling *sling.Sling
authSet *AuthSet
}

func newSMSService(base *sling.Sling, authSet *AuthSet) *SMSService {
sling := base.Base("https://rest.nexmo.com")
return &SMSService{
sling: sling,
authSet: authSet,
}
}

func (c *SMSService) SetBaseURL(baseURL string) {
c.sling.Base(baseURL)
}

Loading

0 comments on commit 6d34c46

Please sign in to comment.