data_source.go raw

   1  package rest
   2  
   3  import (
   4  	"fmt"
   5  	"net/http"
   6  
   7  	"gopkg.in/ns1/ns1-go.v2/rest/model/data"
   8  )
   9  
  10  // DataSourcesService handles 'data/sources' endpoint.
  11  type DataSourcesService service
  12  
  13  // List returns all connected data sources.
  14  //
  15  // NS1 API docs: https://ns1.com/api/#sources-get
  16  func (s *DataSourcesService) List() ([]*data.Source, *http.Response, error) {
  17  	req, err := s.client.NewRequest("GET", "data/sources", nil)
  18  	if err != nil {
  19  		return nil, nil, err
  20  	}
  21  
  22  	dsl := []*data.Source{}
  23  	resp, err := s.client.Do(req, &dsl)
  24  	if err != nil {
  25  		return nil, resp, err
  26  	}
  27  
  28  	return dsl, resp, nil
  29  }
  30  
  31  // Get takes an ID returns the details for a single data source.
  32  //
  33  // NS1 API docs: https://ns1.com/api/#sources-source-get
  34  func (s *DataSourcesService) Get(id string) (*data.Source, *http.Response, error) {
  35  	path := fmt.Sprintf("data/sources/%s", id)
  36  
  37  	req, err := s.client.NewRequest("GET", path, nil)
  38  	if err != nil {
  39  		return nil, nil, err
  40  	}
  41  
  42  	var ds data.Source
  43  	resp, err := s.client.Do(req, &ds)
  44  	if err != nil {
  45  		return nil, resp, err
  46  	}
  47  
  48  	return &ds, resp, nil
  49  }
  50  
  51  // Create takes a *DataSource and creates a new data source.
  52  //
  53  // NS1 API docs: https://ns1.com/api/#sources-put
  54  func (s *DataSourcesService) Create(ds *data.Source) (*http.Response, error) {
  55  	req, err := s.client.NewRequest("PUT", "data/sources", &ds)
  56  	if err != nil {
  57  		return nil, err
  58  	}
  59  
  60  	// Update data sources' fields with data from api(ensure consistent)
  61  	resp, err := s.client.Do(req, &ds)
  62  	if err != nil {
  63  		return resp, err
  64  	}
  65  
  66  	return resp, nil
  67  }
  68  
  69  // Update takes a *DataSource modifies basic details of a data source.
  70  // NOTE: This does not 'publish' data. See the Publish method.
  71  //
  72  // NS1 API docs: https://ns1.com/api/#sources-post
  73  func (s *DataSourcesService) Update(ds *data.Source) (*http.Response, error) {
  74  	path := fmt.Sprintf("data/sources/%s", ds.ID)
  75  	// must be omitted from the body
  76  	ds.ID = ""
  77  
  78  	req, err := s.client.NewRequest("POST", path, &ds)
  79  	if err != nil {
  80  		return nil, err
  81  	}
  82  
  83  	// Update data sources' instance fields with data from api(ensure consistent)
  84  	resp, err := s.client.Do(req, &ds)
  85  	if err != nil {
  86  		return resp, err
  87  	}
  88  
  89  	return resp, nil
  90  }
  91  
  92  // Delete takes an ID and removes an existing data source and all connected feeds from the source.
  93  //
  94  // NS1 API docs: https://ns1.com/api/#sources-delete
  95  func (s *DataSourcesService) Delete(id string) (*http.Response, error) {
  96  	path := fmt.Sprintf("data/sources/%s", id)
  97  
  98  	req, err := s.client.NewRequest("DELETE", path, nil)
  99  	if err != nil {
 100  		return nil, err
 101  	}
 102  
 103  	resp, err := s.client.Do(req, nil)
 104  	if err != nil {
 105  		return resp, err
 106  	}
 107  
 108  	return resp, nil
 109  }
 110  
 111  // Publish takes a datasources' id and data to publish.
 112  //
 113  // NS1 API docs: https://ns1.com/api/#feed-post
 114  func (s *DataSourcesService) Publish(dsID string, data interface{}) (*http.Response, error) {
 115  	path := fmt.Sprintf("feed/%s", dsID)
 116  
 117  	req, err := s.client.NewRequest("POST", path, &data)
 118  	if err != nil {
 119  		return nil, err
 120  	}
 121  
 122  	resp, err := s.client.Do(req, nil)
 123  	if err != nil {
 124  		return resp, err
 125  	}
 126  
 127  	return resp, nil
 128  }
 129