Module: LontaraUtilities::HTTPClient

Defined in:
lib/lontara_utilities/http_client.rb,
lib/lontara_utilities/http_client/request.rb,
lib/lontara_utilities/http_client/body_parser.rb

Overview

Lontara HTTP Client

Request dapat menggunakan method ‘new` disertai HTTP method sebagai parameter, atau menggunakan method predifined `.get`, `.post`, `.put`, `.delete`, `.patch`.

Parameter yang dibutuhkan:

  • method. Contoh: ‘:get`, `:post`, `:put`, `:delete`. Dapat menerima dalam bentuk String.

  • :url. Contoh: ‘localhost:4000/book/category`

  • :params. Contoh: ‘params: { sort: “createDate,desc” }`

  • :body. Contoh: ‘body: { username: admin, password: admin }`

  • :headers. Headers dapat diisi dengan berbagai key-value dalam format lowercase.

    Pisahkan dengan underscore jika key lebih dari satu kata.
    Contoh: `headers: { authorization: "Bearer hnjuvdiwv67wwqvn....", content_type: "application/json" }`
    

Contoh:

Lontara::HTTPClient.new(:get,
  url: 'http://localhost:4000/book/category',
  params: { sort: 'createDate,desc' }
)

atau

request = Lontara::HTTPClient.get(
  url: 'http://localhost:4000/user',
  params: { id: "123GHANIY" },
  headers: {
    authorization: "Bearer #{token}",
    content_type: 'application/json'
  }
)

JSON.parse(request.body)

Defined Under Namespace

Classes: BodyParser, Request

Class Method Summary collapse

Class Method Details

.new(method, url:, **options) ⇒ Object



47
48
49
# File 'lib/lontara_utilities/http_client.rb', line 47

def self.new(method, url:, **options)
  Request.new(method, url:, **options).perform
end