github.com/twilio/twilio-go@v1.20.1/rest/supersim/v1/e_sim_profiles.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Supersim
     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  // Optional parameters for the method 'CreateEsimProfile'
    27  type CreateEsimProfileParams struct {
    28  	// The URL we should call using the `callback_method` when the status of the eSIM Profile changes. At this stage of the eSIM Profile pilot, the a request to the URL will only be called when the ESimProfile resource changes from `reserving` to `available`.
    29  	CallbackUrl *string `json:"CallbackUrl,omitempty"`
    30  	// The HTTP method we should use to call `callback_url`. Can be: `GET` or `POST` and the default is POST.
    31  	CallbackMethod *string `json:"CallbackMethod,omitempty"`
    32  	// When set to `true`, a value for `Eid` does not need to be provided. Instead, when the eSIM profile is reserved, a matching ID will be generated and returned via the `matching_id` property. This identifies the specific eSIM profile that can be used by any capable device to claim and download the profile.
    33  	GenerateMatchingId *bool `json:"GenerateMatchingId,omitempty"`
    34  	// Identifier of the eUICC that will claim the eSIM Profile.
    35  	Eid *string `json:"Eid,omitempty"`
    36  }
    37  
    38  func (params *CreateEsimProfileParams) SetCallbackUrl(CallbackUrl string) *CreateEsimProfileParams {
    39  	params.CallbackUrl = &CallbackUrl
    40  	return params
    41  }
    42  func (params *CreateEsimProfileParams) SetCallbackMethod(CallbackMethod string) *CreateEsimProfileParams {
    43  	params.CallbackMethod = &CallbackMethod
    44  	return params
    45  }
    46  func (params *CreateEsimProfileParams) SetGenerateMatchingId(GenerateMatchingId bool) *CreateEsimProfileParams {
    47  	params.GenerateMatchingId = &GenerateMatchingId
    48  	return params
    49  }
    50  func (params *CreateEsimProfileParams) SetEid(Eid string) *CreateEsimProfileParams {
    51  	params.Eid = &Eid
    52  	return params
    53  }
    54  
    55  // Order an eSIM Profile.
    56  func (c *ApiService) CreateEsimProfile(params *CreateEsimProfileParams) (*SupersimV1EsimProfile, error) {
    57  	path := "/v1/ESimProfiles"
    58  
    59  	data := url.Values{}
    60  	headers := make(map[string]interface{})
    61  
    62  	if params != nil && params.CallbackUrl != nil {
    63  		data.Set("CallbackUrl", *params.CallbackUrl)
    64  	}
    65  	if params != nil && params.CallbackMethod != nil {
    66  		data.Set("CallbackMethod", *params.CallbackMethod)
    67  	}
    68  	if params != nil && params.GenerateMatchingId != nil {
    69  		data.Set("GenerateMatchingId", fmt.Sprint(*params.GenerateMatchingId))
    70  	}
    71  	if params != nil && params.Eid != nil {
    72  		data.Set("Eid", *params.Eid)
    73  	}
    74  
    75  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	defer resp.Body.Close()
    81  
    82  	ps := &SupersimV1EsimProfile{}
    83  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	return ps, err
    88  }
    89  
    90  // Fetch an eSIM Profile.
    91  func (c *ApiService) FetchEsimProfile(Sid string) (*SupersimV1EsimProfile, error) {
    92  	path := "/v1/ESimProfiles/{Sid}"
    93  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    94  
    95  	data := url.Values{}
    96  	headers := make(map[string]interface{})
    97  
    98  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  
   103  	defer resp.Body.Close()
   104  
   105  	ps := &SupersimV1EsimProfile{}
   106  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	return ps, err
   111  }
   112  
   113  // Optional parameters for the method 'ListEsimProfile'
   114  type ListEsimProfileParams struct {
   115  	// List the eSIM Profiles that have been associated with an EId.
   116  	Eid *string `json:"Eid,omitempty"`
   117  	// Find the eSIM Profile resource related to a [Sim](https://www.twilio.com/docs/iot/supersim/api/sim-resource) resource by providing the SIM SID. Will always return an array with either 1 or 0 records.
   118  	SimSid *string `json:"SimSid,omitempty"`
   119  	// List the eSIM Profiles that are in a given status.
   120  	Status *string `json:"Status,omitempty"`
   121  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   122  	PageSize *int `json:"PageSize,omitempty"`
   123  	// Max number of records to return.
   124  	Limit *int `json:"limit,omitempty"`
   125  }
   126  
   127  func (params *ListEsimProfileParams) SetEid(Eid string) *ListEsimProfileParams {
   128  	params.Eid = &Eid
   129  	return params
   130  }
   131  func (params *ListEsimProfileParams) SetSimSid(SimSid string) *ListEsimProfileParams {
   132  	params.SimSid = &SimSid
   133  	return params
   134  }
   135  func (params *ListEsimProfileParams) SetStatus(Status string) *ListEsimProfileParams {
   136  	params.Status = &Status
   137  	return params
   138  }
   139  func (params *ListEsimProfileParams) SetPageSize(PageSize int) *ListEsimProfileParams {
   140  	params.PageSize = &PageSize
   141  	return params
   142  }
   143  func (params *ListEsimProfileParams) SetLimit(Limit int) *ListEsimProfileParams {
   144  	params.Limit = &Limit
   145  	return params
   146  }
   147  
   148  // Retrieve a single page of EsimProfile records from the API. Request is executed immediately.
   149  func (c *ApiService) PageEsimProfile(params *ListEsimProfileParams, pageToken, pageNumber string) (*ListEsimProfileResponse, error) {
   150  	path := "/v1/ESimProfiles"
   151  
   152  	data := url.Values{}
   153  	headers := make(map[string]interface{})
   154  
   155  	if params != nil && params.Eid != nil {
   156  		data.Set("Eid", *params.Eid)
   157  	}
   158  	if params != nil && params.SimSid != nil {
   159  		data.Set("SimSid", *params.SimSid)
   160  	}
   161  	if params != nil && params.Status != nil {
   162  		data.Set("Status", *params.Status)
   163  	}
   164  	if params != nil && params.PageSize != nil {
   165  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   166  	}
   167  
   168  	if pageToken != "" {
   169  		data.Set("PageToken", pageToken)
   170  	}
   171  	if pageNumber != "" {
   172  		data.Set("Page", pageNumber)
   173  	}
   174  
   175  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   176  	if err != nil {
   177  		return nil, err
   178  	}
   179  
   180  	defer resp.Body.Close()
   181  
   182  	ps := &ListEsimProfileResponse{}
   183  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   184  		return nil, err
   185  	}
   186  
   187  	return ps, err
   188  }
   189  
   190  // Lists EsimProfile records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   191  func (c *ApiService) ListEsimProfile(params *ListEsimProfileParams) ([]SupersimV1EsimProfile, error) {
   192  	response, errors := c.StreamEsimProfile(params)
   193  
   194  	records := make([]SupersimV1EsimProfile, 0)
   195  	for record := range response {
   196  		records = append(records, record)
   197  	}
   198  
   199  	if err := <-errors; err != nil {
   200  		return nil, err
   201  	}
   202  
   203  	return records, nil
   204  }
   205  
   206  // Streams EsimProfile records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   207  func (c *ApiService) StreamEsimProfile(params *ListEsimProfileParams) (chan SupersimV1EsimProfile, chan error) {
   208  	if params == nil {
   209  		params = &ListEsimProfileParams{}
   210  	}
   211  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   212  
   213  	recordChannel := make(chan SupersimV1EsimProfile, 1)
   214  	errorChannel := make(chan error, 1)
   215  
   216  	response, err := c.PageEsimProfile(params, "", "")
   217  	if err != nil {
   218  		errorChannel <- err
   219  		close(recordChannel)
   220  		close(errorChannel)
   221  	} else {
   222  		go c.streamEsimProfile(response, params, recordChannel, errorChannel)
   223  	}
   224  
   225  	return recordChannel, errorChannel
   226  }
   227  
   228  func (c *ApiService) streamEsimProfile(response *ListEsimProfileResponse, params *ListEsimProfileParams, recordChannel chan SupersimV1EsimProfile, errorChannel chan error) {
   229  	curRecord := 1
   230  
   231  	for response != nil {
   232  		responseRecords := response.EsimProfiles
   233  		for item := range responseRecords {
   234  			recordChannel <- responseRecords[item]
   235  			curRecord += 1
   236  			if params.Limit != nil && *params.Limit < curRecord {
   237  				close(recordChannel)
   238  				close(errorChannel)
   239  				return
   240  			}
   241  		}
   242  
   243  		record, err := client.GetNext(c.baseURL, response, c.getNextListEsimProfileResponse)
   244  		if err != nil {
   245  			errorChannel <- err
   246  			break
   247  		} else if record == nil {
   248  			break
   249  		}
   250  
   251  		response = record.(*ListEsimProfileResponse)
   252  	}
   253  
   254  	close(recordChannel)
   255  	close(errorChannel)
   256  }
   257  
   258  func (c *ApiService) getNextListEsimProfileResponse(nextPageUrl string) (interface{}, error) {
   259  	if nextPageUrl == "" {
   260  		return nil, nil
   261  	}
   262  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   263  	if err != nil {
   264  		return nil, err
   265  	}
   266  
   267  	defer resp.Body.Close()
   268  
   269  	ps := &ListEsimProfileResponse{}
   270  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   271  		return nil, err
   272  	}
   273  	return ps, nil
   274  }