pulsar_decisions.go raw

   1  package rest
   2  
   3  import (
   4  	"fmt"
   5  	"net/http"
   6  	"net/url"
   7  	"strconv"
   8  	"strings"
   9  
  10  	"gopkg.in/ns1/ns1-go.v2/rest/model/pulsar"
  11  )
  12  
  13  // PulsarDecisionsService handles 'pulsar/apps/APPID/jobs/JOBID/decisions' endpoint.
  14  type PulsarDecisionsService service
  15  
  16  // addQueryParams adds query parameters from DecisionsQueryParams to the path
  17  func addQueryParams(path string, params *pulsar.DecisionsQueryParams) string {
  18  	values := url.Values{}
  19  
  20  	if params.Start != 0 {
  21  		values.Add("start", strconv.FormatInt(params.Start, 10))
  22  	}
  23  	if params.End != 0 {
  24  		values.Add("end", strconv.FormatInt(params.End, 10))
  25  	}
  26  	if params.Period != "" {
  27  		values.Add("period", params.Period)
  28  	}
  29  	if params.Area != "" {
  30  		values.Add("area", params.Area)
  31  	}
  32  	if params.ASN != "" {
  33  		values.Add("asn", params.ASN)
  34  	}
  35  	if params.Job != "" {
  36  		values.Add("job", params.Job)
  37  	}
  38  	if len(params.Jobs) > 0 {
  39  		values.Add("jobs", strings.Join(params.Jobs, ","))
  40  	}
  41  	if params.Record != "" {
  42  		values.Add("record", params.Record)
  43  	}
  44  	if params.Result != "" {
  45  		values.Add("result", params.Result)
  46  	}
  47  	if params.Agg != "" {
  48  		values.Add("agg", params.Agg)
  49  	}
  50  	if params.Geo != "" {
  51  		values.Add("geo", params.Geo)
  52  	}
  53  	if params.ZoneID != "" {
  54  		values.Add("zone_id", params.ZoneID)
  55  	}
  56  	if params.CustomerID != 0 {
  57  		values.Add("customer_id", strconv.FormatInt(params.CustomerID, 10))
  58  	}
  59  
  60  	if len(values) > 0 {
  61  		return path + "?" + values.Encode()
  62  	}
  63  	return path
  64  }
  65  
  66  // GetDecisions retrieves decisions data with optional filtering parameters.
  67  //
  68  // NS1 API docs: https://ns1.com/api/#pulsar-decisions-analytics-get
  69  func (s *PulsarDecisionsService) GetDecisions(params *pulsar.DecisionsQueryParams) (*pulsar.DecisionsResponse, *http.Response, error) {
  70  	path := "pulsar/query/decisions"
  71  	if params != nil {
  72  		path = addQueryParams(path, params)
  73  	}
  74  
  75  	req, err := s.client.NewRequest("GET", path, nil)
  76  	if err != nil {
  77  		return nil, nil, err
  78  	}
  79  
  80  	var decisions pulsar.DecisionsResponse
  81  	resp, err := s.client.Do(req, &decisions)
  82  	if err != nil {
  83  		return nil, resp, err
  84  	}
  85  
  86  	return &decisions, resp, nil
  87  }
  88  
  89  // GetDecisionsGraphRegion retrieves regional graph data for decisions.
  90  //
  91  // NS1 API docs: https://ns1.com/api/#pulsar-decisions-graph-region-get
  92  func (s *PulsarDecisionsService) GetDecisionsGraphRegion(params *pulsar.DecisionsQueryParams) (*pulsar.DecisionsGraphRegionResponse, *http.Response, error) {
  93  	path := "pulsar/query/decisions/graph/region"
  94  	if params != nil {
  95  		path = addQueryParams(path, params)
  96  	}
  97  
  98  	req, err := s.client.NewRequest("GET", path, nil)
  99  	if err != nil {
 100  		return nil, nil, err
 101  	}
 102  
 103  	var graphRegion pulsar.DecisionsGraphRegionResponse
 104  	resp, err := s.client.Do(req, &graphRegion)
 105  	if err != nil {
 106  		return nil, resp, err
 107  	}
 108  
 109  	return &graphRegion, resp, nil
 110  }
 111  
 112  // GetDecisionsGraphTime retrieves time-series graph data for decisions.
 113  //
 114  // NS1 API docs: https://ns1.com/api/#pulsar-decisions-graph-time-get
 115  func (s *PulsarDecisionsService) GetDecisionsGraphTime(params *pulsar.DecisionsQueryParams) (*pulsar.DecisionsGraphTimeResponse, *http.Response, error) {
 116  	path := "pulsar/query/decisions/graph/time"
 117  	if params != nil {
 118  		path = addQueryParams(path, params)
 119  	}
 120  
 121  	req, err := s.client.NewRequest("GET", path, nil)
 122  	if err != nil {
 123  		return nil, nil, err
 124  	}
 125  
 126  	var graphTime pulsar.DecisionsGraphTimeResponse
 127  	resp, err := s.client.Do(req, &graphTime)
 128  	if err != nil {
 129  		return nil, resp, err
 130  	}
 131  
 132  	return &graphTime, resp, nil
 133  }
 134  
 135  // GetDecisionsArea retrieves area-based decisions data.
 136  //
 137  // NS1 API docs: https://ns1.com/api/#pulsar-decisions-area-get
 138  func (s *PulsarDecisionsService) GetDecisionsArea(params *pulsar.DecisionsQueryParams) (*pulsar.DecisionsAreaResponse, *http.Response, error) {
 139  	path := "pulsar/query/decisions/area"
 140  	if params != nil {
 141  		path = addQueryParams(path, params)
 142  	}
 143  
 144  	req, err := s.client.NewRequest("GET", path, nil)
 145  	if err != nil {
 146  		return nil, nil, err
 147  	}
 148  
 149  	var area pulsar.DecisionsAreaResponse
 150  	resp, err := s.client.Do(req, &area)
 151  	if err != nil {
 152  		return nil, resp, err
 153  	}
 154  
 155  	return &area, resp, nil
 156  }
 157  
 158  // GetDecisionsASN retrieves ASN-based decisions data.
 159  //
 160  // NS1 API docs: https://ns1.com/api/#pulsar-decisions-asn-get
 161  func (s *PulsarDecisionsService) GetDecisionsASN(params *pulsar.DecisionsQueryParams) (*pulsar.DecisionsASNResponse, *http.Response, error) {
 162  	path := "pulsar/query/decisions/asn"
 163  	if params != nil {
 164  		path = addQueryParams(path, params)
 165  	}
 166  
 167  	req, err := s.client.NewRequest("GET", path, nil)
 168  	if err != nil {
 169  		return nil, nil, err
 170  	}
 171  
 172  	var asn pulsar.DecisionsASNResponse
 173  	resp, err := s.client.Do(req, &asn)
 174  	if err != nil {
 175  		return nil, resp, err
 176  	}
 177  
 178  	return &asn, resp, nil
 179  }
 180  
 181  // GetDecisionsResultsTime retrieves time-based results data for decisions.
 182  //
 183  // NS1 API docs: https://ns1.com/api/#pulsar-decisions-results-time-get
 184  func (s *PulsarDecisionsService) GetDecisionsResultsTime(params *pulsar.DecisionsQueryParams) (*pulsar.DecisionsResultsTimeResponse, *http.Response, error) {
 185  	path := "pulsar/query/decisions/results/time"
 186  	if params != nil {
 187  		path = addQueryParams(path, params)
 188  	}
 189  
 190  	req, err := s.client.NewRequest("GET", path, nil)
 191  	if err != nil {
 192  		return nil, nil, err
 193  	}
 194  
 195  	var resultsTime pulsar.DecisionsResultsTimeResponse
 196  	resp, err := s.client.Do(req, &resultsTime)
 197  	if err != nil {
 198  		return nil, resp, err
 199  	}
 200  
 201  	return &resultsTime, resp, nil
 202  }
 203  
 204  // GetDecisionsResultsArea retrieves area-based results data for decisions.
 205  //
 206  // NS1 API docs: https://ns1.com/api/#pulsar-decisions-results-area-get
 207  func (s *PulsarDecisionsService) GetDecisionsResultsArea(params *pulsar.DecisionsQueryParams) (*pulsar.DecisionsResultsAreaResponse, *http.Response, error) {
 208  	path := "pulsar/query/decisions/results/area"
 209  	if params != nil {
 210  		path = addQueryParams(path, params)
 211  	}
 212  
 213  	req, err := s.client.NewRequest("GET", path, nil)
 214  	if err != nil {
 215  		return nil, nil, err
 216  	}
 217  
 218  	var resultsArea pulsar.DecisionsResultsAreaResponse
 219  	resp, err := s.client.Do(req, &resultsArea)
 220  	if err != nil {
 221  		return nil, resp, err
 222  	}
 223  
 224  	return &resultsArea, resp, nil
 225  }
 226  
 227  // GetFiltersTime retrieves time-based filter data for decisions.
 228  //
 229  // NS1 API docs: https://ns1.com/api/#pulsar-decisions-filters-time-get
 230  func (s *PulsarDecisionsService) GetFiltersTime(params *pulsar.DecisionsQueryParams) (*pulsar.FiltersTimeResponse, *http.Response, error) {
 231  	path := "pulsar/query/decisions/filters/time"
 232  	if params != nil {
 233  		path = addQueryParams(path, params)
 234  	}
 235  
 236  	req, err := s.client.NewRequest("GET", path, nil)
 237  	if err != nil {
 238  		return nil, nil, err
 239  	}
 240  
 241  	var filtersTime pulsar.FiltersTimeResponse
 242  	resp, err := s.client.Do(req, &filtersTime)
 243  	if err != nil {
 244  		return nil, resp, err
 245  	}
 246  
 247  	return &filtersTime, resp, nil
 248  }
 249  
 250  // GetDecisionCustomer retrieves customer-specific decision data.
 251  //
 252  // NS1 API docs: https://ns1.com/api/#pulsar-decision-customer-get
 253  func (s *PulsarDecisionsService) GetDecisionCustomer(customerID string, params *pulsar.DecisionsQueryParams) (*pulsar.DecisionCustomerResponse, *http.Response, error) {
 254  	path := fmt.Sprintf("pulsar/query/decision/customer/%s", customerID)
 255  	if params != nil {
 256  		path = addQueryParams(path, params)
 257  	}
 258  
 259  	req, err := s.client.NewRequest("GET", path, nil)
 260  	if err != nil {
 261  		return nil, nil, err
 262  	}
 263  
 264  	var customer pulsar.DecisionCustomerResponse
 265  	resp, err := s.client.Do(req, &customer)
 266  	if err != nil {
 267  		return nil, resp, err
 268  	}
 269  
 270  	return &customer, resp, nil
 271  }
 272  
 273  // GetDecisionCustomerUndetermined retrieves undetermined customer-specific decision data.
 274  //
 275  // NS1 API docs: https://ns1.com/api/#pulsar-decision-customer-undetermined-get
 276  func (s *PulsarDecisionsService) GetDecisionCustomerUndetermined(customerID string, params *pulsar.DecisionsQueryParams) (*pulsar.DecisionCustomerResponse, *http.Response, error) {
 277  	path := fmt.Sprintf("pulsar/query/decision/customer/%s/undetermined", customerID)
 278  	if params != nil {
 279  		path = addQueryParams(path, params)
 280  	}
 281  
 282  	req, err := s.client.NewRequest("GET", path, nil)
 283  	if err != nil {
 284  		return nil, nil, err
 285  	}
 286  
 287  	var customer pulsar.DecisionCustomerResponse
 288  	resp, err := s.client.Do(req, &customer)
 289  	if err != nil {
 290  		return nil, resp, err
 291  	}
 292  
 293  	return &customer, resp, nil
 294  }
 295  
 296  // GetDecisionRecord retrieves record-specific decision data.
 297  //
 298  // NS1 API docs: https://ns1.com/api/#pulsar-decision-record-get
 299  func (s *PulsarDecisionsService) GetDecisionRecord(customerID, domain, recType string, params *pulsar.DecisionsQueryParams) (*pulsar.DecisionCustomerResponse, *http.Response, error) {
 300  	path := fmt.Sprintf("pulsar/query/decision/customer/%s/record/%s/%s", customerID, domain, recType)
 301  	if params != nil {
 302  		path = addQueryParams(path, params)
 303  	}
 304  
 305  	req, err := s.client.NewRequest("GET", path, nil)
 306  	if err != nil {
 307  		return nil, nil, err
 308  	}
 309  
 310  	var record pulsar.DecisionCustomerResponse
 311  	resp, err := s.client.Do(req, &record)
 312  	if err != nil {
 313  		return nil, resp, err
 314  	}
 315  
 316  	return &record, resp, nil
 317  }
 318  
 319  // GetDecisionRecordUndetermined retrieves undetermined record-specific decision data.
 320  //
 321  // NS1 API docs: https://ns1.com/api/#pulsar-decision-record-undetermined-get
 322  func (s *PulsarDecisionsService) GetDecisionRecordUndetermined(customerID, domain, recType string, params *pulsar.DecisionsQueryParams) (*pulsar.DecisionCustomerResponse, *http.Response, error) {
 323  	path := fmt.Sprintf("pulsar/query/decision/customer/%s/record/%s/%s/undetermined", customerID, domain, recType)
 324  	if params != nil {
 325  		path = addQueryParams(path, params)
 326  	}
 327  
 328  	req, err := s.client.NewRequest("GET", path, nil)
 329  	if err != nil {
 330  		return nil, nil, err
 331  	}
 332  
 333  	var record pulsar.DecisionCustomerResponse
 334  	resp, err := s.client.Do(req, &record)
 335  	if err != nil {
 336  		return nil, resp, err
 337  	}
 338  
 339  	return &record, resp, nil
 340  }
 341  
 342  // GetDecisionTotal retrieves total decision count.
 343  //
 344  // NS1 API docs: https://ns1.com/api/#pulsar-decision-total-get
 345  func (s *PulsarDecisionsService) GetDecisionTotal(customerID string, params *pulsar.DecisionsQueryParams) (*pulsar.DecisionTotalResponse, *http.Response, error) {
 346  	path := fmt.Sprintf("pulsar/query/decision/customer/%s/total", customerID)
 347  	if params != nil {
 348  		path = addQueryParams(path, params)
 349  	}
 350  
 351  	req, err := s.client.NewRequest("GET", path, nil)
 352  	if err != nil {
 353  		return nil, nil, err
 354  	}
 355  
 356  	var total pulsar.DecisionTotalResponse
 357  	resp, err := s.client.Do(req, &total)
 358  	if err != nil {
 359  		return nil, resp, err
 360  	}
 361  
 362  	return &total, resp, nil
 363  }
 364  
 365  // GetPulsarDecisionsRecords retrieves records-based decisions data.
 366  //
 367  // NS1 API docs: https://ns1.com/api/#pulsar-decisions-records-get
 368  func (s *PulsarDecisionsService) GetPulsarDecisionsRecords(params *pulsar.DecisionsQueryParams) (*pulsar.DecisionsRecordsResponse, *http.Response, error) {
 369  	path := "pulsar/query/decisions/records"
 370  	if params != nil {
 371  		path = addQueryParams(path, params)
 372  	}
 373  
 374  	req, err := s.client.NewRequest("GET", path, nil)
 375  	if err != nil {
 376  		return nil, nil, err
 377  	}
 378  
 379  	var records pulsar.DecisionsRecordsResponse
 380  	resp, err := s.client.Do(req, &records)
 381  	if err != nil {
 382  		return nil, resp, err
 383  	}
 384  
 385  	return &records, resp, nil
 386  }
 387  
 388  // GetPulsarDecisionsResultsRecord retrieves record-based results data for decisions.
 389  //
 390  // NS1 API docs: https://ns1.com/api/#pulsar-decisions-results-record-get
 391  func (s *PulsarDecisionsService) GetPulsarDecisionsResultsRecord(params *pulsar.DecisionsQueryParams) (*pulsar.DecisionsResultsRecordResponse, *http.Response, error) {
 392  	path := "pulsar/query/decisions/results/record"
 393  	if params != nil {
 394  		path = addQueryParams(path, params)
 395  	}
 396  
 397  	req, err := s.client.NewRequest("GET", path, nil)
 398  	if err != nil {
 399  		return nil, nil, err
 400  	}
 401  
 402  	var resultsRecord pulsar.DecisionsResultsRecordResponse
 403  	resp, err := s.client.Do(req, &resultsRecord)
 404  	if err != nil {
 405  		return nil, resp, err
 406  	}
 407  
 408  	return &resultsRecord, resp, nil
 409  }
 410