Skip to content

Commit

Permalink
feat: short link endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Dec 16, 2024
1 parent 0f1727d commit a917fb8
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 1 deletion.
9 changes: 8 additions & 1 deletion internal/http.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package internal

import "net/http"
import (
"io"
"net/http"
)

type HttpPkg struct {
HttpPkgInterface
Expand All @@ -9,9 +12,13 @@ type HttpPkg struct {
func (HttpPkg) Get(url string) (resp *http.Response, err error) {
return http.Get(url)
}
func (HttpPkg) Post(url string, contentType string, body io.Reader) (resp *http.Response, err error) {
return http.Post(url, contentType, body)
}

var Http HttpPkg

type HttpPkgInterface interface {
Get(url string) (resp *http.Response, err error)
Post(url string, contentType string, body io.Reader) (resp *http.Response, err error)
}
73 changes: 73 additions & 0 deletions internal/participation.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package internal

import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/url"
Expand Down Expand Up @@ -139,3 +141,74 @@ func ToLoraDeepLink(network string, offline bool, incentiveEligible bool, part a
}
return fmt.Sprintf("https://lora.algokit.io/%s/transaction-wizard?%s", loraNetwork, strings.Replace(query, "[0]", encodedIndex, -1)), nil
}

// OnlineShortLinkBody represents the request payload for creating an online short link.
// It contains account details, cryptographic keys in Base64, validity range, key dilution, and network information.
type OnlineShortLinkBody struct {
Account string `json:"account"`
VoteKeyB64 string `json:"voteKeyB64"`
SelectionKeyB64 string `json:"selectionKeyB64"`
StateProofKeyB64 string `json:"stateProofKeyB64"`
VoteFirstValid int `json:"voteFirstValid"`
VoteLastValid int `json:"voteLastValid"`
KeyDilution int `json:"keyDilution"`
Network string `json:"network"`
}

// ToOnlineShortLink sends a POST request to create an online short link
// and returns the response or an error if it occurs.
func ToOnlineShortLink(http HttpPkgInterface, part OnlineShortLinkBody) (ShortLinkResponse, error) {
var response ShortLinkResponse
data, err := json.Marshal(part)
if err != nil {
return response, err
}
res, err := http.Post("http://b.nodekit.run/online", "applicaiton/json", bytes.NewReader(data))
if err != nil {
return response, err
}
defer res.Body.Close()

err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return response, err
}

return response, nil
}

// ShortLinkResponse represents the response structure for a shortened link,
// containing its unique identifier.
type ShortLinkResponse struct {
Id string `json:"id"`
}

// OfflineShortLinkBody represents the request body for creating an
// offline short link containing an address and network.
type OfflineShortLinkBody struct {
Account string `json:"account"`
Network string `json:"network"`
}

// ToOfflineShortLink sends an OnlineShortLinkBody to create an offline short link and returns the corresponding response.
// Uses the provided HttpPkgInterface for the POST request and handles JSON encoding/decoding of request and response.
// Returns an OfflineShortLinkResponse on success or an error if the operation fails.
func ToOfflineShortLink(http HttpPkgInterface, offline OfflineShortLinkBody) (ShortLinkResponse, error) {
var response ShortLinkResponse
data, err := json.Marshal(offline)
if err != nil {
return response, err
}
res, err := http.Post("http://b.nodekit.run/offline", "applicaiton/json", bytes.NewReader(data))
if err != nil {
return response, err
}
defer res.Body.Close()

err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return response, err
}

return response, nil
}
91 changes: 91 additions & 0 deletions internal/participation_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package internal

import (
"bytes"
"context"
"fmt"
"github.com/algorandfoundation/algorun-tui/api"
"github.com/algorandfoundation/algorun-tui/internal/test"
"github.com/algorandfoundation/algorun-tui/internal/test/mock"
"io"
"net/http"
"testing"
)

Expand Down Expand Up @@ -208,3 +211,91 @@ func Test_RemovePartKeyByID(t *testing.T) {
}
})
}

var onlineShortLinkResponseStr = `{
"id": "WKIPKTWIFZQJ2"
}`

type testOnlineShortner struct {
HttpPkgInterface
}

// TODO: toggle between Unit/Integration tests
func (testOnlineShortner) Post(url string, bodyType string, body io.Reader) (resp *http.Response, err error) {
responseBody := io.NopCloser(bytes.NewReader([]byte(onlineShortLinkResponseStr)))
return &http.Response{
Status: "",
StatusCode: 0,
Proto: "",
ProtoMajor: 0,
ProtoMinor: 0,
Header: nil,
Body: responseBody,
ContentLength: 0,
TransferEncoding: nil,
Close: false,
Uncompressed: false,
Trailer: nil,
Request: nil,
TLS: nil,
}, nil
}
func Test_ToOnlineShortLink(t *testing.T) {
link, err := ToOnlineShortLink(new(testOnlineShortner), OnlineShortLinkBody{
Account: "JPEGRZ6G4IBZCOC7UV6QZWJ6TENNKRIPENUJTLG5K7PKIKMVTJHUGERARE",
VoteKeyB64: "WWHePYtNZ2T3sHkqdd/38EvoFWrnIKPrTo6xN/4T1l4=",
SelectionKeyB64: "e4kBLu7zXOorjLVzJHOiAn+IhOBsYBCqqHKaJCiCdJs=",
StateProofKeyB64: "1GdNPOck+t6yXvuXxrDEPKqgi4I2sTaNugV1kd5ksUW2G1U6x1FT0WR3aT3ZSSmbYoDt3cVrB3vIPJA8GkqSYg==",
VoteFirstValid: 3118965,
VoteLastValid: 3148965,
KeyDilution: 995,
Network: "mainnet",
})
if err != nil {
t.Error(err)
}
if link.Id != "WKIPKTWIFZQJ2" {
t.Error("Link should be a known hash")
}
}

var offlineShortLinkResponseStr = `{
"id": "D3O3GEG2UD2GW"
}`

type testOfflineShortner struct {
HttpPkgInterface
}

// TODO: toggle between Unit/Integration tests
func (testOfflineShortner) Post(url string, bodyType string, body io.Reader) (resp *http.Response, err error) {
responseBody := io.NopCloser(bytes.NewReader([]byte(offlineShortLinkResponseStr)))
return &http.Response{
Status: "",
StatusCode: 0,
Proto: "",
ProtoMajor: 0,
ProtoMinor: 0,
Header: nil,
Body: responseBody,
ContentLength: 0,
TransferEncoding: nil,
Close: false,
Uncompressed: false,
Trailer: nil,
Request: nil,
TLS: nil,
}, nil
}
func Test_ToOfflineShortLink(t *testing.T) {
link, err := ToOfflineShortLink(new(testOfflineShortner), OfflineShortLinkBody{
Account: "JPEGRZ6G4IBZCOC7UV6QZWJ6TENNKRIPENUJTLG5K7PKIKMVTJHUGERARE",
Network: "mainnet",
})
if err != nil {
t.Error(err)
}
if link.Id != "D3O3GEG2UD2GW" {
t.Error("Link should be a known hash")
}
}

0 comments on commit a917fb8

Please sign in to comment.