/
tenant_retrieve.go
69 lines (54 loc) · 1.56 KB
/
tenant_retrieve.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cmd
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/bartmika/osin-example/internal/utils"
"github.com/spf13/cobra"
)
// EXAMPLE USAGE
// go run main.go tenant_retrieve --id=1
var (
tenantRetrieveID string
)
func init() {
tenantRetrieveCmd.Flags().StringVarP(&tenantRetrieveID, "id", "a", "0", "Id of the tenant")
tenantRetrieveCmd.MarkFlagRequired("id")
rootCmd.AddCommand(tenantRetrieveCmd)
}
var tenantRetrieveCmd = &cobra.Command{
Use: "tenant_retrieve -d -e",
TraverseChildren: true,
Short: "Login a customer account",
Run: func(cmd *cobra.Command, args []string) {
fmt.Print("\033[H\033[2J") // Clear screen
doRunTenantRetrieve()
},
}
func doRunTenantRetrieve() {
aUrl := "http://127.0.0.1:8000" + strings.Replace("/api/v1/tenant/xxx", "xxx", tenantRetrieveID, -1)
// Create a Bearer string by appending string access token
accessToken := os.Getenv("OSIN_EXAMPLE_CLI_ACCESS_TOKEN")
var bearer = "Bearer " + accessToken
client := &http.Client{}
req, _ := http.NewRequest("GET", aUrl, nil)
// add authorization header to the req
req.Header.Add("Authorization", bearer)
// Set data format.
req.Header.Set("Content-Type", "application/json")
// Send req using http Client
resp, err := client.Do(req)
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
defer resp.Body.Close()
// Read the response body
responseBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("ReadAll | An Error Occured %v", err)
}
fmt.Println(utils.JsonPrettyPrint(string(responseBytes)))
}