Module: Connector

Defined in:
lib/easycodefrb/connector.rb

Overview

상품 요청

Instance Method Summary collapse

Instance Method Details

#execute(url_path, body, access_token_cls, req_info, service_type) ⇒ Object

API 요청 실행



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/easycodefrb/connector.rb', line 97

def execute(url_path, body, access_token_cls, req_info, service_type)
  set_token(
    req_info.client_id,
    req_info.client_secret,
    access_token_cls,
    service_type
  )
  enc_body = CGI::escape(body.to_json())
  return request_product(
    req_info.domain + url_path,
    access_token_cls.get_token(service_type),
    enc_body
  )
end

#request_product(req_url, token, body_str) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/easycodefrb/connector.rb', line 14

def request_product(req_url, token, body_str)
  uri = URI.parse(req_url)
  header = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Accept': 'application/json'
  }

  if token != '' && token != nil
    header['Authorization'] = 'Bearer ' + token
  end

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  req = Net::HTTP::Post.new(uri.request_uri, header)
  req.body = body_str

  res = http.request(req)

  return case res
  when Net::HTTPOK
    data = CGI::unescape(res.body)
    return JSON.parse(data)
  when Net::HTTPBadRequest
    new_response_message(Message::BAD_REQUEST)
  when Net::HTTPUnauthorized
    new_response_message(Message::UNAUTHORIZED)
  when Net::HTTPForbidden
    new_response_message(Message::FORBIDDEN)
  when Net::HTTPNotFound
    new_response_message(Message::NOT_FOUND)
  else
    new_response_message(Message::SERVER_ERROR)
  end
end

#request_token(client_id, client_secret) ⇒ Object

엑세스 토큰 요청



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/easycodefrb/connector.rb', line 50

def request_token(client_id, client_secret)
  uri = URI.parse(EasyCodef::OAUTH_DOMAIN + EasyCodef::PATH_GET_TOKEN)

  auth = client_id + ':' + client_secret
  enc_auth = Base64.encode64(auth).gsub(/\n/, '')

  header = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Authorization': 'Basic ' + enc_auth
  } 

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  req = Net::HTTP::Post.new(uri.request_uri, header)
  req.body = 'grant_type=client_credentials&scope=read'
  
  res = http.request(req)

  return case res
  when Net::HTTPOK
    JSON.parse(res.body)
  else
    return nil
  end
end

#set_token(client_id, client_secret, access_token_cls, service_type) ⇒ Object

토큰 셋팅Codef 인스턴스 변수에 조회된 access_token을 셋팅한다



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/easycodefrb/connector.rb', line 78

def set_token(client_id, client_secret, access_token_cls, service_type)
  repeat_cnt = 3
  i = 0
  if access_token_cls.get_token(service_type) == ''
    while i < repeat_cnt
      token_map = request_token(client_id, client_secret)
      if token_map != nil
        access_token = token_map[EasyCodef::KEY_ACCESS_TOKEN]
        access_token_cls.set_token(access_token, service_type)
        if access_token != nil && access_token != ''
          break
        end
      end
      i += 1
    end
  end
end