Module: Kf5Api::Base

Included in:
Kchat, Organization, User, UserField
Defined in:
lib/kf5_api/base.rb

Instance Method Summary collapse

Instance Method Details

#basic_auth(user_name = nil, password = nil, use_password = false) ⇒ Object



30
31
32
33
34
# File 'lib/kf5_api/base.rb', line 30

def basic_auth(user_name = nil, password = nil, use_password = false)
  user_name ||= use_password ? Kf5Api.config.user_name : "#{Kf5Api.config.user_name}/token"
  password ||= use_password ? Kf5Api.config.password : Kf5Api.config.token
  { username: user_name, password: password }
end

#get(action, query_params = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/kf5_api/base.rb', line 6

def get(action, query_params = {})
  url = Kf5Api.server + action
  query_params = query_params.inject({}){ |memo, (k,v)| memo[k.to_s] = v; memo }

  response = HTTParty.get(url, query: query_params, basic_auth: basic_auth, header: { 'Content-Type' => 'application/json' })

  unless response.code == 200
    Kf5Api.logger.error "[Kf5Api] url: #{url}, status: #{response.code}, body: #{response.parsed_response}"
  end

  response
end

#put(action, body) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/kf5_api/base.rb', line 19

def put(action, body)
  url = Kf5Api.server + action
  response = HTTParty.put(url, body: body.to_json, basic_auth: basic_auth, header: { 'Content-Type' => 'application/json' })

  unless response.code == 200
    Kf5Api.logger.error "[Kf5Api] url: #{url}, status: #{response.code}, body: #{response.parsed_response}"
  end

  response
end