Class: MarketingCloudSDK::ET_Client

Inherits:
ET_CreateWSDL show all
Defined in:
lib/new.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(getWSDL = true, debug = false, params = nil) ⇒ ET_Client

Returns a new instance of ET_Client.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/new.rb', line 168

def initialize(getWSDL = true, debug = false, params = nil)
  config = YAML.load_file("config.yaml")
  @clientId = config["clientid"]
  @clientSecret = config["clientsecret"]
  @appsignature = config["appsignature"]
  @wsdl = config["defaultwsdl"]
  @debug = debug

  begin
    @path = File.dirname(__FILE__)

    #make a new WSDL
    if getWSDL then
      super(@path)
    end

    if params && params.has_key?("jwt") then
      jwt = JWT.decode(params["jwt"], @appsignature, true);
      @authToken = jwt['request']['user']['oauthToken']
      @authTokenExpiration = Time.new + jwt['request']['user']['expiresIn']
      @internalAuthToken = jwt['request']['user']['internalOauthToken']
      @refreshKey = jwt['request']['user']['refreshToken']

      self.determineStack

      @authObj = {'oAuth' => {'oAuthToken' => @internalAuthToken}}
      @authObj[:attributes!] = { 'oAuth' => { 'xmlns' => 'http://exacttarget.com' }}

      myWSDL = File.read(@path + '/ExactTargetWSDL.xml')
      @auth = Savon.client(
        soap_header: @authObj,
        wsdl: myWSDL,
        endpoint: @endpoint,
        wsse_auth: ["*", "*"],
        raise_errors: false,
        log: @debug,
        open_timeout:180,
        read_timeout: 180
      )
    else
      self.refreshToken
    end

  rescue
    raise
  end

  if ((@auth.operations.length > 0) and (@status >= 200 and @status <= 400)) then
    @ready = true
  else
    @ready = false
  end
end

Instance Attribute Details

#appsignatureObject (readonly)

Returns the value of attribute appsignature.



165
166
167
# File 'lib/new.rb', line 165

def appsignature
  @appsignature
end

#authObject

Returns the value of attribute auth.



164
165
166
# File 'lib/new.rb', line 164

def auth
  @auth
end

#authObjObject (readonly)

Returns the value of attribute authObj.



165
166
167
# File 'lib/new.rb', line 165

def authObj
  @authObj
end

#authTokenObject

Returns the value of attribute authToken.



164
165
166
# File 'lib/new.rb', line 164

def authToken
  @authToken
end

#authTokenExpirationObject (readonly)

Returns the value of attribute authTokenExpiration.



165
166
167
# File 'lib/new.rb', line 165

def authTokenExpiration
  @authTokenExpiration
end

#clientIdObject (readonly)

Returns the value of attribute clientId.



165
166
167
# File 'lib/new.rb', line 165

def clientId
  @clientId
end

#clientSecretObject (readonly)

Returns the value of attribute clientSecret.



165
166
167
# File 'lib/new.rb', line 165

def clientSecret
  @clientSecret
end

#debugObject

Returns the value of attribute debug.



164
165
166
# File 'lib/new.rb', line 164

def debug
  @debug
end

#internalAuthTokenObject (readonly)

Returns the value of attribute internalAuthToken.



165
166
167
# File 'lib/new.rb', line 165

def internalAuthToken
  @internalAuthToken
end

#pathObject (readonly)

Returns the value of attribute path.



165
166
167
# File 'lib/new.rb', line 165

def path
  @path
end

#readyObject

Returns the value of attribute ready.



164
165
166
# File 'lib/new.rb', line 164

def ready
  @ready
end

#refreshKeyObject (readonly)

Returns the value of attribute refreshKey.



165
166
167
# File 'lib/new.rb', line 165

def refreshKey
  @refreshKey
end

#soapHeaderObject (readonly)

Returns the value of attribute soapHeader.



165
166
167
# File 'lib/new.rb', line 165

def soapHeader
  @soapHeader
end

#stackIDObject (readonly)

Returns the value of attribute stackID.



165
166
167
# File 'lib/new.rb', line 165

def stackID
  @stackID
end

#statusObject

Returns the value of attribute status.



164
165
166
# File 'lib/new.rb', line 164

def status
  @status
end

#wsdlLocObject (readonly)

Returns the value of attribute wsdlLoc.



165
166
167
# File 'lib/new.rb', line 165

def wsdlLoc
  @wsdlLoc
end

Instance Method Details

#AddSubscriberToList(emailAddress, listIDs, subscriberKey = nil) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/new.rb', line 279

def AddSubscriberToList(emailAddress, listIDs, subscriberKey = nil)
  newSub = ET_Subscriber.new
  newSub.authStub = self
  lists = []

  listIDs.each{ |p|
    lists.push({"ID"=> p})
  }

  newSub.props = {"EmailAddress" => emailAddress, "Lists" => lists}
  if !subscriberKey.nil? then
    newSub.props['SubscriberKey']  = subscriberKey;
  end

  # Try to add the subscriber
  postResponse = newSub.post

  if postResponse.status == false then
    # If the subscriber already exists in the account then we need to do an update.
    # Update Subscriber On List
    if postResponse.results[0][:error_code] == "12014" then
      patchResponse = newSub.patch
      return patchResponse
    end
  end
  return postResponse
end

#CreateDataExtensions(dataExtensionDefinitions) ⇒ Object



307
308
309
310
311
312
313
314
315
# File 'lib/new.rb', line 307

def CreateDataExtensions(dataExtensionDefinitions)
  newDEs = ET_DataExtension.new
  newDEs.authStub = self

  newDEs.props = dataExtensionDefinitions
  postResponse = newDEs.post

  return postResponse
end

#refreshToken(force = nil) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/new.rb', line 226

def refreshToken(force = nil)
  #If we don't already have a token or the token expires within 5 min(300 seconds), get one
  if ((@authToken.nil? || Time.new + 300 > @authTokenExpiration) || force) then
    begin
      uri = URI.parse("https://auth.exacttargetapis.com/v1/requestToken?legacy=1")
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      request = Net::HTTP::Post.new(uri.request_uri)
      jsonPayload = {'clientId' => @clientId, 'clientSecret' => @clientSecret}

      #Pass in the refreshKey if we have it
      if @refreshKey then
        jsonPayload['refreshToken'] = @refreshKey
      end

      request.body = jsonPayload.to_json
      request.add_field "Content-Type", "application/json"
      tokenResponse = JSON.parse(http.request(request).body)

      if !tokenResponse.has_key?('accessToken') then
        raise 'Unable to validate App Keys(ClientID/ClientSecret) provided: ' + http.request(request).body
      end

      @authToken = tokenResponse['accessToken']
      @authTokenExpiration = Time.new + tokenResponse['expiresIn']
      @internalAuthToken = tokenResponse['legacyToken']
      if tokenResponse.has_key?("refreshToken") then
        @refreshKey = tokenResponse['refreshToken']
      end

      if @endpoint.nil? then
        self.determineStack
      end

      @authObj = {'oAuth' => {'oAuthToken' => @internalAuthToken}}
      @authObj[:attributes!] = { 'oAuth' => { 'xmlns' => 'http://exacttarget.com' }}

      myWSDL = File.read(@path + '/ExactTargetWSDL.xml')
      @auth = Savon.client(
        soap_header: @authObj,
        wsdl: myWSDL,
        endpoint: @endpoint,
        wsse_auth: ["*", "*"],
        raise_errors: false,
        log: @debug
      )

    rescue Exception => e
      raise 'Unable to validate App Keys(ClientID/ClientSecret) provided: ' + e.message
    end
  end
end