class TableauServerClient::Client

Attributes

api_version[R]
content_url[R]
impersonation_user_id[R]
logger[R]
password[R]
token_lifetime[R]
username[R]

Public Class Methods

new(server_url, username, password, content_url, api_version, token_lifetime, logger, impersonation_user_id) click to toggle source
# 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

Public Instance Methods

create(resource, path: nil, request: nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) { |r| ... } click to toggle source
# 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_url() click to toggle source
# File lib/tableau_server_client/client.rb, line 30
def server_url
  @_server_url ||= URI(@server_url.chomp("/"))
end
session() click to toggle source
# File lib/tableau_server_client/client.rb, line 92
def session
  faraday.headers['X-Tableau-Auth'] = token.to_s
  faraday
end
token() click to toggle source
# File lib/tableau_server_client/client.rb, line 97
def token
  unless @token and @token.valid?
    @token = signin
  end
  @token
end
update(resource, path: nil, request: nil) click to toggle source
# 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

Private Instance Methods

faraday() click to toggle source
# File lib/tableau_server_client/client.rb, line 131
def faraday
  @faraday ||= Faraday.new(request: {params_encoder: EmptyEncoder.new}, headers: {'Content-Type' => 'application/xml'}) do |f|
    f.response :raise_error
    f.response :logger, logger
    f.adapter Faraday.default_adapter
  end
end
request_body(&block) click to toggle source
# File lib/tableau_server_client/client.rb, line 112
def request_body(&block)
  build_request &block
end
request_url(path, query_params={}) click to toggle source
# File lib/tableau_server_client/client.rb, line 108
def request_url(path, query_params={})
  RequestUrl.new(server_url, api_version, path, query_params)
end
signin() click to toggle source
# File lib/tableau_server_client/client.rb, line 116
def signin
  request = request_body {|b|
    b.credentials(name: username, password: password) {
      b.site(contentUrl: content_url)
      b.user(id: impersonation_user_id) if impersonation_user_id
    }
  }
  # POST without Token
  res = faraday.post do |req|
    req.url request_url("auth/signin").to_s
    req.body = request
  end
  @token = TableauServerClient::Token.parse(res.body, token_lifetime)
end