Skip to content

Commit

Permalink
Merge pull request #139 from pcatalini/DAITDS-139_Applications_support
Browse files Browse the repository at this point in the history
DAITDS-139 applications support
  • Loading branch information
Zach-Johnson authored Aug 31, 2021
2 parents 94ccad4 + 0688046 commit 051ab93
Show file tree
Hide file tree
Showing 7 changed files with 552 additions and 0 deletions.
66 changes: 66 additions & 0 deletions mockns1/application.go
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, "", "",
)
}
125 changes: 125 additions & 0 deletions rest/_examples/applications.go
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
}
124 changes: 124 additions & 0 deletions rest/application.go
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")
)
Loading

0 comments on commit 051ab93

Please sign in to comment.