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

     1  package srvaccount
     2  
     3  import (
     4  	_nethttp "net/http"
     5  
     6  	ciosctx "github.com/optim-corp/cios-golang-sdk/ctx"
     7  
     8  	cnv "github.com/fcfcqloow/go-advance/convert"
     9  	xmath "github.com/fcfcqloow/go-advance/math"
    10  	"github.com/optim-corp/cios-golang-sdk/cios"
    11  	"github.com/optim-corp/cios-golang-sdk/util"
    12  )
    13  
    14  func MakeGetResourceOwnersOpts() cios.ApiGetResourceOwnersRequest {
    15  	return cios.ApiGetResourceOwnersRequest{}
    16  }
    17  
    18  func (self *CiosAccount) GetResourceOwners(ctx ciosctx.RequestCtx, params cios.ApiGetResourceOwnersRequest) (response cios.MultipleResourceOwner, httpResponse *_nethttp.Response, err error) {
    19  	if err := self.refresh(); err != nil {
    20  		return cios.MultipleResourceOwner{}, nil, err
    21  	}
    22  	params.ApiService = self.ApiClient.ResourceOwnerApi
    23  	params.Ctx = self.withHost(ctx)
    24  	params.P_order = util.ToNil(params.P_order)
    25  	params.P_orderBy = util.ToNil(params.P_orderBy)
    26  	params.P_page = util.ToNil(params.P_page)
    27  	params.P_type_ = util.ToNil(params.P_type_)
    28  	params.P_userId = util.ToNil(params.P_userId)
    29  	return params.Execute()
    30  }
    31  func (self *CiosAccount) GetResourceOwnersAll(ctx ciosctx.RequestCtx, params cios.ApiGetResourceOwnersRequest) ([]cios.ResourceOwner, *_nethttp.Response, error) {
    32  	var (
    33  		result      []cios.ResourceOwner
    34  		httpRes     *_nethttp.Response
    35  		err         error
    36  		_limit      = int64(1000)
    37  		offset      = int64(0)
    38  		getFunction = func(offset int64) (cios.MultipleResourceOwner, *_nethttp.Response, error) {
    39  			return self.GetResourceOwners(ctx, params.Limit(xmath.MinInt64(_limit, 1000)).Offset(offset+cnv.MustInt64(params.P_offset)))
    40  		}
    41  	)
    42  	if params.P_limit != nil {
    43  		_limit = *params.P_limit
    44  		for {
    45  			response, httpResponse, err := getFunction(offset)
    46  			if err != nil {
    47  				return []cios.ResourceOwner{}, httpResponse, err
    48  			}
    49  			result = append(result, response.ResourceOwners...)
    50  			offset += 1000
    51  			_limit -= 1000
    52  			if _limit <= 0 {
    53  				break
    54  			}
    55  		}
    56  	} else {
    57  		response, httpResponse, err := getFunction(offset)
    58  		if err != nil {
    59  			return []cios.ResourceOwner{}, httpResponse, err
    60  		}
    61  		result = append(result, response.ResourceOwners...)
    62  		for offset = int64(1000); offset+cnv.MustInt64(params.P_offset) < response.Total; offset += 1000 {
    63  			response, httpResponse, err := getFunction(offset)
    64  			if err != nil {
    65  				return []cios.ResourceOwner{}, httpResponse, err
    66  			}
    67  			result = append(result, response.ResourceOwners...)
    68  		}
    69  	}
    70  	return result, httpRes, err
    71  }
    72  func (self *CiosAccount) GetResourceOwnersUnlimited(ctx ciosctx.RequestCtx, params cios.ApiGetResourceOwnersRequest) ([]cios.ResourceOwner, *_nethttp.Response, error) {
    73  	params.P_limit = nil
    74  	return self.GetResourceOwnersAll(ctx, params)
    75  }
    76  
    77  func (self *CiosAccount) GetResourceOwner(ctx ciosctx.RequestCtx, id string) (cios.ResourceOwner, *_nethttp.Response, error) {
    78  	if err := self.refresh(); err != nil {
    79  		return cios.ResourceOwner{}, nil, err
    80  	}
    81  	return self.ApiClient.ResourceOwnerApi.GetResourceOwner(self.withHost(ctx), id).Execute()
    82  
    83  }
    84  func (self *CiosAccount) GetResourceOwnerByGroupId(ctx ciosctx.RequestCtx, groupID string) (cios.ResourceOwner, *_nethttp.Response, error) {
    85  	resourceOwners, httpResponse, err := self.GetResourceOwners(ctx, cios.ApiGetResourceOwnersRequest{P_groupId: &groupID})
    86  	if err != nil {
    87  		return cios.ResourceOwner{}, httpResponse, err
    88  	}
    89  	return resourceOwners.ResourceOwners[0], httpResponse, err
    90  }
    91  
    92  func (self *CiosAccount) GetResourceOwnersMapByID(ctx ciosctx.RequestCtx) (map[string]cios.ResourceOwner, *_nethttp.Response, error) {
    93  	resourceOwnerMap := map[string]cios.ResourceOwner{}
    94  	resourceOwners, httpResponse, err := self.GetResourceOwners(ctx, MakeGetResourceOwnersOpts())
    95  	if err != nil {
    96  		return nil, httpResponse, err
    97  	}
    98  	for _, resourceOwner := range resourceOwners.ResourceOwners {
    99  		resourceOwnerMap[resourceOwner.Id] = resourceOwner
   100  	}
   101  	return resourceOwnerMap, httpResponse, nil
   102  
   103  }
   104  
   105  func (self *CiosAccount) GetResourceOwnersMapByGroupID(ctx ciosctx.RequestCtx) (map[string]cios.ResourceOwner, error) {
   106  	resourceOwnerMap := map[string]cios.ResourceOwner{}
   107  	resourceOwners, _, err := self.GetResourceOwners(ctx, MakeGetResourceOwnersOpts())
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  	for _, resourceOwner := range resourceOwners.ResourceOwners {
   112  		if resourceOwner.GroupId != nil {
   113  			resourceOwnerMap[*resourceOwner.GroupId] = resourceOwner
   114  		}
   115  	}
   116  	return resourceOwnerMap, nil
   117  
   118  }
   119  
   120  func ChannelsMapByResourceOwnerID(channels []cios.Channel) map[string][]cios.Channel {
   121  	result := map[string][]cios.Channel{}
   122  	for _, channel := range channels {
   123  		result[channel.ResourceOwnerId] = append(result[channel.ResourceOwnerId], channel)
   124  	}
   125  	return result
   126  }
   127  func GroupMapByResourceOwnerID(groups []cios.Group, resourceOwners []cios.ResourceOwner) map[string]cios.Group {
   128  	result := map[string]cios.Group{}
   129  	for _, ro := range resourceOwners {
   130  		for _, gp := range groups {
   131  			if ro.GroupId != nil {
   132  				if gp.Id == *ro.GroupId {
   133  					result[ro.Id] = gp
   134  				}
   135  			}
   136  		}
   137  	}
   138  	return result
   139  }
   140  func ResourceOwnerMapByGroupID(resourceOwners []cios.ResourceOwner, groups []cios.Group) map[string]cios.ResourceOwner {
   141  	result := map[string]cios.ResourceOwner{}
   142  	for _, ro := range resourceOwners {
   143  		for _, gp := range groups {
   144  			if ro.GroupId != nil {
   145  				if gp.Id == *ro.GroupId {
   146  					result[gp.Id] = ro
   147  				}
   148  			}
   149  		}
   150  	}
   151  	return result
   152  }
   153  func ResourceOwnerIDMapByChannelID(channels []cios.Channel) map[string]string {
   154  	result := map[string]string{}
   155  	for _, c := range channels {
   156  		result[c.Id] = c.ResourceOwnerId
   157  	}
   158  	return result
   159  }
   160  func BucketsMapByResourceOwnerID(buckets []cios.Bucket) map[string][]cios.Bucket {
   161  	result := map[string][]cios.Bucket{}
   162  	for _, bucket := range buckets {
   163  		result[bucket.ResourceOwnerId] = append(result[bucket.ResourceOwnerId], bucket)
   164  	}
   165  	return result
   166  }