Skip to content

Commit d403726

Browse files
authored
docs: example go client (#304)
* Add plumbing usage example * Try to make it look better on godoc
1 parent b22e5e8 commit d403726

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

go/plumbing/doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Package plumbing provides an auto-generated Go client for accessing Netlify's API.
3+
4+
See https://goswagger.io/generate/client.html for more example usage of an API client generated by go-swagger.
5+
*/
6+
package plumbing

go/plumbing/example_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package plumbing
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"net/http/httptest"
8+
"strings"
9+
10+
"github.com/go-openapi/runtime"
11+
httptransport "github.com/go-openapi/runtime/client"
12+
"github.com/go-openapi/strfmt"
13+
"github.com/netlify/open-api/go/plumbing/operations"
14+
)
15+
16+
func Example() {
17+
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
18+
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
19+
rw.Write([]byte(`{ "name": "Pets of Netlify" }`))
20+
}))
21+
defer server.Close()
22+
host := strings.ReplaceAll(server.URL, "http://", "")
23+
24+
// Create the API client
25+
// For Netlify's production API use DefaultHost, DefaultBasePath, DefaultSchemes
26+
transport := httptransport.New(host, DefaultBasePath, []string{"http"})
27+
client := New(transport, strfmt.Default)
28+
29+
// Prepare the API token
30+
authInfo := runtime.ClientAuthInfoWriterFunc(func(r runtime.ClientRequest, _ strfmt.Registry) error {
31+
r.SetHeaderParam("User-Agent", "Your app")
32+
r.SetHeaderParam("Authorization", "Bearer your_netlify_api_token")
33+
return nil
34+
})
35+
36+
// Make a request
37+
params := operations.NewGetSiteParams()
38+
params.SiteID = "123"
39+
res, err := client.Operations.GetSite(params, authInfo)
40+
if err != nil {
41+
log.Fatal(err)
42+
}
43+
fmt.Println(res.Payload.Name)
44+
// Output: Pets of Netlify
45+
}

0 commit comments

Comments
 (0)