Module: PetstoreApiClient::Request

Included in:
Client
Defined in:
lib/petstore_api_client/request.rb

Overview

HTTP request methods for the Petstore API client

Provides high-level abstraction over HTTP operations using Faraday. This module implements the Dependency Inversion Principle by abstracting HTTP operations behind a clean interface.

All HTTP methods return a Response object that wraps the Faraday response. Errors are automatically detected and raised as appropriate exception types.

This module is included by Client and provides:

  • GET requests for retrieving resources

  • POST requests for creating resources

  • PUT requests for updating resources

  • DELETE requests for removing resources

Examples:

Making a GET request

client = Client.new
response = client.get("/pet/123")
pet = response.body

Making a POST request with body

response = client.post("/pet", body: { name: "Fluffy", status: "available" })

See Also:

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#delete(path, params: {}) ⇒ Response

Perform HTTP DELETE request

Deletes a resource from the API. Query parameters can be provided via the params hash if needed.

Examples:

Delete a pet

response = client.delete("/pet/123")

Parameters:

  • path (String)

    The API endpoint path

  • params (Hash) (defaults to: {})

    Optional query parameters

Returns:

Raises:

Since:

  • 0.1.0



131
132
133
# File 'lib/petstore_api_client/request.rb', line 131

def delete(path, params: {})
  request(:delete, path, params: params)
end

#get(path, params: {}) ⇒ Response

Perform HTTP GET request

Retrieves a resource from the API. Query parameters can be provided via the params hash.

Examples:

Get a pet by ID

response = client.get("/pet/123")
pet = response.body

Get pets with query parameters

response = client.get("/pet/findByStatus", params: { status: "available" })
pets = response.body

Parameters:

  • path (String)

    The API endpoint path (e.g., “/pet/123”)

  • params (Hash) (defaults to: {})

    Optional query parameters

Returns:

Raises:

Since:

  • 0.1.0



55
56
57
# File 'lib/petstore_api_client/request.rb', line 55

def get(path, params: {})
  request(:get, path, params: params)
end

#post(path, body: {}) ⇒ Response

Perform HTTP POST request

Creates a new resource on the API. The request body should contain the resource data as a hash, which will be automatically serialized to JSON.

Examples:

Create a new pet

response = client.post("/pet", body: {
  name: "Fluffy",
  status: "available",
  category: { id: 1, name: "Dogs" }
})

Parameters:

  • path (String)

    The API endpoint path

  • body (Hash) (defaults to: {})

    Request body data

Returns:

Raises:

Since:

  • 0.1.0



82
83
84
# File 'lib/petstore_api_client/request.rb', line 82

def post(path, body: {})
  request(:post, path, body: body)
end

#put(path, body: {}) ⇒ Response

Perform HTTP PUT request

Updates an existing resource on the API. The request body should contain the updated resource data as a hash.

Examples:

Update an existing pet

response = client.put("/pet", body: {
  id: 123,
  name: "Fluffy Updated",
  status: "sold"
})

Parameters:

  • path (String)

    The API endpoint path

  • body (Hash) (defaults to: {})

    Request body data with updates

Returns:

Raises:

Since:

  • 0.1.0



109
110
111
# File 'lib/petstore_api_client/request.rb', line 109

def put(path, body: {})
  request(:put, path, body: body)
end