-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da374e1
commit a46e9f1
Showing
15 changed files
with
277 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
```go | ||
<!--@include: client_example.go --> | ||
# Code Examples | ||
|
||
_Code examples are a great way to learn how to use the Connect API in different languages._ | ||
|
||
::: code-group | ||
|
||
[//]: # (TODO move docs to connect-java and include java examples here) | ||
|
||
```java [Connect Plugin <VPBadge>Java</VPBadge>] | ||
TODO CHECK BACK LATER | ||
<!--@include: javaconnectplugin/src/main/java/com/minekube/connect/example/ExamplePlugin.java --> | ||
``` | ||
|
||
```go [Buf Remote Package <VPBadge>Golang</VPBadge>] | ||
<!--@include: goexample/example_test.go --> | ||
``` | ||
|
||
```md [Other languages] | ||
Checkout https://buf.build/minekube/connect/assets | ||
``` | ||
|
||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package goexample | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
minekube "buf.build/gen/go/minekube/connect/bufbuild/connect-go/minekube/connect/v1alpha1/connectv1alpha1connect" | ||
connectpb "buf.build/gen/go/minekube/connect/protocolbuffers/go/minekube/connect/v1alpha1" | ||
|
||
// You can read more about Buf's Connect for Go here https://connect.build/docs/go | ||
"github.com/bufbuild/connect-go" | ||
) | ||
|
||
const ( | ||
// This is the official Connect API endpoint. | ||
baseURL = "https://connect-api.minekube.com" | ||
|
||
// These are the headers you need to set to authenticate with the Connect API. | ||
endpointHeader = "Connect-Endpoint" | ||
tokenHeader = "Connect-Token" | ||
) | ||
|
||
// ExampleClient_ListEndpoints shows how to list endpoints you have access to. | ||
// It uses the default http.Client and sets the endpoint and token headers | ||
// manually. | ||
func ExampleClient_ListEndpoints() { | ||
// Set up the client. | ||
client := minekube.NewConnectServiceClient(http.DefaultClient, baseURL) | ||
|
||
// Set up a request to list endpoints you have access to. | ||
ctx := context.TODO() | ||
req := connect.NewRequest(&connectpb.ListEndpointsRequest{}) | ||
req.Header().Set(endpointHeader, "my-endpoint") | ||
req.Header().Set(tokenHeader, "my-token") | ||
|
||
// Fetch all endpoints until the server returns an empty page. | ||
for { | ||
// Send the request. | ||
res, err := client.ListEndpoints(ctx, req) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Print the endpoints. | ||
for _, endpoint := range res.Msg.GetEndpoints() { | ||
// Do something with the endpoint. | ||
println(endpoint) | ||
} | ||
|
||
// Prepare the next request. | ||
req.Msg.PageToken = res.Msg.GetNextPageToken() | ||
if req.Msg.PageToken == "" { | ||
// No more pages. | ||
break | ||
} | ||
} | ||
} | ||
|
||
// ExampleClient_ListEndpoints_WithHeadersTransport shows how to connect players | ||
// to an endpoint. It uses a custom http.Client that adds the endpoint and token | ||
// headers to every request automatically. | ||
func ExampleClient_ConnectEndpoint_WithHeadersTransport() { | ||
// Set up the client. | ||
httpClient := &http.Client{Transport: &headersTransport{ | ||
headers: map[string]string{ | ||
endpointHeader: "my-endpoint", | ||
tokenHeader: "my-token", | ||
}, | ||
}} | ||
client := minekube.NewConnectServiceClient(httpClient, baseURL) | ||
|
||
// Set up a request to connect a players to an endpoint you have access to. | ||
ctx := context.TODO() | ||
req := connect.NewRequest(&connectpb.ConnectEndpointRequest{ | ||
Endpoint: "my-endpoint", | ||
Players: []string{ | ||
// example player uuids, | ||
// the players must be online and on another endpoint you have access to. | ||
"11111111-1111-1111-1111-111111111111", | ||
"22222222-2222-2222-2222-222222222222", | ||
"33333333-3333-3333-3333-333333333333", | ||
}, | ||
}) | ||
|
||
// Send the request. | ||
_, err := client.ConnectEndpoint(ctx, req) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// headersTransport is a http.RoundTripper that adds headers to requests | ||
// before sending them so that we don't have to add them to every request | ||
// manually. | ||
type headersTransport struct { | ||
headers map[string]string | ||
base http.RoundTripper | ||
} | ||
|
||
// RoundTrip implements http.RoundTripper. It adds the headers to the request. | ||
func (h *headersTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
for k, v := range h.headers { | ||
req.Header.Add(k, v) | ||
} | ||
base := h.base | ||
if base == nil { | ||
base = http.DefaultTransport | ||
} | ||
return base.RoundTrip(req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module goexample | ||
|
||
go 1.20 | ||
|
||
require ( | ||
buf.build/gen/go/minekube/connect/bufbuild/connect-go v1.7.0-20230426152538-ad9b44e4a050.1 // indirect | ||
buf.build/gen/go/minekube/connect/protocolbuffers/go v1.30.0-20230426152538-ad9b44e4a050.1 // indirect | ||
github.com/bufbuild/connect-go v1.7.0 // indirect | ||
google.golang.org/protobuf v1.30.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
buf.build/gen/go/minekube/connect/bufbuild/connect-go v1.7.0-20230426152538-ad9b44e4a050.1 h1:4LWolQseUun8p0kZC8nN9kj7M1dQlPJjv61lkdKt/B0= | ||
buf.build/gen/go/minekube/connect/bufbuild/connect-go v1.7.0-20230426152538-ad9b44e4a050.1/go.mod h1:5x7C3tv7MLXHBkeolbWEG2wf7vwyjeaE+FgdTjmP0HM= | ||
buf.build/gen/go/minekube/connect/protocolbuffers/go v1.30.0-20230426152538-ad9b44e4a050.1 h1:bUDOTKeRKFVO0I+pYYs3W9QgbKU55tlRt1n88ZAvN2w= | ||
buf.build/gen/go/minekube/connect/protocolbuffers/go v1.30.0-20230426152538-ad9b44e4a050.1/go.mod h1:pY8nIBqtBexIBlNoBXG+3gviKiC4C9QLAIJ3FHwXUDs= | ||
github.com/bufbuild/connect-go v1.7.0 h1:MGp82v7SCza+3RhsVhV7aMikwxvI3ZfD72YiGt8FYJo= | ||
github.com/bufbuild/connect-go v1.7.0/go.mod h1:GmMJYR6orFqD0Y6ZgX8pwQ8j9baizDrIQMm1/a6LnHk= | ||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= | ||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= | ||
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= | ||
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
# Downloads | ||
|
||
_Downloading Connect Plugin is a breeze, | ||
Just a few clicks and you'll be pleased!_ | ||
|
||
<!--@include: ./includes/downloads.md--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.