Skip to content

03 Request Reponse

Victor Hugo Avelar Ossorio edited this page Mar 1, 2024 · 1 revision

Requests in Mollie's Library

In Mollie's library, each service takes the responsibility of creating requests for specific API endpoints using the NewAPIRequest method. The method is also exposed, providing you with the ability to craft custom requests and experiment with highly specific queries allowing you to take full advantage of Mollie's API capabilities.

This design offers you enhanced flexibility in constructing API requests that precisely match your unique requirements. Whether you need to explore advanced features of Mollie's API or interact with less common or uncovered endpoints, the NewAPIRequest method empowers you to do so without being constrained by the library's predefined structures.

Here's how you can use the NewAPIRequest method:

req, err := mollie.NewAPIRequest(ctx, httpMethod, uri, body)

Responses in Mollie's Library

In Mollie's library, a Response struct is employed to wrap the standard library http.Response. This custom Response struct includes an additional field called content, which holds the decoded body of the response as an array of bytes.

The primary advantage of this design is that it eliminates the need for continuously reading and updating the closer in the io.ReadCloser implementation of http.Request.Body. This enhancement streamlines the process of handling responses and allows for more efficient handling of the response body.

Responses and Services

When interacting with Mollie's API through the provided methods, you can expect almost all functions to return three values:

// GetPartnerClient method
func (ps *PartnerService) Get(ctx context.Context, id string, opts *GetPartnerClientOptions) (
 res *Response,
 pc *PartnerClient,
 err error,
) {}
  1. The first value returned is the res *mollie.Response wrapper. This contains the HTTP response details, such as status code, headers, and the response body as an array of bytes.

  2. The second value is the pc *mollie.PartnerClient variable, which holds the parsed request body contents in a specific Golang struct format. This allows you to easily access the data from the API response in a structured manner.

  3. The third value returned is the err error variable. If any error occurs during the API call or response handling, an error object will be returned. Otherwise, the err value will be nil, indicating a successful operation.

By receiving these three return values, you can efficiently handle responses from Mollie's API, extract the relevant data in a structured manner, and identify and handle any potential errors that may occur during the API interactions. This consistent pattern ensures a smooth and reliable communication with the API in your Golang applications.