github.com/twilio/twilio-go@v1.20.1/rest/numbers/v2/regulatory_compliance_regulations.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Numbers
     8   * This is the public Twilio REST API.
     9   *
    10   * NOTE: This class is auto generated by OpenAPI Generator.
    11   * https://openapi-generator.tech
    12   * Do not edit the class manually.
    13   */
    14  
    15  package openapi
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"net/url"
    21  	"strings"
    22  
    23  	"github.com/twilio/twilio-go/client"
    24  )
    25  
    26  // Fetch specific Regulation Instance.
    27  func (c *ApiService) FetchRegulation(Sid string) (*NumbersV2Regulation, error) {
    28  	path := "/v2/RegulatoryCompliance/Regulations/{Sid}"
    29  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    30  
    31  	data := url.Values{}
    32  	headers := make(map[string]interface{})
    33  
    34  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	defer resp.Body.Close()
    40  
    41  	ps := &NumbersV2Regulation{}
    42  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	return ps, err
    47  }
    48  
    49  // Optional parameters for the method 'ListRegulation'
    50  type ListRegulationParams struct {
    51  	// The type of End User the regulation requires - can be `individual` or `business`.
    52  	EndUserType *string `json:"EndUserType,omitempty"`
    53  	// The ISO country code of the phone number's country.
    54  	IsoCountry *string `json:"IsoCountry,omitempty"`
    55  	// The type of phone number that the regulatory requiremnt is restricting.
    56  	NumberType *string `json:"NumberType,omitempty"`
    57  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    58  	PageSize *int `json:"PageSize,omitempty"`
    59  	// Max number of records to return.
    60  	Limit *int `json:"limit,omitempty"`
    61  }
    62  
    63  func (params *ListRegulationParams) SetEndUserType(EndUserType string) *ListRegulationParams {
    64  	params.EndUserType = &EndUserType
    65  	return params
    66  }
    67  func (params *ListRegulationParams) SetIsoCountry(IsoCountry string) *ListRegulationParams {
    68  	params.IsoCountry = &IsoCountry
    69  	return params
    70  }
    71  func (params *ListRegulationParams) SetNumberType(NumberType string) *ListRegulationParams {
    72  	params.NumberType = &NumberType
    73  	return params
    74  }
    75  func (params *ListRegulationParams) SetPageSize(PageSize int) *ListRegulationParams {
    76  	params.PageSize = &PageSize
    77  	return params
    78  }
    79  func (params *ListRegulationParams) SetLimit(Limit int) *ListRegulationParams {
    80  	params.Limit = &Limit
    81  	return params
    82  }
    83  
    84  // Retrieve a single page of Regulation records from the API. Request is executed immediately.
    85  func (c *ApiService) PageRegulation(params *ListRegulationParams, pageToken, pageNumber string) (*ListRegulationResponse, error) {
    86  	path := "/v2/RegulatoryCompliance/Regulations"
    87  
    88  	data := url.Values{}
    89  	headers := make(map[string]interface{})
    90  
    91  	if params != nil && params.EndUserType != nil {
    92  		data.Set("EndUserType", *params.EndUserType)
    93  	}
    94  	if params != nil && params.IsoCountry != nil {
    95  		data.Set("IsoCountry", *params.IsoCountry)
    96  	}
    97  	if params != nil && params.NumberType != nil {
    98  		data.Set("NumberType", *params.NumberType)
    99  	}
   100  	if params != nil && params.PageSize != nil {
   101  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   102  	}
   103  
   104  	if pageToken != "" {
   105  		data.Set("PageToken", pageToken)
   106  	}
   107  	if pageNumber != "" {
   108  		data.Set("Page", pageNumber)
   109  	}
   110  
   111  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	defer resp.Body.Close()
   117  
   118  	ps := &ListRegulationResponse{}
   119  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	return ps, err
   124  }
   125  
   126  // Lists Regulation records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   127  func (c *ApiService) ListRegulation(params *ListRegulationParams) ([]NumbersV2Regulation, error) {
   128  	response, errors := c.StreamRegulation(params)
   129  
   130  	records := make([]NumbersV2Regulation, 0)
   131  	for record := range response {
   132  		records = append(records, record)
   133  	}
   134  
   135  	if err := <-errors; err != nil {
   136  		return nil, err
   137  	}
   138  
   139  	return records, nil
   140  }
   141  
   142  // Streams Regulation records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   143  func (c *ApiService) StreamRegulation(params *ListRegulationParams) (chan NumbersV2Regulation, chan error) {
   144  	if params == nil {
   145  		params = &ListRegulationParams{}
   146  	}
   147  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   148  
   149  	recordChannel := make(chan NumbersV2Regulation, 1)
   150  	errorChannel := make(chan error, 1)
   151  
   152  	response, err := c.PageRegulation(params, "", "")
   153  	if err != nil {
   154  		errorChannel <- err
   155  		close(recordChannel)
   156  		close(errorChannel)
   157  	} else {
   158  		go c.streamRegulation(response, params, recordChannel, errorChannel)
   159  	}
   160  
   161  	return recordChannel, errorChannel
   162  }
   163  
   164  func (c *ApiService) streamRegulation(response *ListRegulationResponse, params *ListRegulationParams, recordChannel chan NumbersV2Regulation, errorChannel chan error) {
   165  	curRecord := 1
   166  
   167  	for response != nil {
   168  		responseRecords := response.Results
   169  		for item := range responseRecords {
   170  			recordChannel <- responseRecords[item]
   171  			curRecord += 1
   172  			if params.Limit != nil && *params.Limit < curRecord {
   173  				close(recordChannel)
   174  				close(errorChannel)
   175  				return
   176  			}
   177  		}
   178  
   179  		record, err := client.GetNext(c.baseURL, response, c.getNextListRegulationResponse)
   180  		if err != nil {
   181  			errorChannel <- err
   182  			break
   183  		} else if record == nil {
   184  			break
   185  		}
   186  
   187  		response = record.(*ListRegulationResponse)
   188  	}
   189  
   190  	close(recordChannel)
   191  	close(errorChannel)
   192  }
   193  
   194  func (c *ApiService) getNextListRegulationResponse(nextPageUrl string) (interface{}, error) {
   195  	if nextPageUrl == "" {
   196  		return nil, nil
   197  	}
   198  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   199  	if err != nil {
   200  		return nil, err
   201  	}
   202  
   203  	defer resp.Body.Close()
   204  
   205  	ps := &ListRegulationResponse{}
   206  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   207  		return nil, err
   208  	}
   209  	return ps, nil
   210  }