application.go raw

   1  package rest
   2  
   3  import (
   4  	"errors"
   5  	"fmt"
   6  	"net/http"
   7  
   8  	"gopkg.in/ns1/ns1-go.v2/rest/model/pulsar"
   9  )
  10  
  11  // ApplicationsService handles 'pulsar/apps/' endpoint.
  12  type ApplicationsService service
  13  
  14  // List returns all pulsar Applications
  15  //
  16  // NS1 API docs: https://ns1.com/api#get-list-pulsar-applications
  17  func (s *ApplicationsService) List() ([]*pulsar.Application, *http.Response, error) {
  18  	req, err := s.client.NewRequest("GET", "pulsar/apps", nil)
  19  	if err != nil {
  20  		return nil, nil, err
  21  	}
  22  
  23  	var al []*pulsar.Application
  24  	resp, err := s.client.Do(req, &al)
  25  	if err != nil {
  26  		return nil, resp, err
  27  	}
  28  
  29  	return al, resp, nil
  30  }
  31  
  32  // Get takes a application id and returns application struct.
  33  //
  34  // NS1 API docs: https://ns1.com/api#get-list-pulsar-applications
  35  func (s *ApplicationsService) Get(id string) (*pulsar.Application, *http.Response, error) {
  36  	path := fmt.Sprintf("pulsar/apps/%s", id)
  37  
  38  	req, err := s.client.NewRequest("GET", path, nil)
  39  	if err != nil {
  40  		return nil, nil, err
  41  	}
  42  
  43  	var a pulsar.Application
  44  	resp, err := s.client.Do(req, &a)
  45  	if err != nil {
  46  		switch err.(type) {
  47  		case *Error:
  48  			if err.(*Error).Resp.StatusCode == 404 {
  49  				return nil, resp, ErrApplicationMissing
  50  			}
  51  		}
  52  		return nil, resp, err
  53  	}
  54  
  55  	return &a, resp, nil
  56  }
  57  
  58  // Create takes a *pulsar.Application and creates a new Application.
  59  //
  60  // The given application must have at least the name
  61  // NS1 API docs: https://ns1.com/api#put-create-a-pulsar-application
  62  func (s *ApplicationsService) Create(a *pulsar.Application) (*http.Response, error) {
  63  	req, err := s.client.NewRequest("PUT", "pulsar/apps", a)
  64  	if err != nil {
  65  		return nil, err
  66  	}
  67  	resp, err := s.client.Do(req, a)
  68  	return resp, err
  69  }
  70  
  71  // Update takes a *pulsar.Application and updates the application with same id on Ns1.
  72  //
  73  // NS1 API docs: https://ns1.com/api#post-modify-an-application
  74  func (s *ApplicationsService) Update(a *pulsar.Application) (*http.Response, error) {
  75  	path := fmt.Sprintf("pulsar/apps/%s", a.ID)
  76  
  77  	req, err := s.client.NewRequest("POST", path, &a)
  78  	if err != nil {
  79  		return nil, err
  80  	}
  81  
  82  	resp, err := s.client.Do(req, &a)
  83  	if err != nil {
  84  		switch err.(type) {
  85  		case *Error:
  86  			if err.(*Error).Resp.StatusCode == 404 {
  87  				return resp, ErrApplicationMissing
  88  			}
  89  		}
  90  		return resp, err
  91  	}
  92  
  93  	return resp, nil
  94  }
  95  
  96  // Delete takes a application Id, and removes an existing application
  97  //
  98  // NS1 API docs: https://ns1.com/api#delete-delete-a-pulsar-application
  99  func (s *ApplicationsService) Delete(id string) (*http.Response, error) {
 100  	path := fmt.Sprintf("pulsar/apps/%s", id)
 101  
 102  	req, err := s.client.NewRequest("DELETE", path, nil)
 103  	if err != nil {
 104  		return nil, err
 105  	}
 106  
 107  	resp, err := s.client.Do(req, nil)
 108  	if err != nil {
 109  		switch err.(type) {
 110  		case *Error:
 111  			if err.(*Error).Resp.StatusCode == 404 {
 112  				return resp, ErrApplicationMissing
 113  			}
 114  		}
 115  		return resp, err
 116  	}
 117  
 118  	return resp, nil
 119  }
 120  
 121  var (
 122  	// ErrApplicationMissing bundles GET/POST/DELETE error.
 123  	ErrApplicationMissing = errors.New("application does not exist")
 124  )
 125