github.com/optim-corp/cios-golang-sdk@v0.5.1/sdk/service/pubsub/channel.go (about)

     1  package srvpubsub
     2  
     3  import (
     4  	"fmt"
     5  	_nethttp "net/http"
     6  
     7  	cnv "github.com/fcfcqloow/go-advance/convert"
     8  
     9  	ciosctx "github.com/optim-corp/cios-golang-sdk/ctx"
    10  	"github.com/optim-corp/cios-golang-sdk/util"
    11  
    12  	xmath "github.com/fcfcqloow/go-advance/math"
    13  
    14  	"github.com/optim-corp/cios-golang-sdk/cios"
    15  )
    16  
    17  func MakeGetChannelsOpts() cios.ApiGetChannelsRequest {
    18  	return cios.ApiGetChannelsRequest{}
    19  }
    20  
    21  func (self *CiosPubSub) GetChannels(ctx ciosctx.RequestCtx, params cios.ApiGetChannelsRequest) (response cios.MultipleChannel, httpResponse *_nethttp.Response, err error) {
    22  	if err = self.refresh(); err != nil {
    23  		return
    24  	}
    25  	params.Ctx = self.withHost(ctx)
    26  	params.ApiService = self.ApiClient.PublishSubscribeApi
    27  	params.P_name = util.ToNil(params.P_name)
    28  	params.P_label = util.ToNil(params.P_label)
    29  	params.P_order = util.ToNil(params.P_order)
    30  	params.P_orderBy = util.ToNil(params.P_orderBy)
    31  	params.P_resourceOwnerId = util.ToNil(params.P_resourceOwnerId)
    32  	params.P_isPublic = util.ToNil(params.P_isPublic)
    33  	params.P_lang = util.ToNil(params.P_lang)
    34  	params.P_channelProtocol = util.ToNil(params.P_channelProtocol)
    35  	params.P_datastoreEnabled = util.ToNil(params.P_datastoreEnabled)
    36  	params.P_messagingEnabled = util.ToNil(params.P_messagingEnabled)
    37  	params.P_messagingPersisted = util.ToNil(params.P_messagingPersisted)
    38  	return params.Execute()
    39  }
    40  func (self *CiosPubSub) GetChannelsAll(ctx ciosctx.RequestCtx, params cios.ApiGetChannelsRequest) ([]cios.Channel, *_nethttp.Response, error) {
    41  	var (
    42  		result      []cios.Channel
    43  		httpRes     *_nethttp.Response
    44  		err         error
    45  		offset      = int64(0)
    46  		_limit      = int64(1000)
    47  		getFunction = func(offset int64) (cios.MultipleChannel, *_nethttp.Response, error) {
    48  			return self.GetChannels(ctx, params.Limit(xmath.MinInt64(_limit, 1000)).Offset(offset+cnv.MustInt64(params.P_offset)))
    49  		}
    50  	)
    51  	if params.P_limit != nil {
    52  		_limit = *params.P_limit
    53  		for {
    54  			res, httpRes, err := getFunction(offset)
    55  			if err != nil {
    56  				return nil, httpRes, err
    57  			}
    58  			result = append(result, res.Channels...)
    59  			offset += 1000
    60  			_limit -= 1000
    61  			if _limit <= 0 {
    62  				break
    63  			}
    64  		}
    65  	} else {
    66  		res, httpRes, err := getFunction(offset)
    67  		if err != nil {
    68  			return nil, httpRes, err
    69  		}
    70  		result = append(result, res.Channels...)
    71  		for offset = int64(1000); offset+cnv.MustInt64(params.P_offset) < res.Total; offset += 1000 {
    72  			res, httpRes, err = getFunction(offset)
    73  			if err != nil {
    74  				return nil, httpRes, err
    75  			}
    76  			result = append(result, res.Channels...)
    77  		}
    78  	}
    79  	return result, httpRes, err
    80  }
    81  func (self *CiosPubSub) GetChannelsUnlimited(ctx ciosctx.RequestCtx, params cios.ApiGetChannelsRequest) ([]cios.Channel, *_nethttp.Response, error) {
    82  	params.P_limit = nil
    83  	return self.GetChannelsAll(ctx, params)
    84  }
    85  func (self *CiosPubSub) GetChannel(ctx ciosctx.RequestCtx, channelID string, isDev *bool, lang *string) (cios.Channel, *_nethttp.Response, error) {
    86  	if err := self.refresh(); err != nil {
    87  		return cios.Channel{}, nil, err
    88  	}
    89  	request := self.ApiClient.PublishSubscribeApi.GetChannel(self.withHost(ctx), channelID)
    90  	if isDev != nil {
    91  		request = request.IsDev(*isDev)
    92  	}
    93  	if lang != nil {
    94  		request = request.Lang(*lang)
    95  	}
    96  	response, httpResponse, err := request.Execute()
    97  	if err != nil {
    98  		return cios.Channel{}, httpResponse, err
    99  	}
   100  	return response.Channel, httpResponse, err
   101  }
   102  func (self *CiosPubSub) GetChannelFirst(ctx ciosctx.RequestCtx, params cios.ApiGetChannelsRequest) (cios.Channel, *_nethttp.Response, error) {
   103  	response, httpResponse, err := self.GetChannels(ctx, params.Limit(1))
   104  	if err != nil {
   105  		return cios.Channel{}, httpResponse, err
   106  	}
   107  	if len(response.Channels) == 0 {
   108  		return cios.Channel{}, httpResponse, fmt.Errorf("not found")
   109  	}
   110  	return response.Channels[0], httpResponse, err
   111  }
   112  func (self *CiosPubSub) GetChannelsMapByID(ctx ciosctx.RequestCtx, params cios.ApiGetChannelsRequest) (map[string]cios.Channel, *_nethttp.Response, error) {
   113  	channels, httpResponse, err := self.GetChannelsUnlimited(ctx, params)
   114  	if err != nil {
   115  		return nil, httpResponse, err
   116  	}
   117  	channelsMap := map[string]cios.Channel{}
   118  	for _, channel := range channels {
   119  		channelsMap[channel.Id] = channel
   120  	}
   121  	return channelsMap, httpResponse, nil
   122  }
   123  func (self *CiosPubSub) GetChannelsMapByResourceOwnerID(ctx ciosctx.RequestCtx, params cios.ApiGetChannelsRequest) (map[string][]cios.Channel, *_nethttp.Response, error) {
   124  	channels, httpResponse, err := self.GetChannelsUnlimited(ctx, params)
   125  	if err != nil {
   126  		return nil, httpResponse, err
   127  	}
   128  	channelsMap := map[string][]cios.Channel{}
   129  	for _, channel := range channels {
   130  		channelsMap[channel.ResourceOwnerId] = append(channelsMap[channel.Id], channel)
   131  	}
   132  	return channelsMap, httpResponse, nil
   133  }
   134  func (self *CiosPubSub) DeleteChannel(ctx ciosctx.RequestCtx, channelID string) (*_nethttp.Response, error) {
   135  	if err := self.refresh(); err != nil {
   136  		return nil, err
   137  	}
   138  	request := self.ApiClient.PublishSubscribeApi.
   139  		DeleteChannel(
   140  			self.withHost(ctx),
   141  			channelID,
   142  		)
   143  	httpResponse, err := request.Execute()
   144  	return httpResponse, err
   145  }
   146  func (self *CiosPubSub) GetOrCreateChannel(ctx ciosctx.RequestCtx, params cios.ApiGetChannelsRequest, body cios.ChannelProposal) (cios.Channel, *_nethttp.Response, error) {
   147  	channels, httpResponse, err := self.GetChannelsUnlimited(ctx, params)
   148  	if len(channels) == 0 {
   149  		return self.CreateChannel(ctx, body)
   150  	}
   151  	return channels[0], httpResponse, err
   152  }
   153  func (self *CiosPubSub) CreateChannel(ctx ciosctx.RequestCtx, body cios.ChannelProposal) (cios.Channel, *_nethttp.Response, error) {
   154  	if err := self.refresh(); err != nil {
   155  		return cios.Channel{}, nil, err
   156  	}
   157  	request := self.ApiClient.PublishSubscribeApi.CreateChannel(self.withHost(ctx)).ChannelProposal(body)
   158  	response, httpResponse, err := request.Execute()
   159  	if err != nil {
   160  		return cios.Channel{}, httpResponse, err
   161  	}
   162  	return response.Channel, httpResponse, err
   163  }
   164  func (self *CiosPubSub) UpdateChannel(ctx ciosctx.RequestCtx, channelID string, body cios.ChannelUpdateProposal) (cios.MultipleChannel, *_nethttp.Response, error) {
   165  	if err := self.refresh(); err != nil {
   166  		return cios.MultipleChannel{}, nil, err
   167  	}
   168  	return self.ApiClient.PublishSubscribeApi.UpdateChannel(self.withHost(ctx), channelID).ChannelUpdateProposal(body).Execute()
   169  }