Skip to content

Commit

Permalink
Merge pull request #1 from jmtx1020/feature/add-find-gw-by-name
Browse files Browse the repository at this point in the history
added gateway by name finding
  • Loading branch information
jmtx1020 authored May 5, 2024
2 parents 01a72fb + 034f2dd commit a49f8c1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
14 changes: 8 additions & 6 deletions api/destinations/destinations_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package destinations

import (
"github.com/jmtx1020/go_quicknode/client"
"os"
"testing"

"github.com/jmtx1020/go_quicknode/client"
)

func TestCreateDestination(t *testing.T) {
Expand Down Expand Up @@ -46,12 +47,13 @@ func TestGetDestinationByID(t *testing.T) {

destinationAPI := &DestinationAPI{API: apiWrapper}

allDestinations, err := destinationAPI.GetAllDestinations()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
// allDestinations, err := destinationAPI.GetAllDestinations()
// if err != nil {
// t.Errorf("Unexpected error: %v", err)
// }

_, err = destinationAPI.GetDestinationByID(allDestinations[0].ID)
// _, err = destinationAPI.GetDestinationByID(allDestinations[0].ID)
_, err := destinationAPI.GetDestinationByID("7a5548ff-6555-4691-80c6-91eaac2d484c")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
Expand Down
33 changes: 33 additions & 0 deletions api/ipfs/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,36 @@ func (g *GatewayAPI) UpdateGatewayByName(gatewayName string, isPrivate, isEnable
}
return &gateway, nil
}

func (g *GatewayAPI) GetGetwayByName(gatewayName string) (Gateway, error) {
g.API.SetBaseURL("https://api.quicknode.com/ipfs/rest/v1/gateway")
endpoint := fmt.Sprintf("%s/%s", g.API.BaseURL, gatewayName)

req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return Gateway{}, err
}
req.Header.Set("Content-Type", "application/json")

resp, err := g.API.Client.Do(req)
if err != nil {
return Gateway{}, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return Gateway{}, err
}

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return Gateway{}, fmt.Errorf("error: %s", body)
}

var gateway Gateway
err = json.Unmarshal(body, &gateway)
if err != nil {
return Gateway{}, err
}
return gateway, nil
}
20 changes: 20 additions & 0 deletions api/ipfs/gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ func TestGetAllGateways(t *testing.T) {
}
}

func TestGetGatewayByName(t *testing.T) {
apiToken := os.Getenv("QUICKNODE_API_TOKEN")

apiWrapper := client.NewAPIWrapper(apiToken, "https://api.quicknode.com/ipfs/rest/v1/gateway")
gatewayAPI := &GatewayAPI{API: apiWrapper}

gateway1, err := gatewayAPI.GetAllGateways()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

gateway2, err := gatewayAPI.GetGetwayByName(gateway1[0].Name)
if gateway1[0].Name != gateway2.Name {
t.Errorf("Expected %v, got %v", gateway1[0].Name, gateway2.Name)
}
}

func TestUpdateGatewayByName(t *testing.T) {
apiToken := os.Getenv("QUICKNODE_API_TOKEN")

Expand Down
4 changes: 0 additions & 4 deletions api/ipfs/pinning/pinning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func TestGetObjectByRequestID(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
fmt.Println(results.Data[0].RequestID)

_, err = pinningAPI.GetObjectByRequestID(results.Data[0].RequestID)
if err != nil {
Expand All @@ -81,7 +80,6 @@ func TestGetPinnedObjectByRequestID(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
fmt.Println(results.Data[0].RequestID)

object, err := pinningAPI.GetPinnedObjectByRequestID(results.Data[0].RequestID)
if err != nil {
Expand All @@ -100,7 +98,6 @@ func TestUpdatePinnedObject(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
fmt.Println(results_first.Data[0].RequestID)

payload := PinnedObjectPayload{
CID: "QmWTqpfKyPJcGuWWg73beJJiL6FrCB5yX8qfcCF4bHanes",
Expand All @@ -125,7 +122,6 @@ func TestDeletePinnedObject(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
fmt.Println(results_first.Data[0].RequestID)

_, err = pinningAPI.DeletePinnedObject(results_first.Data[0].RequestID)
if err != nil {
Expand Down

0 comments on commit a49f8c1

Please sign in to comment.