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, retries: 5) ⇒ Tenant

Returns a new instance of Tenant.



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

def initialize(token_generator, retries: 5)
  @token_generator = token_generator
  @workspaces = WorkspaceArray.new(self)
  @gateways = GatewayArray.new(self)

  ## WHY RETRIES? ##
  # It is noticed that once in a while (~0.1% API calls), the Power BI server returns a 500 (internal server error) withou apparent reason, just retrying works :-)
  ##################
  @retry_options = {
    max: retries,
    exceptions: [Errno::ETIMEDOUT, Timeout::Error, Faraday::TimeoutError, Faraday::RetriableResponse],
    methods: [:get, :post, :patch, :delete],
    retry_statuses: [500], # internal server error
    interval: 0.2,
    interval_randomness: 0,
    backoff_factor: 4,
    retry_block: -> (env, options, retries, exc) { puts "retrying...!! (@ #{Time.now.to_s}), exception: #{exc.to_s} ---- #{exc.message}" },
  }
end

Instance Attribute Details

#gatewaysObject (readonly)

Returns the value of attribute gateways.



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

def gateways
  @gateways
end

#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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/power-bi/tenant.rb', line 96

def delete(url, params = {})
  conn = Faraday.new do |f|
    f.request :retry, @retry_options
  end
  response = conn.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



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

def get(url, params = {})
  conn = Faraday.new do |f|
    f.request :retry, @retry_options
  end
  response = conn.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
  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_raw(url, params = {}) ⇒ Object



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

def get_raw(url, params = {})
  conn = Faraday.new do |f|
    f.request :retry, @retry_options
  end
  response = conn.get(PowerBI::BASE_URL + url) do |req|
    req.params = params
    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
  response.body
end

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/power-bi/tenant.rb', line 77

def patch(url, params = {})
  conn = Faraday.new do |f|
    f.request :retry, @retry_options
  end
  response = conn.patch(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(url, params = {}) ⇒ Object



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

def post(url, params = {})
  conn = Faraday.new do |f|
    f.request :retry, @retry_options
  end
  response = conn.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, 201, 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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/power-bi/tenant.rb', line 114

def post_file(url, file, params = {})
  conn = Faraday.new do |f|
    f.request :multipart
    f.request :retry, @retry_options
  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')}
    req.options.timeout = 120  # default is 60 seconds Net::ReadTimeout
  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