Closes #8 Reviewed-on: #15 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
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
|
|
}
|
|
}
|
|
}
|