Class: AzureMediaService::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/azure_media_service/request.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Request

Returns a new instance of Request.



4
5
6
# File 'lib/azure_media_service/request.rb', line 4

def initialize(config)
  build_config(config) 
end

Instance Method Details

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/azure_media_service/request.rb', line 99

def delete(endpoint, params={})

  setToken() if token_expire?

  res = conn(@config[:mediaURI]).delete do |req|
    req.url URI.escape(endpoint, '():')
    req.headers = @default_headers
    req.headers[:Authorization] = "Bearer #{@access_token}"
    req.params = params
  end

  if res.status == 301
    @config[:mediaURI] = res.headers['location']
    delete(endpoint, params)
  else
    if res.headers[:error]
      raise MediaServiceError.new("#{res.headers[:error]}: #{res.headers[:error_description]}")
    end
    res.body
  end
end

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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/azure_media_service/request.rb', line 8

def get(endpoint, params={})

  setToken() if token_expire?

  res = conn(@config[:mediaURI]).get do |req|
    req.url URI.escape(endpoint, '():')
    req.headers = @default_headers
    req.headers[:Authorization] = "Bearer #{@access_token}"
    req.params = params
  end

  if res.status == 301
    @config[:mediaURI] = res.headers['location']
    get(endpoint, params)
  else
    if res.headers[:error]
      raise MediaServiceError.new("#{res.headers[:error]}: #{res.headers[:error_description]}")
    end
    res.body
  end
end

#post(endpoint, body) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/azure_media_service/request.rb', line 30

def post(endpoint, body)
  setToken if token_expire?

  res = conn(@config[:mediaURI]).post do |req|
    req.url endpoint
    req.headers = @default_headers
    req.headers[:Authorization] = "Bearer #{@access_token}"
    req.body = body
  end

  if res.status == 301
    @config[:mediaURI] = res.headers['location']
    post(endpoint, body)
  else
    if res.headers[:error]
      raise MediaServiceError.new("#{res.headers[:error]}: #{res.headers[:error_description]}")
    end
    res.body
  end
end

#put(endpoint, body) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/azure_media_service/request.rb', line 51

def put(endpoint, body)
  setToken if token_expire?

  res = conn(@config[:mediaURI]).put do |req|
    req.url endpoint
    req.headers = @default_headers
    req.headers[:Authorization] = "Bearer #{@access_token}"
    req.body = body
  end

  if res.status == 301
    @config[:mediaURI] = res.headers['location']
    post(endpoint, body)
  else
    if res.headers[:error]
      raise MediaServiceError.new("#{res.headers[:error]}: #{res.headers[:error_description]}")
    end
    res.body
  end
end

#put_row(url, body) ⇒ Object



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
# File 'lib/azure_media_service/request.rb', line 72

def put_row(url, body)

  _conn = conn(url) do |builder|
    builder.request :multipart
  end

  headers = {}

  if block_given?
    yield(headers)
  end

  res = _conn.put do |req|
    req.headers = headers
    req.body = body
  end
  if res.status == 301
    @config[:mediaURI] = res.headers['location']
    put(url, body)
  else
    if res.headers[:error]
      raise MediaServiceError.new("#{res.headers[:error]}: #{res.headers[:error_description]}")
    end
    res.body
  end
end