11from __future__ import absolute_import , division , print_function , \
22 unicode_literals
33from splitapiclient .resources .base_resource import BaseResource
4- from splitapiclient .resources .attribute import Attribute
5- from splitapiclient .resources .identity import Identity
6- from splitapiclient .util .exceptions import ClientRequiredError
7-
4+ from splitapiclient .util .helpers import as_dict , require_client
85
96class TrafficType (BaseResource ):
107 '''
@@ -50,13 +47,7 @@ def fetch_attributes(self, apiclient=None):
5047 :returns: List of attributes associated with this traffic type
5148 :rtype: list(Attribute)
5249 '''
53- if apiclient is not None :
54- amc = apiclient .attributes
55- elif self ._client is not None :
56- from splitapiclient .microclients import AttributeMicroClient
57- amc = AttributeMicroClient (self ._client )
58- else :
59- raise ClientRequiredError ('An AttributeMicroClient is required' )
50+ amc = require_client ('Attribute' , self ._client , apiclient )
6051 return amc .list (self .id )
6152
6253 def add_attribute (self , data , apiclient = None ):
@@ -71,17 +62,9 @@ def add_attribute(self, data, apiclient=None):
7162 :returns: Newly created attribute
7263 :rtype: Attribute
7364 '''
74- if apiclient is not None :
75- amc = apiclient .attributes
76- elif self ._client is not None :
77- from splitapiclient .microclients import AttributeMicroClient
78- amc = AttributeMicroClient (self ._client )
79- else :
80- raise ClientRequiredError ('An AttributeMicroClient is required' )
81-
82- attribute = data .to_dict () if isinstance (data , Attribute ) else data
83- if not attribute .get ('trafficTypeId' ):
84- attribute ['trafficTypeId' ] = self .id
65+ amc = require_client ('Attribute' , self ._client , apiclient )
66+ attribute = as_dict (data )
67+ attribute ['trafficTypeId' ] = self .id
8568 return amc .save (attribute )
8669
8770 def add_identity (self , data , apiclient = None ):
@@ -96,17 +79,9 @@ def add_identity(self, data, apiclient=None):
9679 :returns: newly created Identity
9780 :rtype: Identity
9881 '''
99- if apiclient is not None :
100- imc = apiclient .identities
101- elif self ._client is not None :
102- from splitapiclient .microclients import IdentityMicroClient
103- imc = IdentityMicroClient (self ._client )
104- else :
105- raise ClientRequiredError ('An IdentityMicroClient is required' )
106-
107- identity = data .to_dict () if isinstance (data , Identity ) else data
108- if not identity .get ('trafficTypeId' ):
109- identity ['trafficTypeId' ] = self .id
82+ imc = require_client ('Identity' , self ._client , apiclient )
83+ identity = as_dict (data )
84+ identity ['trafficTypeId' ] = self .id
11085 return imc .save (identity )
11186
11287 def add_identities (self , data , apiclient = None ):
@@ -124,20 +99,8 @@ def add_identities(self, data, apiclient=None):
12499 for the failed item togegther with a status code and a message
125100 :rtype: tuple
126101 '''
127- if apiclient is not None :
128- imc = apiclient .identities
129- elif self ._client is not None :
130- from splitapiclient .microclients .identity_microclient import IdentityMicroClient
131- imc = IdentityMicroClient (self ._client )
132- else :
133- raise ClientRequiredError ('An IdentityMicroClient is required' )
134-
135- identities = [
136- i .to_dict () if isinstance (i , Identity ) else i
137- for i in data
138- ]
102+ imc = require_client ('Identity' , self ._client , apiclient )
103+ identities = [as_dict (i ) for i in data ]
139104 for item in identities :
140- if not item .get ('trafficTypeId' ):
141- item ['trafficTypeId' ] = self .id
105+ item ['trafficTypeId' ] = self .id
142106 return imc .save_all (identities )
143-
0 commit comments