Class: BrickFTP::RESTfulAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/brick_ftp/restful_api/client.rb

Defined Under Namespace

Classes: Error, ErrorResponse

Constant Summary collapse

OPEN_TIMEOUT =
60
READ_TIMEOUT =
60
USER_AGENT =
'BrickFTP Client/1.0 (https://github.com/koshigoe/brick_ftp)'

Instance Method Summary collapse

Constructor Details

#initialize(base_url, api_key) ⇒ Client

Initialize REST API client.

Parameters:

  • base_url (String)
  • api_key (String)


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/brick_ftp/restful_api/client.rb', line 41

def initialize(base_url, api_key)
  @base_uri = make_base_uri(base_url)
  @http = Net::HTTP.new(@base_uri.host, @base_uri.port)
  @http.use_ssl = (@base_uri.scheme == 'https')
  @http.open_timeout = OPEN_TIMEOUT
  @http.read_timeout = READ_TIMEOUT
  @request_headers = {
    'User-Agent' => USER_AGENT,
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
  }
  @api_key = api_key
end

Instance Method Details

#delete(path, headers = nil) ⇒ Hash

Send HTTP request via DELETE method.

Parameters:

  • path (String)

    the request path including query string.

  • headers (Hash, nil) (defaults to: nil)

    additional request headers

Returns:

  • (Hash)

    JSON parsed object.



123
124
125
126
127
128
129
# File 'lib/brick_ftp/restful_api/client.rb', line 123

def delete(path, headers = nil)
  req = Net::HTTP::Delete.new(path, (headers || {}).merge(@request_headers))
  req.basic_auth(@api_key, 'x')
  res = @http.start { |session| session.request(req) }

  handle_response(res)
end

#get(path, headers = nil) ⇒ Hash

Send HTTP request via GET method.

Parameters:

  • path (String)

    the request path including query string.

  • headers (Hash, nil) (defaults to: nil)

    additional request headers

Returns:

  • (Hash)

    JSON parsed object.



61
62
63
64
65
66
67
# File 'lib/brick_ftp/restful_api/client.rb', line 61

def get(path, headers = nil)
  req = Net::HTTP::Get.new(path, (headers || {}).merge(@request_headers))
  req.basic_auth(@api_key, 'x')
  res = @http.start { |session| session.request(req) }

  handle_response(res)
end

#patch(path, data = nil, headers = nil) ⇒ Hash

Send HTTP request via PATCH method.

Parameters:

  • path (String)

    the request path including query string.

  • data (Hash, nil) (defaults to: nil)

    the request body

  • headers (Hash, nil) (defaults to: nil)

    additional request headers

Returns:

  • (Hash)

    JSON parsed object.



108
109
110
111
112
113
114
115
# File 'lib/brick_ftp/restful_api/client.rb', line 108

def patch(path, data = nil, headers = nil)
  req = Net::HTTP::Patch.new(path, (headers || {}).merge(@request_headers))
  req.basic_auth(@api_key, 'x')
  req.body = data.to_json unless data.nil?
  res = @http.start { |session| session.request(req) }

  handle_response(res)
end

#post(path, data = nil, headers = nil) ⇒ Hash

Send HTTP request via POST method.

Parameters:

  • path (String)

    the request path including query string.

  • data (Hash, nil) (defaults to: nil)

    the request body

  • headers (Hash, nil) (defaults to: nil)

    additional request headers

Returns:

  • (Hash)

    JSON parsed object.



76
77
78
79
80
81
82
83
# File 'lib/brick_ftp/restful_api/client.rb', line 76

def post(path, data = nil, headers = nil)
  req = Net::HTTP::Post.new(path, (headers || {}).merge(@request_headers))
  req.basic_auth(@api_key, 'x')
  req.body = data.to_json unless data.nil?
  res = @http.start { |session| session.request(req) }

  handle_response(res)
end

#put(path, data = nil, headers = nil) ⇒ Hash

Send HTTP request via PUT method.

Parameters:

  • path (String)

    the request path including query string.

  • data (Hash, nil) (defaults to: nil)

    the request body

  • headers (Hash, nil) (defaults to: nil)

    additional request headers

Returns:

  • (Hash)

    JSON parsed object.



92
93
94
95
96
97
98
99
# File 'lib/brick_ftp/restful_api/client.rb', line 92

def put(path, data = nil, headers = nil)
  req = Net::HTTP::Put.new(path, (headers || {}).merge(@request_headers))
  req.basic_auth(@api_key, 'x')
  req.body = data.to_json unless data.nil?
  res = @http.start { |session| session.request(req) }

  handle_response(res)
end

#upload_file(http_method, upload_url, io) ⇒ Integer

Upload file.

Parameters:

  • http_method (String)

    Value is PUT or POST, and is the HTTP method used when uploading the file.

  • upload_url (String)

    The URL where the file is uploaded to.

  • io (IO)

    uploading data

Returns:

  • (Integer)

    content length

Raises:

  • (ArgumentError)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/brick_ftp/restful_api/client.rb', line 138

def upload_file(http_method, upload_url, io)
  raise ArgumentError, "Unsupported HTTP method `#{http_method}`" unless %w[POST PUT].include?(http_method)

  uri = URI.parse(upload_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  req = Net::HTTP.const_get(http_method.capitalize).new(uri.request_uri)
  req.body_stream = io
  req['Content-Length'] = io.size
  res = http.start { |session| session.request(req) }

  return io.size if res.is_a?(Net::HTTPSuccess)

  raise Error, parse_error_response(res)
end