-
Notifications
You must be signed in to change notification settings - Fork 59
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 #139 from pcatalini/DAITDS-139_Applications_support
DAITDS-139 applications support
- Loading branch information
Showing
7 changed files
with
552 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package mockns1 | ||
|
||
import ( | ||
"gopkg.in/ns1/ns1-go.v2/rest/model/pulsar" | ||
"net/http" | ||
) | ||
|
||
// AddApplicationTestCase sets up a test case for the api.Client.application.List() | ||
// function | ||
func (s *Service) AddApplicationTestCase( | ||
requestHeaders, responseHeaders http.Header, | ||
response []*pulsar.Application, | ||
) error { | ||
return s.AddTestCase( | ||
http.MethodGet, "/pulsar/apps", http.StatusOK, requestHeaders, | ||
responseHeaders, "", response, | ||
) | ||
} | ||
|
||
// AddApplicationGetTestCase sets up a test case for the api.Client.application.Get() | ||
// function | ||
func (s *Service) AddApplicationGetTestCase( | ||
id string, | ||
requestHeaders, responseHeaders http.Header, | ||
response *pulsar.Application, | ||
) error { | ||
return s.AddTestCase( | ||
http.MethodGet, "/pulsar/apps/"+id, http.StatusOK, requestHeaders, | ||
responseHeaders, "", response, | ||
) | ||
} | ||
|
||
// AddApplicationCreateTestCase sets up a test case for the api.Client.application.Create() | ||
// function | ||
func (s *Service) AddApplicationCreateTestCase( | ||
requestHeaders, responseHeaders http.Header, | ||
application, response *pulsar.Application, | ||
) error { | ||
return s.AddTestCase( | ||
http.MethodPut, "/pulsar/apps", http.StatusCreated, requestHeaders, | ||
responseHeaders, application, response, | ||
) | ||
} | ||
|
||
// AddApplicationUpdateTestCase sets up a test case for the api.Client.application.Update() | ||
// function | ||
func (s *Service) AddApplicationUpdateTestCase( | ||
requestHeaders, responseHeaders http.Header, | ||
application, response *pulsar.Application, | ||
) error { | ||
return s.AddTestCase( | ||
http.MethodPost, "/pulsar/apps/"+application.ID, http.StatusOK, requestHeaders, | ||
responseHeaders, application, response, | ||
) | ||
} | ||
|
||
// AddApplicationDeleteTestCase sets up a test case for the api.Client.application.Delete() | ||
// function | ||
func (s *Service) AddApplicationDeleteTestCase( | ||
id string, requestHeaders, responseHeaders http.Header, | ||
) error { | ||
return s.AddTestCase( | ||
http.MethodDelete, "/pulsar/apps/"+id, http.StatusNoContent, requestHeaders, | ||
responseHeaders, "", "", | ||
) | ||
} |
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,125 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"gopkg.in/ns1/ns1-go.v2/rest/model/pulsar" | ||
"log" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
api "gopkg.in/ns1/ns1-go.v2/rest" | ||
) | ||
|
||
var client *api.Client | ||
|
||
// Helper that initializes rest api client from environment variable. | ||
func init() { | ||
k := os.Getenv("NS1_APIKEY") | ||
if k == "" { | ||
fmt.Println("NS1_APIKEY environment variable is not set, giving up") | ||
} | ||
|
||
httpClient := &http.Client{Timeout: time.Second * 10} | ||
// Adds logging to each http request. | ||
doer := api.Decorate(httpClient, api.Logging(log.New(os.Stdout, "", log.LstdFlags))) | ||
client = api.NewClient(doer, api.SetAPIKey(k)) | ||
} | ||
|
||
func main() { | ||
// create applications | ||
var app *pulsar.Application | ||
var apps []*pulsar.Application | ||
for i := 0; i < 2; i++ { | ||
app = pulsar.NewApplication("MyAPP_" + strconv.Itoa(i)) | ||
app.DefaultConfig = pulsar.DefaultConfig{ | ||
Http: false, | ||
Https: false, | ||
RequestTimeoutMillis: 100 + 10*i, | ||
JobTimeoutMillis: 100 + 10*i, | ||
UseXhr: false, | ||
StaticValues: false, | ||
} | ||
app.Active = false | ||
app.BrowserWaitMillis = 100 | ||
app.JobsPerTransaction = 100 | ||
|
||
_, err := client.Applications.Create(app) | ||
if err != nil { | ||
panic("could not create " + app.Name) | ||
return | ||
} | ||
apps = append(apps, app) | ||
} | ||
|
||
// list all | ||
err := printAllApplications() | ||
if err != nil { | ||
return | ||
} | ||
|
||
// print all by id | ||
err = printAllByID(apps) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// update | ||
for i, v := range apps { | ||
v.Name = "MyAPP_Updated_" + strconv.Itoa(i) | ||
_, err = client.Applications.Update(v) | ||
|
||
if err != nil { | ||
panic("could not get application" + err.Error()) | ||
return | ||
} | ||
} | ||
|
||
// print all by id | ||
err = printAllByID(apps) | ||
if err != nil { | ||
return | ||
} | ||
|
||
//delete all | ||
for _, v := range apps { | ||
_, err := client.Applications.Delete(v.ID) | ||
if err != nil { | ||
panic("could not delete " + v.ID + " " + err.Error()) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func printAllApplications() error { | ||
appsList, _, err := client.Applications.List() | ||
fmt.Println("\n#### printing all ####") | ||
if err != nil { | ||
panic("could not list all applications " + err.Error()) | ||
return err | ||
} | ||
for _, v := range appsList { | ||
fmt.Println("%w", v) | ||
} | ||
fmt.Println("") | ||
return nil | ||
} | ||
|
||
func printAllByID(apps []*pulsar.Application) error { | ||
var appsReturned []*pulsar.Application | ||
for _, v := range apps { | ||
app, _, err := client.Applications.Get(v.ID) | ||
if err != nil { | ||
panic("could not get application" + err.Error()) | ||
return err | ||
} | ||
appsReturned = append(appsReturned, app) | ||
} | ||
fmt.Println("\n#### printing all by id ####") | ||
for _, v := range appsReturned { | ||
fmt.Println("%w", v) | ||
} | ||
fmt.Println("") | ||
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,124 @@ | ||
package rest | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"gopkg.in/ns1/ns1-go.v2/rest/model/pulsar" | ||
) | ||
|
||
// ApplicationsService handles 'pulsar/apps/' endpoint. | ||
type ApplicationsService service | ||
|
||
// List returns all pulsar Applications | ||
// | ||
// NS1 API docs: https://ns1.com/api#get-list-pulsar-applications | ||
func (s *ApplicationsService) List() ([]*pulsar.Application, *http.Response, error) { | ||
req, err := s.client.NewRequest("GET", "pulsar/apps", nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
var al []*pulsar.Application | ||
resp, err := s.client.Do(req, &al) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return al, resp, nil | ||
} | ||
|
||
// Get takes a application id and returns application struct. | ||
// | ||
// NS1 API docs: https://ns1.com/api#get-list-pulsar-applications | ||
func (s *ApplicationsService) Get(id string) (*pulsar.Application, *http.Response, error) { | ||
path := fmt.Sprintf("pulsar/apps/%s", id) | ||
|
||
req, err := s.client.NewRequest("GET", path, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
var a pulsar.Application | ||
resp, err := s.client.Do(req, &a) | ||
if err != nil { | ||
switch err.(type) { | ||
case *Error: | ||
if err.(*Error).Resp.StatusCode == 404 { | ||
return nil, resp, ErrApplicationMissing | ||
} | ||
} | ||
return nil, resp, err | ||
} | ||
|
||
return &a, resp, nil | ||
} | ||
|
||
// Create takes a *pulsar.Application and creates a new Application. | ||
// | ||
// The given application must have at least the name | ||
// NS1 API docs: https://ns1.com/api#put-create-a-pulsar-application | ||
func (s *ApplicationsService) Create(a *pulsar.Application) (*http.Response, error) { | ||
req, err := s.client.NewRequest("PUT", "pulsar/apps", a) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := s.client.Do(req, a) | ||
return resp, err | ||
} | ||
|
||
// Update takes a *pulsar.Application and updates the application with same id on Ns1. | ||
// | ||
// NS1 API docs: https://ns1.com/api#post-modify-an-application | ||
func (s *ApplicationsService) Update(a *pulsar.Application) (*http.Response, error) { | ||
path := fmt.Sprintf("pulsar/apps/%s", a.ID) | ||
|
||
req, err := s.client.NewRequest("POST", path, &a) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := s.client.Do(req, &a) | ||
if err != nil { | ||
switch err.(type) { | ||
case *Error: | ||
if err.(*Error).Resp.StatusCode == 404 { | ||
return resp, ErrApplicationMissing | ||
} | ||
} | ||
return resp, err | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
// Delete takes a application Id, and removes an existing application | ||
// | ||
// NS1 API docs: https://ns1.com/api#delete-delete-a-pulsar-application | ||
func (s *ApplicationsService) Delete(id string) (*http.Response, error) { | ||
path := fmt.Sprintf("pulsar/apps/%s", id) | ||
|
||
req, err := s.client.NewRequest("DELETE", path, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := s.client.Do(req, nil) | ||
if err != nil { | ||
switch err.(type) { | ||
case *Error: | ||
if err.(*Error).Resp.StatusCode == 404 { | ||
return resp, ErrApplicationMissing | ||
} | ||
} | ||
return resp, err | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
var ( | ||
// ErrApplicationMissing bundles GET/POST/DELETE error. | ||
ErrApplicationMissing = errors.New("application does not exist") | ||
) |
Oops, something went wrong.