-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexample_test.go
61 lines (52 loc) · 1.64 KB
/
example_test.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
package megaport_test
import (
"context"
"fmt"
"log/slog"
"os"
megaport "github.com/megaport/megaportgo"
)
// Create a new client using your credentials and interact with the Megaport API
func Example() {
// Creates a new client using the default HTTP cleint with the specified credentials against the staging environment
client, err := megaport.New(nil,
megaport.WithCredentials("ACCESS_KEY", "SECRET_KEY"),
megaport.WithEnvironment(megaport.EnvironmentStaging),
)
if err != nil {
// ...
}
// Authorize the client using the client's credentials
authInfo, err := client.Authorize(context.TODO())
if err != nil {
// ...
}
fmt.Println(authInfo.AccessToken)
fmt.Println(authInfo.Expiration) // You can use the expiration here to reauthorize the client when your access token expires
// After you have authorized you can interact with the API
locations, err := client.LocationService.ListLocations(context.TODO())
if err != nil {
// ...
}
for _, location := range locations {
fmt.Println(location.Name)
}
}
// Example with a custom logger
func Example_logger() {
// A handler that logs JSON logs to stdout but only errors
handler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelError,
})
// Create a new client with your custom log handler
client, err := megaport.New(nil,
megaport.WithCredentials("ACCESS_KEY", "SECRET_KEY"),
megaport.WithEnvironment(megaport.EnvironmentStaging),
megaport.WithLogHandler(handler),
)
if err != nil {
// ...
}
client.Logger.ErrorContext(context.Background(), "testing") // will print
client.Logger.InfoContext(context.Background(), "testing") // won't print
}