tsk-8: Schedule message (#15)

Closes #8

Reviewed-on: #15
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
phoenix
2025-11-29 19:28:05 +00:00
committed by phoenix
parent 9af11dc961
commit 04da7785c8
9 changed files with 367 additions and 6 deletions
+58
View File
@@ -0,0 +1,58 @@
package service
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"git.kundeng.us/phoenix/textsender-models/tx0/contact"
"git.kundeng.us/phoenix/textsender-models/tx0/token"
"github.com/google/uuid"
"git.kundeng.us/phoenix/catapult/internal/app"
)
type Contact struct {
Application *app.App
Token *token.Login
}
type getContactResponse struct {
Message string `json:"message"`
Data []*contact.Contact `json:"data"`
}
func (c *Contact) GetContact(id uuid.UUID) (*contact.Contact, error) {
params := url.Values{}
params.Add("id", id.String())
pm := params.Encode()
fullUrl := fmt.Sprintf("%s/api/v1/contact?%s", c.Application.ApiUrl, pm)
fmt.Println("Url:", fullUrl)
req, err := http.NewRequest("GET", fullUrl, nil)
if err != nil {
return nil, err
}
token := c.Token.AccessToken
req.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var r getContactResponse
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return nil, err
} else {
if len(r.Data) == 0 {
return nil, nil
} else {
return r.Data[0], nil
}
}
}