github.com/twilio/twilio-go@v1.20.1/rest/sync/v1/services_documents.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Sync
     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 'CreateDocument'
    27  type CreateDocumentParams struct {
    28  	// An application-defined string that uniquely identifies the Sync Document
    29  	UniqueName *string `json:"UniqueName,omitempty"`
    30  	// A JSON string that represents an arbitrary, schema-less object that the Sync Document stores. Can be up to 16 KiB in length.
    31  	Data *interface{} `json:"Data,omitempty"`
    32  	// How long, [in seconds](https://www.twilio.com/docs/sync/limits#sync-payload-limits), before the Sync Document expires and is deleted (the Sync Document's time-to-live).
    33  	Ttl *int `json:"Ttl,omitempty"`
    34  }
    35  
    36  func (params *CreateDocumentParams) SetUniqueName(UniqueName string) *CreateDocumentParams {
    37  	params.UniqueName = &UniqueName
    38  	return params
    39  }
    40  func (params *CreateDocumentParams) SetData(Data interface{}) *CreateDocumentParams {
    41  	params.Data = &Data
    42  	return params
    43  }
    44  func (params *CreateDocumentParams) SetTtl(Ttl int) *CreateDocumentParams {
    45  	params.Ttl = &Ttl
    46  	return params
    47  }
    48  
    49  //
    50  func (c *ApiService) CreateDocument(ServiceSid string, params *CreateDocumentParams) (*SyncV1Document, error) {
    51  	path := "/v1/Services/{ServiceSid}/Documents"
    52  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    53  
    54  	data := url.Values{}
    55  	headers := make(map[string]interface{})
    56  
    57  	if params != nil && params.UniqueName != nil {
    58  		data.Set("UniqueName", *params.UniqueName)
    59  	}
    60  	if params != nil && params.Data != nil {
    61  		v, err := json.Marshal(params.Data)
    62  
    63  		if err != nil {
    64  			return nil, err
    65  		}
    66  
    67  		data.Set("Data", string(v))
    68  	}
    69  	if params != nil && params.Ttl != nil {
    70  		data.Set("Ttl", fmt.Sprint(*params.Ttl))
    71  	}
    72  
    73  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	defer resp.Body.Close()
    79  
    80  	ps := &SyncV1Document{}
    81  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	return ps, err
    86  }
    87  
    88  //
    89  func (c *ApiService) DeleteDocument(ServiceSid string, Sid string) error {
    90  	path := "/v1/Services/{ServiceSid}/Documents/{Sid}"
    91  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    92  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    93  
    94  	data := url.Values{}
    95  	headers := make(map[string]interface{})
    96  
    97  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	defer resp.Body.Close()
   103  
   104  	return nil
   105  }
   106  
   107  //
   108  func (c *ApiService) FetchDocument(ServiceSid string, Sid string) (*SyncV1Document, error) {
   109  	path := "/v1/Services/{ServiceSid}/Documents/{Sid}"
   110  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   111  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   112  
   113  	data := url.Values{}
   114  	headers := make(map[string]interface{})
   115  
   116  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	defer resp.Body.Close()
   122  
   123  	ps := &SyncV1Document{}
   124  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   125  		return nil, err
   126  	}
   127  
   128  	return ps, err
   129  }
   130  
   131  // Optional parameters for the method 'ListDocument'
   132  type ListDocumentParams struct {
   133  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   134  	PageSize *int `json:"PageSize,omitempty"`
   135  	// Max number of records to return.
   136  	Limit *int `json:"limit,omitempty"`
   137  }
   138  
   139  func (params *ListDocumentParams) SetPageSize(PageSize int) *ListDocumentParams {
   140  	params.PageSize = &PageSize
   141  	return params
   142  }
   143  func (params *ListDocumentParams) SetLimit(Limit int) *ListDocumentParams {
   144  	params.Limit = &Limit
   145  	return params
   146  }
   147  
   148  // Retrieve a single page of Document records from the API. Request is executed immediately.
   149  func (c *ApiService) PageDocument(ServiceSid string, params *ListDocumentParams, pageToken, pageNumber string) (*ListDocumentResponse, error) {
   150  	path := "/v1/Services/{ServiceSid}/Documents"
   151  
   152  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   153  
   154  	data := url.Values{}
   155  	headers := make(map[string]interface{})
   156  
   157  	if params != nil && params.PageSize != nil {
   158  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   159  	}
   160  
   161  	if pageToken != "" {
   162  		data.Set("PageToken", pageToken)
   163  	}
   164  	if pageNumber != "" {
   165  		data.Set("Page", pageNumber)
   166  	}
   167  
   168  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   169  	if err != nil {
   170  		return nil, err
   171  	}
   172  
   173  	defer resp.Body.Close()
   174  
   175  	ps := &ListDocumentResponse{}
   176  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   177  		return nil, err
   178  	}
   179  
   180  	return ps, err
   181  }
   182  
   183  // Lists Document records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   184  func (c *ApiService) ListDocument(ServiceSid string, params *ListDocumentParams) ([]SyncV1Document, error) {
   185  	response, errors := c.StreamDocument(ServiceSid, params)
   186  
   187  	records := make([]SyncV1Document, 0)
   188  	for record := range response {
   189  		records = append(records, record)
   190  	}
   191  
   192  	if err := <-errors; err != nil {
   193  		return nil, err
   194  	}
   195  
   196  	return records, nil
   197  }
   198  
   199  // Streams Document records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   200  func (c *ApiService) StreamDocument(ServiceSid string, params *ListDocumentParams) (chan SyncV1Document, chan error) {
   201  	if params == nil {
   202  		params = &ListDocumentParams{}
   203  	}
   204  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   205  
   206  	recordChannel := make(chan SyncV1Document, 1)
   207  	errorChannel := make(chan error, 1)
   208  
   209  	response, err := c.PageDocument(ServiceSid, params, "", "")
   210  	if err != nil {
   211  		errorChannel <- err
   212  		close(recordChannel)
   213  		close(errorChannel)
   214  	} else {
   215  		go c.streamDocument(response, params, recordChannel, errorChannel)
   216  	}
   217  
   218  	return recordChannel, errorChannel
   219  }
   220  
   221  func (c *ApiService) streamDocument(response *ListDocumentResponse, params *ListDocumentParams, recordChannel chan SyncV1Document, errorChannel chan error) {
   222  	curRecord := 1
   223  
   224  	for response != nil {
   225  		responseRecords := response.Documents
   226  		for item := range responseRecords {
   227  			recordChannel <- responseRecords[item]
   228  			curRecord += 1
   229  			if params.Limit != nil && *params.Limit < curRecord {
   230  				close(recordChannel)
   231  				close(errorChannel)
   232  				return
   233  			}
   234  		}
   235  
   236  		record, err := client.GetNext(c.baseURL, response, c.getNextListDocumentResponse)
   237  		if err != nil {
   238  			errorChannel <- err
   239  			break
   240  		} else if record == nil {
   241  			break
   242  		}
   243  
   244  		response = record.(*ListDocumentResponse)
   245  	}
   246  
   247  	close(recordChannel)
   248  	close(errorChannel)
   249  }
   250  
   251  func (c *ApiService) getNextListDocumentResponse(nextPageUrl string) (interface{}, error) {
   252  	if nextPageUrl == "" {
   253  		return nil, nil
   254  	}
   255  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   256  	if err != nil {
   257  		return nil, err
   258  	}
   259  
   260  	defer resp.Body.Close()
   261  
   262  	ps := &ListDocumentResponse{}
   263  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   264  		return nil, err
   265  	}
   266  	return ps, nil
   267  }
   268  
   269  // Optional parameters for the method 'UpdateDocument'
   270  type UpdateDocumentParams struct {
   271  	// The If-Match HTTP request header
   272  	IfMatch *string `json:"If-Match,omitempty"`
   273  	// A JSON string that represents an arbitrary, schema-less object that the Sync Document stores. Can be up to 16 KiB in length.
   274  	Data *interface{} `json:"Data,omitempty"`
   275  	// How long, [in seconds](https://www.twilio.com/docs/sync/limits#sync-payload-limits), before the Sync Document expires and is deleted (time-to-live).
   276  	Ttl *int `json:"Ttl,omitempty"`
   277  }
   278  
   279  func (params *UpdateDocumentParams) SetIfMatch(IfMatch string) *UpdateDocumentParams {
   280  	params.IfMatch = &IfMatch
   281  	return params
   282  }
   283  func (params *UpdateDocumentParams) SetData(Data interface{}) *UpdateDocumentParams {
   284  	params.Data = &Data
   285  	return params
   286  }
   287  func (params *UpdateDocumentParams) SetTtl(Ttl int) *UpdateDocumentParams {
   288  	params.Ttl = &Ttl
   289  	return params
   290  }
   291  
   292  //
   293  func (c *ApiService) UpdateDocument(ServiceSid string, Sid string, params *UpdateDocumentParams) (*SyncV1Document, error) {
   294  	path := "/v1/Services/{ServiceSid}/Documents/{Sid}"
   295  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   296  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   297  
   298  	data := url.Values{}
   299  	headers := make(map[string]interface{})
   300  
   301  	if params != nil && params.Data != nil {
   302  		v, err := json.Marshal(params.Data)
   303  
   304  		if err != nil {
   305  			return nil, err
   306  		}
   307  
   308  		data.Set("Data", string(v))
   309  	}
   310  	if params != nil && params.Ttl != nil {
   311  		data.Set("Ttl", fmt.Sprint(*params.Ttl))
   312  	}
   313  
   314  	if params != nil && params.IfMatch != nil {
   315  		headers["If-Match"] = *params.IfMatch
   316  	}
   317  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   318  	if err != nil {
   319  		return nil, err
   320  	}
   321  
   322  	defer resp.Body.Close()
   323  
   324  	ps := &SyncV1Document{}
   325  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   326  		return nil, err
   327  	}
   328  
   329  	return ps, err
   330  }