Class: ET::Client

Inherits:
CreateWSDL show all
Defined in:
lib/exact-target-api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from CreateWSDL

#stringify_keys!, #symbolize_keys!, #wsdl_file

Constructor Details

#initialize(config, options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/exact-target-api/client.rb', line 8

def initialize(config, options = {})
  load_config(config)
  symbolize_keys!(options)

  get_wsdl = options.has_key?(:wsdl) ? options[:wsdl] : true

  @debug = options[:debug]

  @path = Dir.tmpdir

  begin
    if get_wsdl
      super(@path)
    end

    if options[:jwt]
      jwt = JWT.decode(options[:jwt], @appsignature, true)
      @authToken = jwt['request']['user']['oauthToken']
      @authTokenExpiration = [Time.at(jwt['exp']), Time.now + jwt['request']['user']['expiresIn']].min
      @internalAuthToken = jwt['request']['user']['internalOauthToken']
      @refreshKey = jwt['request']['user']['refreshToken']

      self.determine_stack

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

      @auth = Savon.client(soap_header: @authObj,
                           wsdl: File.read(wsdl_file(@path)),
                           endpoint: @endpoint,
                           wsse_auth: ["*", "*"],
                           raise_errors: false,
                           log: @debug,
                           open_timeout: 180,
                           read_timeout: 180)
    end
    refresh_token
  rescue
    raise
  end

  @ready = @auth.operations.length > 0 && @status >= 200 && @status <= 400
end

Instance Attribute Details

#appsignatureObject (readonly)

Returns the value of attribute appsignature.



6
7
8
# File 'lib/exact-target-api/client.rb', line 6

def appsignature
  @appsignature
end

#authObject

Returns the value of attribute auth.



5
6
7
# File 'lib/exact-target-api/client.rb', line 5

def auth
  @auth
end

#authObjObject (readonly)

Returns the value of attribute authObj.



6
7
8
# File 'lib/exact-target-api/client.rb', line 6

def authObj
  @authObj
end

#authTokenObject

Returns the value of attribute authToken.



5
6
7
# File 'lib/exact-target-api/client.rb', line 5

def authToken
  @authToken
end

#authTokenExpirationObject (readonly)

Returns the value of attribute authTokenExpiration.



6
7
8
# File 'lib/exact-target-api/client.rb', line 6

def authTokenExpiration
  @authTokenExpiration
end

#clientIdObject (readonly)

Returns the value of attribute clientId.



6
7
8
# File 'lib/exact-target-api/client.rb', line 6

def clientId
  @clientId
end

#clientSecretObject (readonly)

Returns the value of attribute clientSecret.



6
7
8
# File 'lib/exact-target-api/client.rb', line 6

def clientSecret
  @clientSecret
end

#debugObject

Returns the value of attribute debug.



5
6
7
# File 'lib/exact-target-api/client.rb', line 5

def debug
  @debug
end

#internalAuthTokenObject (readonly)

Returns the value of attribute internalAuthToken.



6
7
8
# File 'lib/exact-target-api/client.rb', line 6

def internalAuthToken
  @internalAuthToken
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/exact-target-api/client.rb', line 6

def path
  @path
end

#readyObject

Returns the value of attribute ready.



5
6
7
# File 'lib/exact-target-api/client.rb', line 5

def ready
  @ready
end

#refreshKeyObject (readonly)

Returns the value of attribute refreshKey.



6
7
8
# File 'lib/exact-target-api/client.rb', line 6

def refreshKey
  @refreshKey
end

#soapHeaderObject (readonly)

Returns the value of attribute soapHeader.



6
7
8
# File 'lib/exact-target-api/client.rb', line 6

def soapHeader
  @soapHeader
end

#stackIDObject (readonly)

Returns the value of attribute stackID.



6
7
8
# File 'lib/exact-target-api/client.rb', line 6

def stackID
  @stackID
end

#statusObject

Returns the value of attribute status.



5
6
7
# File 'lib/exact-target-api/client.rb', line 5

def status
  @status
end

#wsdlLocObject (readonly)

Returns the value of attribute wsdlLoc.



6
7
8
# File 'lib/exact-target-api/client.rb', line 6

def wsdlLoc
  @wsdlLoc
end

Instance Method Details

#foldersObject



111
112
113
# File 'lib/exact-target-api/client.rb', line 111

def folders
  ET::Folders.new(self)
end

#listObject



103
104
105
# File 'lib/exact-target-api/client.rb', line 103

def list
  ET::List.new(self)
end

#refresh_token(force = nil) ⇒ Object Also known as: refreshToken



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/exact-target-api/client.rb', line 52

def refresh_token(force = nil)
  #If we don't already have a token or the token expires within 5 min(300 seconds), get one
  if force || @authToken.nil? || Time.now + 300 > @authTokenExpiration
    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, accessType: 'offline'}
      # Pass in the refreshKey if we have it
      if @refreshKey
        jsonPayload[:refreshToken] = @refreshKey
        jsonPayload[:scope] = "cas:#{@internalAuthToken}"
      end
      request.body = jsonPayload.to_json
      request.add_field "Content-Type", "application/json"
      tokenResponse = JSON.parse(http.request(request).body)

      if tokenResponse['accessToken'].nil?
        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["refreshToken"]
        @refreshKey = tokenResponse['refreshToken']
      end


      self.determine_stack if @endpoint.nil?

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

      @auth = Savon.client(soap_header: @authObj,
                           wsdl: File.read(wsdl_file(@path)),
                           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

#subscriberObject



107
108
109
# File 'lib/exact-target-api/client.rb', line 107

def subscriber
  ET::Subscriber.new(self)
end