Class: Gs2::Core::AbstractClient

Inherits:
Object
  • Object
show all
Defined in:
lib/gs2/core/AbstractClient.rb

Constant Summary collapse

@@ENDPOINT_BASE =
'https://{service}.{region}.gs2.io'

Instance Method Summary collapse

Constructor Details

#initialize(region, gs2_client_id, gs2_client_secret) ⇒ AbstractClient

Returns a new instance of AbstractClient.



22
23
24
25
26
# File 'lib/gs2/core/AbstractClient.rb', line 22

def initialize(region, gs2_client_id, gs2_client_secret)
  @region = region
  @gs2_client_id = gs2_client_id
  @gs2_client_secret = gs2_client_secret
end

Instance Method Details

#create_sign(_module, function, timestamp) ⇒ Object



28
29
30
31
# File 'lib/gs2/core/AbstractClient.rb', line 28

def create_sign(_module, function, timestamp)
  message = _module + ':' + function + ':' + timestamp.to_s
  return Base64.strict_encode64(OpenSSL::HMAC::digest(OpenSSL::Digest::SHA256.new, Base64.strict_decode64(@gs2_client_secret), message))
end

#delete(_module, function, endpoint, url, query = {}, extheader = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gs2/core/AbstractClient.rb', line 75

def delete(_module, function, endpoint, url, query = {}, extheader = {})
  url = (@@ENDPOINT_BASE + url).gsub(/{service}/, endpoint).gsub(/{region}/, @region)
  client = HTTPClient.new
  timestamp = Time.now.to_i
  header = {
    'X-GS2-CLIENT-ID' => @gs2_client_id,
    'X-GS2-REQUEST-TIMESTAMP' => timestamp,
    'X-GS2-REQUEST-SIGN' => create_sign(_module,function,timestamp),
    'Content-Type' => 'application/json'
  }
  header.merge!(extheader)
  return handling(client.delete(url, query: query, header: header))
end

#get(_module, function, endpoint, url, query = {}, extheader = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gs2/core/AbstractClient.rb', line 33

def get(_module, function, endpoint, url, query = {}, extheader = {})
  url = (@@ENDPOINT_BASE + url).gsub(/{service}/, endpoint).gsub(/{region}/, @region)
  client = HTTPClient.new
  timestamp = Time.now.to_i
  header = {
    'X-GS2-CLIENT-ID' => @gs2_client_id,
    'X-GS2-REQUEST-TIMESTAMP' => timestamp,
    'X-GS2-REQUEST-SIGN' => create_sign(_module,function,timestamp),
    'Content-Type' => 'application/json'
  }
  header.merge!(extheader)
  return handling(client.get(url, query: query, header: header))
end

#handling(response) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/gs2/core/AbstractClient.rb', line 89

def handling(response)
  case response.status
    when 200
      begin
        return JSON.parse(response.body)
      rescue JSON::ParserError
        return nil
      end
    when 400; raise Gs2::Core::BadRequestException.new(JSON.parse(JSON.parse(response.body)['message']))
    when 401; raise Gs2::Core::UnauthorizedException.new(JSON.parse(JSON.parse(response.body)['message']))
    when 402; raise Gs2::Core::QuotaExceedException.new(JSON.parse(JSON.parse(response.body)['message']))
    when 404; raise Gs2::Core::NotFoundException.new(JSON.parse(JSON.parse(response.body)['message']))
    when 409; raise Gs2::Core::ConflictException.new(JSON.parse(JSON.parse(response.body)['message']))
    when 500; raise Gs2::Core::InternalServerErrorException.new(JSON.parse(JSON.parse(response.body)['message']))
    when 502; raise Gs2::Core::BadGatewayException.new(JSON.parse(JSON.parse(response.body)['message']))
    when 503; raise Gs2::Core::ServiceUnavailableException.new(JSON.parse(JSON.parse(response.body)['message']))
    when 504; raise Gs2::Core::RequestTimeoutException.new(JSON.parse(JSON.parse(response.body)['message']))
  end
  puts response.status
  puts response.body
end

#post(_module, function, endpoint, url, body, query = {}, extheader = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gs2/core/AbstractClient.rb', line 47

def post(_module, function, endpoint, url, body, query = {}, extheader = {})
  url = (@@ENDPOINT_BASE + url).gsub(/{service}/, endpoint).gsub(/{region}/, @region)
  client = HTTPClient.new
  timestamp = Time.now.to_i
  header = {
    'X-GS2-CLIENT-ID' => @gs2_client_id,
    'X-GS2-REQUEST-TIMESTAMP' => timestamp,
    'X-GS2-REQUEST-SIGN' => create_sign(_module,function,timestamp),
    'Content-Type' => 'application/json'
  }
  header.merge!(extheader)
  return handling(client.post(url, query: query, header: header, body: body.to_json))
end

#put(_module, function, endpoint, url, body, query = {}, extheader = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gs2/core/AbstractClient.rb', line 61

def put(_module, function, endpoint, url, body, query = {}, extheader = {})
  url = (@@ENDPOINT_BASE + url).gsub(/{service}/, endpoint).gsub(/{region}/, @region)
  client = HTTPClient.new
  timestamp = Time.now.to_i
  header = {
    'X-GS2-CLIENT-ID' => @gs2_client_id,
    'X-GS2-REQUEST-TIMESTAMP' => timestamp,
    'X-GS2-REQUEST-SIGN' => create_sign(_module,function,timestamp),
    'Content-Type' => 'application/json'
  }
  header.merge!(extheader)
  return handling(client.put(url, query: query, header: header, body: body.to_json))
end