Igris/pkg/zohal/zohal.go

64 lines
1.2 KiB
Go

package zohal
import (
"backend/config"
"backend/pkg/util"
"backend/pkg/validate/common"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
type Zohal struct {
cfg config.KYC
}
func (z *Zohal) Shahkar(ctx context.Context, phone, nationalID string) (ZohalShahkarResp, error) {
var req ZohalShahkarReq
var resp ZohalShahkarResp
ok, err := common.IsValidPhone(phone)
if !ok {
return resp, err
}
ok, err = common.IsValidNationalID(nationalID)
if !ok {
return resp, err
}
header := make(map[string]string)
header["Content-Type"] = "application/json"
header["Accept"] = "application/json"
header["Authorization"] = fmt.Sprintf("Bearer %s", z.cfg.APIKey)
req.Phone = phone
req.NationalID = nationalID
u, err := url.Parse(z.cfg.URL)
if err != nil {
return resp, err
}
u = u.JoinPath("inquiry", "shahkar")
client := util.NewHttpClient()
clientResp, err := client.HttpRequest(ctx, http.MethodPost, u.String(), req, header)
if err != nil {
return resp, err
}
bodyBytes, err := io.ReadAll(clientResp.Body)
if err != nil {
return resp, err
}
err = json.Unmarshal(bodyBytes, &resp)
if err != nil {
return resp, err
}
return resp, err
}