farmstar-survey-backend/pkg/ussd/ussd.go
2024-02-05 16:05:28 +03:00

37 lines
528 B
Go

package ussd
import (
"context"
"github.com/carlmjohnson/requests"
)
type (
USSDClient struct {
endpoint string
}
USSDResponse struct {
Address string `json:"address"`
}
)
func New(endpoint string) *USSDClient {
return &USSDClient{
endpoint: endpoint,
}
}
func (uc *USSDClient) GetAddress(ctx context.Context, phone string) (string, error) {
var resp USSDResponse
if err := requests.
URL(uc.endpoint + phone).
ToJSON(&resp).
Fetch(ctx); err != nil {
return "", err
}
return resp.Address, nil
}