File tree Expand file tree Collapse file tree 3 files changed +30
-29
lines changed
Expand file tree Collapse file tree 3 files changed +30
-29
lines changed Original file line number Diff line number Diff line change 44Applications often need to communicate with external services. The [ HTTP API] ( ../api/http ) provides helpers that cache responses and surface loading states.
55
66``` go
7- var todo Todo
7+ var todo struct {
8+ Title string ` json:"title"`
9+ }
810if err := http.FetchJSON (" https://jsonplaceholder.typicode.com/todos/1" , &todo); err != nil {
911 if errors.Is (err, http.ErrPending ) {
1012 // display loading UI
13+ } else {
14+ // handle fetch error
1115 }
1216}
1317```
1418
1519## When to Use
16- Use API calls when retrieving remote data to populate stores or render components.
20+ Use ` http.FetchJSON ` when retrieving remote data to populate stores or render components.
1721
1822``` go
1923go func () {
20- data , _ := fetchData (" https://jsonplaceholder.typicode.com/todos/1" )
21- store.Set (" apiData" , data)
24+ var todo struct {
25+ Title string ` json:"title"`
26+ }
27+ if err := http.FetchJSON (" https://jsonplaceholder.typicode.com/todos/1" , &todo); err != nil {
28+ store.Set (" apiData" , err.Error ())
29+ return
30+ }
31+ store.Set (" apiData" , todo.Title )
2232}()
2333```
2434
Original file line number Diff line number Diff line change @@ -7,20 +7,23 @@ import (
77 "net/http/httptest"
88 "testing"
99
10- "github.com/rfwlab/rfw/docs/examples/components "
10+ rfwhttp "github.com/rfwlab/rfw/v1/http "
1111)
1212
13- func TestFetchData (t * testing.T ) {
13+ func TestFetchJSON (t * testing.T ) {
1414 srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
15- w .Write ([]byte ("ok" ))
15+ w .Header ().Set ("Content-Type" , "application/json" )
16+ w .Write ([]byte (`{"title":"ok"}` ))
1617 }))
1718 defer srv .Close ()
1819
19- data , err := components .FetchData (srv .URL )
20- if err != nil {
20+ var todo struct {
21+ Title string `json:"title"`
22+ }
23+ if err := rfwhttp .FetchJSON (srv .URL , & todo ); err != nil {
2124 t .Fatalf ("unexpected error: %v" , err )
2225 }
23- if data != "ok" {
24- t .Fatalf ("expected 'ok', got %s" , data )
26+ if todo . Title != "ok" {
27+ t .Fatalf ("expected 'ok', got %s" , todo . Title )
2528 }
2629}
Original file line number Diff line number Diff line change @@ -4,11 +4,10 @@ package components
44
55import (
66 _ "embed"
7- "io"
8- "net/http"
97
108 core "github.com/rfwlab/rfw/v1/core"
119 dom "github.com/rfwlab/rfw/v1/dom"
10+ "github.com/rfwlab/rfw/v1/http"
1211)
1312
1413//go:embed templates/api_integration_component.rtml
@@ -18,27 +17,16 @@ func NewAPIIntegrationComponent() *core.HTMLComponent {
1817 c := core .NewComponent ("APIIntegrationComponent" , apiIntegrationComponentTpl , nil )
1918 dom .RegisterHandlerFunc ("load" , func () {
2019 go func () {
21- data , err := FetchData ("https://jsonplaceholder.typicode.com/todos/1" )
22- if err != nil {
20+ var todo struct {
21+ Title string `json:"title"`
22+ }
23+ if err := http .FetchJSON ("https://jsonplaceholder.typicode.com/todos/1" , & todo ); err != nil {
2324 c .Store .Set ("apiData" , err .Error ())
2425 return
2526 }
26- c .Store .Set ("apiData" , data )
27+ c .Store .Set ("apiData" , todo . Title )
2728 }()
2829 })
2930
3031 return c
3132}
32-
33- func FetchData (url string ) (string , error ) {
34- resp , err := http .Get (url )
35- if err != nil {
36- return "" , err
37- }
38- defer resp .Body .Close ()
39- b , err := io .ReadAll (resp .Body )
40- if err != nil {
41- return "" , err
42- }
43- return string (b ), nil
44- }
You can’t perform that action at this time.
0 commit comments