Class: TableauServerClient::Client

Inherits:
Object
  • Object
show all
Includes:
RequestBuilder
Defined in:
lib/tableau_server_client/client.rb

Defined Under Namespace

Classes: EmptyEncoder

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestBuilder

#build_request

Constructor Details

#initialize(server_url, username, password, content_url, api_version, token_lifetime, logger, impersonation_user_id) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
26
# File 'lib/tableau_server_client/client.rb', line 17

def initialize(server_url, username, password, content_url, api_version, token_lifetime, logger, impersonation_user_id)
  @server_url = server_url
  @username = username
  @password = password
  @content_url = content_url
  @api_version = api_version
  @token_lifetime = token_lifetime
  @logger = logger
  @impersonation_user_id = impersonation_user_id
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



28
29
30
# File 'lib/tableau_server_client/client.rb', line 28

def api_version
  @api_version
end

#content_urlObject (readonly)

Returns the value of attribute content_url.



28
29
30
# File 'lib/tableau_server_client/client.rb', line 28

def content_url
  @content_url
end

#impersonation_user_idObject (readonly)

Returns the value of attribute impersonation_user_id.



28
29
30
# File 'lib/tableau_server_client/client.rb', line 28

def impersonation_user_id
  @impersonation_user_id
end

#loggerObject (readonly)

Returns the value of attribute logger.



28
29
30
# File 'lib/tableau_server_client/client.rb', line 28

def logger
  @logger
end

#token_lifetimeObject (readonly)

Returns the value of attribute token_lifetime.



28
29
30
# File 'lib/tableau_server_client/client.rb', line 28

def token_lifetime
  @token_lifetime
end

#usernameObject (readonly)

Returns the value of attribute username.



28
29
30
# File 'lib/tableau_server_client/client.rb', line 28

def username
  @username
end

Instance Method Details

#create(resource, path: nil, request: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/tableau_server_client/client.rb', line 50

def create(resource, path: nil, request: nil)
  path = path || resource.path
  request = request || resource.to_request
  response = session.post do |req|
    req.url request_url(path).to_s
    req.body = request
  end
  Nokogiri::XML(response.body).xpath("//xmlns:tsResponse").children.first
end

#delete(resource, path: nil) ⇒ Object



87
88
89
90
# File 'lib/tableau_server_client/client.rb', line 87

def delete(resource, path: nil)
  path = path || resource.path
  session.delete request_url(path).to_s
end

#download(resource_location, file_path: nil) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/tableau_server_client/client.rb', line 60

def download(resource_location, file_path: nil)
  req_url = request_url("#{resource_location.path}/content", resource_location.query_params)
  response = session.get req_url.to_s
  if file_path
    File.write(file_path, response.body)
  end
  return response
end

#download_image(resource_location, file_path: nil) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/tableau_server_client/client.rb', line 69

def download_image(resource_location, file_path: nil)
  req_url = request_url("#{resource_location.path}/image", resource_location.query_params)
  response = session.get req_url.to_s
  if file_path
    File.write(file_path, response.body)
  end
  return response.body
end

#get(resource_location) ⇒ Object



43
44
45
46
47
48
# File 'lib/tableau_server_client/client.rb', line 43

def get(resource_location)
  req_url = request_url(resource_location.path)
  response = session.get req_url.to_s
  xml =  Nokogiri::XML(response.body).xpath("//xmlns:tsResponse").children.first
  resource_location.klass.from_response(self, resource_location.path, xml)
end

#get_collection(resource_location, &block) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/tableau_server_client/client.rb', line 34

def get_collection(resource_location, &block)
  return self.to_enum(:get_collection, resource_location) unless block
  req_url = request_url(resource_location.path, resource_location.query_params)
  response = session.get req_url.to_s
  TableauServerClient::PaginatableResponse.new(self, req_url, response).each_body do |b|
    resource_location.klass.from_collection_response(self, resource_location.path, b) {|r| yield r }
  end
end

#server_urlObject



30
31
32
# File 'lib/tableau_server_client/client.rb', line 30

def server_url
  @_server_url ||= URI(@server_url.chomp("/"))
end

#sessionObject



92
93
94
95
# File 'lib/tableau_server_client/client.rb', line 92

def session
  faraday.headers['X-Tableau-Auth'] = token.to_s
  faraday
end

#tokenObject



97
98
99
100
101
102
# File 'lib/tableau_server_client/client.rb', line 97

def token
  unless @token and @token.valid?
    @token = 
  end
  @token
end

#update(resource, path: nil, request: nil) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/tableau_server_client/client.rb', line 78

def update(resource, path: nil, request: nil)
  path = path || resource.path
  request = request || resource.to_request
  session.put do |req|
    req.url request_url(path).to_s
    req.body = request
  end
end