4. Client - mihilbabin/guesty_api GitHub Wiki

Client

GuestyAPI::Client class is a heart of interaction with a Guesty API. Being an extension of HTTParty gem it contains a low-level interface for common HTTP methods used by Guesty as well as authentication headers inclusion in a request.

Creating a client

To create a client just use a class constructor

client = GuestyAPI::Client.new(username: '<API-KEY>', password: '<SECRET-KEY>')

The code above creates low-level wrapper which will hit Guesty API by using the following methods.

Available methods

#get(url:, data: nil)

Performs a simple GET request to given url and passes data as a query string.

#post(url:, data:)

Performs a simple POST request to given url and passes data as a request body in JSON format. Note that data is the required parameter.

#put(url:, data:)

Performs a simple PUT request to given url and passes data as a request body in JSON format. Note that data is the required parameter.

#delete(url:)

Performs a simple DELETE request to given url.

All methods above are returning raw HTTParty response so if you are using a client directly you may want to refer to it as well.

Authentication Mode

There are 2 ways you can get your authentication credentials: via your account dashboard or via Guesty marketplace. The first option uses Basic HTTP authentication and enabled by default. Just pass authentication data in a constructor as usual:

# auth_mode: :basic is by default, so you can omit that parameter
client = GuestyAPI::Client.new(username: '<API-KEY>', password: '<SECRET-KEY>', auth_mode: :basic)

Marketplace credentials are represented by JWT Bearer token passed in request headers. Once you have that token you can create a client:

# auth_mode can be anything you like (except :basic, obviously)
client = GuestyAPI::Client.new(username: '<JWT-TOKEN>', auth_mode: :jwt)

Notice that password parameter is omitted as well. Keep in mind that token may be less permissive then regular authentication.

Doesn't matter which auth_mode you'll pick - authentication will be performed by client automatically.

⚠️ **GitHub.com Fallback** ⚠️