billing_usage.go raw

   1  package rest
   2  
   3  import (
   4  	"errors"
   5  	"fmt"
   6  	"net/http"
   7  	"strings"
   8  
   9  	billingusage "gopkg.in/ns1/ns1-go.v2/rest/model/billingusage"
  10  )
  11  
  12  // BillingUsageService handles 'billimg-usage/v1' endpoint.
  13  type BillingUsageService service
  14  
  15  // The base for the billing-usage api relative to /v1
  16  // client.NewRequest will call ResolveReference and remove /v1/../
  17  const billingUsageRelativeBase = "../billing-usage/v1"
  18  
  19  // GetQueries takes the timeframe input "from" and "to", returns all its queries.
  20  // NS1 API docs: https://ns1.com/api/#billing-usage-queries-get
  21  func (bu *BillingUsageService) GetQueries(from int32, to int32) (*billingusage.Queries, *http.Response, error) {
  22  	path := fmt.Sprintf("%s/%s?from=%d&to=%d", billingUsageRelativeBase, billingusage.BillingUsageQueries, from, to)
  23  	req, err := bu.client.NewRequest(http.MethodGet, path, nil)
  24  	if err != nil {
  25  		return nil, nil, err
  26  	}
  27  
  28  	var queries billingusage.Queries
  29  
  30  	resp, err := bu.client.Do(req, &queries)
  31  	if err != nil {
  32  		var clientErr *Error
  33  		switch {
  34  		case errors.As(err, &clientErr):
  35  			if strings.HasSuffix(clientErr.Message, billingusage.NotFound) {
  36  				return nil, resp, billingusage.ErrBillingUsageNotFound
  37  			}
  38  		}
  39  		return nil, resp, err
  40  	}
  41  
  42  	return &queries, resp, nil
  43  }
  44  
  45  // GetDecisions takes the timeframe input "from" and "to", returns all its decisions.
  46  // NS1 API docs: https://ns1.com/api/#billing-usage-decisions-get
  47  func (bu *BillingUsageService) GetDecisions(from int32, to int32) (*billingusage.TotalUsage, *http.Response, error) {
  48  	path := fmt.Sprintf("%s/%s?from=%d&to=%d", billingUsageRelativeBase, billingusage.BillingUsageDecisions, from, to)
  49  
  50  	req, err := bu.client.NewRequest(http.MethodGet, path, nil)
  51  	if err != nil {
  52  		return nil, nil, err
  53  	}
  54  
  55  	var decisions billingusage.TotalUsage
  56  
  57  	resp, err := bu.client.Do(req, &decisions)
  58  	if err != nil {
  59  		var clientErr *Error
  60  		switch {
  61  		case errors.As(err, &clientErr):
  62  			if strings.HasSuffix(clientErr.Message, billingusage.NotFound) {
  63  				return nil, resp, billingusage.ErrBillingUsageNotFound
  64  			}
  65  		}
  66  		return nil, resp, err
  67  	}
  68  
  69  	return &decisions, resp, nil
  70  }
  71  
  72  // GetLimits takes the timeframe input "from" and "to", returns all its limits.
  73  // NS1 API docs: https://ns1.com/api/#billing-usage-limits-get
  74  func (bu *BillingUsageService) GetLimits(from int32, to int32) (*billingusage.Limits, *http.Response, error) {
  75  	path := fmt.Sprintf("%s/%s?from=%d&to=%d", billingUsageRelativeBase, billingusage.BillingUsageLimits, from, to)
  76  
  77  	req, err := bu.client.NewRequest(http.MethodGet, path, nil)
  78  	if err != nil {
  79  		return nil, nil, err
  80  	}
  81  
  82  	var limits billingusage.Limits
  83  
  84  	resp, err := bu.client.Do(req, &limits)
  85  	if err != nil {
  86  		var clientErr *Error
  87  		switch {
  88  		case errors.As(err, &clientErr):
  89  			if strings.HasSuffix(clientErr.Message, billingusage.NotFound) {
  90  				return nil, resp, billingusage.ErrBillingUsageNotFound
  91  			}
  92  		}
  93  		return nil, resp, err
  94  	}
  95  
  96  	return &limits, resp, nil
  97  }
  98  
  99  // GetMonitors returns total no. of monitors.
 100  // NS1 API docs: https://ns1.com/api/#billing-usage-monitors-get
 101  func (bu *BillingUsageService) GetMonitors() (*billingusage.TotalUsage, *http.Response, error) {
 102  	path := fmt.Sprintf("%s/%s", billingUsageRelativeBase, billingusage.BillingUsageMonitors)
 103  
 104  	req, err := bu.client.NewRequest(http.MethodGet, path, nil)
 105  	if err != nil {
 106  		return nil, nil, err
 107  	}
 108  
 109  	var monitors billingusage.TotalUsage
 110  
 111  	resp, err := bu.client.Do(req, &monitors)
 112  	if err != nil {
 113  		var clientErr *Error
 114  		switch {
 115  		case errors.As(err, &clientErr):
 116  			if strings.HasSuffix(clientErr.Message, billingusage.NotFound) {
 117  				return nil, resp, billingusage.ErrBillingUsageNotFound
 118  			}
 119  		}
 120  		return nil, resp, err
 121  	}
 122  
 123  	return &monitors, resp, nil
 124  }
 125  
 126  // GetFilterChains returns total no. of filter-chains.
 127  // NS1 API docs: https://ns1.com/api/#billing-usage-filter-chains-get
 128  func (bu *BillingUsageService) GetFilterChains() (*billingusage.TotalUsage, *http.Response, error) {
 129  	path := fmt.Sprintf("%s/%s", billingUsageRelativeBase, billingusage.BillingUsageFilterChains)
 130  
 131  	req, err := bu.client.NewRequest(http.MethodGet, path, nil)
 132  	if err != nil {
 133  		return nil, nil, err
 134  	}
 135  
 136  	var filterChains billingusage.TotalUsage
 137  
 138  	resp, err := bu.client.Do(req, &filterChains)
 139  	if err != nil {
 140  		var clientErr *Error
 141  		switch {
 142  		case errors.As(err, &clientErr):
 143  			if strings.HasSuffix(clientErr.Message, billingusage.NotFound) {
 144  				return nil, resp, billingusage.ErrBillingUsageNotFound
 145  			}
 146  		}
 147  		return nil, resp, err
 148  	}
 149  
 150  	return &filterChains, resp, nil
 151  }
 152  
 153  // GetRecords returns total no. of records.
 154  // NS1 API docs: https://ns1.com/api/#billing-usage-records-get
 155  func (bu *BillingUsageService) GetRecords() (*billingusage.TotalUsage, *http.Response, error) {
 156  	path := fmt.Sprintf("%s/%s", billingUsageRelativeBase, billingusage.BillingUsageRecords)
 157  
 158  	req, err := bu.client.NewRequest(http.MethodGet, path, nil)
 159  	if err != nil {
 160  		return nil, nil, err
 161  	}
 162  
 163  	var records billingusage.TotalUsage
 164  
 165  	resp, err := bu.client.Do(req, &records)
 166  	if err != nil {
 167  		var clientErr *Error
 168  		switch {
 169  		case errors.As(err, &clientErr):
 170  			if strings.HasSuffix(clientErr.Message, billingusage.NotFound) {
 171  				return nil, resp, billingusage.ErrBillingUsageNotFound
 172  			}
 173  		}
 174  		return nil, resp, err
 175  	}
 176  
 177  	return &records, resp, nil
 178  }
 179