Class: PowerBI::Tenant

Inherits:
Object
  • Object
show all
Defined in:
lib/power-bi/tenant.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token_generator) ⇒ Tenant

Returns a new instance of Tenant.



5
6
7
8
# File 'lib/power-bi/tenant.rb', line 5

def initialize(token_generator)
  @token_generator = token_generator
  @workspaces = WorkspaceArray.new(self)
end

Instance Attribute Details

#workspacesObject (readonly)

Returns the value of attribute workspaces.



3
4
5
# File 'lib/power-bi/tenant.rb', line 3

def workspaces
  @workspaces
end

Instance Method Details

#delete(url, params = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/power-bi/tenant.rb', line 41

def delete(url, params = {})
  response = Faraday.delete(PowerBI::BASE_URL + url) do |req|
    req.params = params
    req.headers['Accept'] = 'application/json'
    req.headers['authorization'] = "Bearer #{token}"
    yield req if block_given?
  end
  unless [200, 202].include? response.status
    raise APIError.new("Error calling Power BI API (status #{response.status}): #{response.body}")
  end
  unless response.body.empty?
    JSON.parse(response.body, symbolize_names: true)
  end
end

#get(url, params = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/power-bi/tenant.rb', line 10

def get(url, params = {})
  response = Faraday.get(PowerBI::BASE_URL + url) do |req|
    req.params = params
    req.headers['Accept'] = 'application/json'
    req.headers['authorization'] = "Bearer #{token}"
    yield req if block_given?
  end
  if response.status != 200
    raise APIError.new("Error calling Power BI API (status #{response.status}): #{response.body}")
  end
  unless response.body.empty?
    JSON.parse(response.body, symbolize_names: true)
  end
end

#post(url, params = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/power-bi/tenant.rb', line 25

def post(url, params = {})
  response = Faraday.post(PowerBI::BASE_URL + url) do |req|
    req.params = params
    req.headers['Accept'] = 'application/json'
    req.headers['Content-Type'] = 'application/json'
    req.headers['authorization'] = "Bearer #{token}"
    yield req if block_given?
  end
  unless [200, 202].include? response.status
    raise APIError.new("Error calling Power BI API (status #{response.status}): #{response.body}")
  end
  unless response.body.empty?
    JSON.parse(response.body, symbolize_names: true)
  end
end

#post_file(url, file, params = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/power-bi/tenant.rb', line 56

def post_file(url, file, params = {})
  conn = Faraday.new do |f|
    f.request :multipart
  end
  response = conn.post(PowerBI::BASE_URL + url) do |req|
    req.params = params
    req.headers['Accept'] = 'application/json'
    req.headers['Content-Type'] = 'multipart/form-data'
    req.headers['authorization'] = "Bearer #{token}"
    req.body = {value: Faraday::UploadIO.new(file, 'application/octet-stream')}
  end
  if response.status != 202
    raise APIError.new("Error calling Power BI API (status #{response.status}): #{response.body}")
  end
  JSON.parse(response.body, symbolize_names: true)
end