mirror of
https://github.com/getAlby/hub.git
synced 2026-08-13 12:33:39 +02:00
* fix: checks for ok status code in responses * refactor: replacing native fetch with useSWR * chore: remove url from logs * chore: add phoenixd error logs for non-success responses * chore: add use currencies hook * chore: use loading from currencies hook and filter out btc --------- Co-authored-by: im-adithya <imadithyavardhan@gmail.com>
67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/getAlby/hub/logger"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func (api *api) RequestEsploraApi(ctx context.Context, endpoint string) (interface{}, error) {
|
|
url := api.cfg.GetEnv().LDKEsploraServer + endpoint
|
|
|
|
client := http.Client{
|
|
Timeout: time.Second * 10,
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
logger.Logger.WithError(err).WithFields(logrus.Fields{
|
|
"url": url,
|
|
}).Error("Failed to create http request")
|
|
return nil, err
|
|
}
|
|
|
|
res, err := client.Do(req)
|
|
if err != nil {
|
|
logger.Logger.WithError(err).WithFields(logrus.Fields{
|
|
"url": url,
|
|
}).Error("Failed to send request")
|
|
return nil, err
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
body, readErr := io.ReadAll(res.Body)
|
|
if readErr != nil {
|
|
logger.Logger.WithError(err).WithFields(logrus.Fields{
|
|
"url": url,
|
|
}).Error("Failed to read response body")
|
|
return nil, errors.New("failed to read response body")
|
|
}
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
logger.Logger.WithFields(logrus.Fields{
|
|
"endpoint": endpoint,
|
|
"status_code": res.StatusCode,
|
|
"body": string(body),
|
|
}).Error("Esplora endpoint returned non-success code")
|
|
return nil, fmt.Errorf("esplora endpoint returned non-success code: %s", string(body))
|
|
}
|
|
|
|
var jsonContent interface{}
|
|
jsonErr := json.Unmarshal(body, &jsonContent)
|
|
if jsonErr != nil {
|
|
logger.Logger.WithError(jsonErr).WithFields(logrus.Fields{
|
|
"url": url,
|
|
}).Error("Failed to deserialize json")
|
|
return nil, fmt.Errorf("failed to deserialize json %s %s", url, string(body))
|
|
}
|
|
return jsonContent, nil
|
|
}
|