package services

import (
	"fmt"

	"github.com/imroc/req/v3"
)

type CommonClient struct {
	*req.Client
}

func NewClient() *CommonClient {
	return &CommonClient{req.C().
		OnAfterResponse(func(client *req.Client, resp *req.Response) error {
			if resp.Err != nil {
				if resp.String() != "" {
					resp.Err = fmt.Errorf("%s\nraw content:\n%s", resp.Err.Error(), resp.String())
				} else {
					resp.Err = fmt.Errorf("bad request")
				}
				return nil
			}

			if !resp.IsSuccessState() {
				if resp.String() != "" {
					resp.Err = fmt.Errorf("bad response, raw content:\n%s", resp.String())
				} else {
					resp.Err = fmt.Errorf("bad response")
				}
				return nil
			}
			return nil
		}),
	}
}
