mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
38 lines
755 B
Go
38 lines
755 B
Go
package client
|
|
|
|
import (
|
|
"os"
|
|
|
|
"log"
|
|
"net/url"
|
|
|
|
fnclient "github.com/funcy/functions_go/client"
|
|
httptransport "github.com/go-openapi/runtime/client"
|
|
"github.com/go-openapi/strfmt"
|
|
)
|
|
|
|
func Host() string {
|
|
apiURL := os.Getenv("API_URL")
|
|
if apiURL == "" {
|
|
apiURL = "http://localhost:8080"
|
|
}
|
|
|
|
u, err := url.Parse(apiURL)
|
|
if err != nil {
|
|
log.Fatalln("Couldn't parse API URL:", err)
|
|
}
|
|
return u.Host
|
|
}
|
|
|
|
func APIClient() *fnclient.Functions {
|
|
transport := httptransport.New(Host(), "/v1", []string{"http"})
|
|
if os.Getenv("FN_TOKEN") != "" {
|
|
transport.DefaultAuthentication = httptransport.BearerToken(os.Getenv("FN_TOKEN"))
|
|
}
|
|
|
|
// create the API client, with the transport
|
|
client := fnclient.New(transport, strfmt.Default)
|
|
|
|
return client
|
|
}
|