farmstar-survey-backend/pkg/ussd/ussd.go

37 lines
528 B
Go
Raw Permalink Normal View History

2024-02-05 14:05:28 +01:00
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
}