Skip to content

Commit

Permalink
Merge pull request #2 from getsynq/feature/examples
Browse files Browse the repository at this point in the history
lineage example
  • Loading branch information
grasskode authored Jan 18, 2024
2 parents 9d44c1f + eb2e0c9 commit 2fbe0b5
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/generate_docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
if: '!contains(github.event.commits[0].message, ''ci skip'')'
runs-on: ubuntu-latest
env:
CI_COMMIT_MESSAGE: CI generated docs
CI_COMMIT_MESSAGE: Generated API Docs
CI_COMMIT_AUTHOR_NAME: Synqlair
CI_COMMIT_AUTHOR_EMAIL: [email protected]
steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode
gen
118 changes: 118 additions & 0 deletions examples/lineage/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strings"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

corev1 "github.com/getsynq/api/core/v1"
schemasv1 "github.com/getsynq/api/schemas/v1"
)

const (
apiUrl = "https://api.synq.io/"
clientId = os.Getenv("SYNQ_CLIENT_ID")
clientSecret = os.Getenv("SYNQ_CLIENT_SECRET")
)

// Bearer authentication
type bearerAuth struct {
token string
}

// Convert user credentials to request metadata.
func (b bearerAuth) GetRequestMetadata(ctx context.Context, in ...string) (map[string]string, error) {
return map[string]string{
"authorization": "Bearer " + b.token,
}, nil
}

// Specify whether channel security is required to pass these credentials.
func (b bearerAuth) RequireTransportSecurity() bool {
return false
}

func main() {
// Configure credentials.
token, err := getToken()
if err != nil {
panic(err)
}
fmt.Println(token)
opts := []grpc.DialOption{
grpc.WithPerRPCCredentials(bearerAuth{
token: token,
}),
grpc.WithTransportCredentials(insecure.NewCredentials()),
}

conn, err := grpc.Dial(apiUrl, opts...)
if err != nil {
log.Fatalf("could not connect to server -> %+v", err)
}

client := schemasv1.NewLineageServiceClient(conn)

// Fetch lineage.
resp, err := client.GetLineage(context.Background(), &schemasv1.GetLineageRequest{
StartPoint: &schemasv1.GetLineageStartPoint{
From: &schemasv1.GetLineageStartPoint_Entities{
Entities: &schemasv1.EntitiesStartPoint{
Entities: []*corev1.EntityRef{
{
Path: "xxxxxxxxxxxxxxxxxxxxx", // replace with entity path
Type: corev1.EntityType_ENTITY_TYPE_UNSPECIFIED, // replace with entity type
},
},
},
},
},
})
if err != nil {
log.Fatalf("error fetching lineage -> %v", err)
}

fmt.Printf("Lineage -> %+v", resp.Lineage)
}

func getToken() (string, error) {
type oauth2Token struct {
AccessToken string `json:"access_token"`
}

v := &url.Values{}
v.Set("client_id", clientID)
v.Set("client_secret", clientSecret)
v.Set("grant_type", "client_credentials")

req, err := http.NewRequest("POST", fmt.Sprintf("%soauth2/token", apiUrl), strings.NewReader(v.Encode()))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()

token := &oauth2Token{}
if err := json.NewDecoder(res.Body).Decode(token); err != nil {
return "", err
}
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("error fetching token. status code %d", res.StatusCode)
}

return token.AccessToken, nil
}
6 changes: 5 additions & 1 deletion generate_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ set -e
rm -rf $DOCS_DIR
mkdir -p $DOCS_DIR


entity_dirs=`find $PROTOS_DIR -maxdepth 1 -mindepth 1 -type d`
for entity_dir in $entity_dirs; do
entity=`basename $entity_dir`
version_dirs=`find $entity_dir -maxdepth 1 -mindepth 1 -type d`
for version_dir in $version_dirs; do
version=`basename $version_dir`
proto_files=`find $version_dir -name *.proto -type f`
protoc --proto_path=${PROTOS_DIR} --doc_out=${DOCS_DIR} --doc_opt=markdown,${entity}_${version}.md ${proto_files}
protoc --proto_path=${PROTOS_DIR} \
--doc_out=${DOCS_DIR} \
--doc_opt=markdown,${entity}_${version}.md \
${proto_files}
done
done

Expand Down
2 changes: 1 addition & 1 deletion protos/core/v1/entity.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package core.v1;
import "core/v1/entity_type.proto";
import "core/v1/platforms.proto";

option go_package = "github.com/getsynq/cloud/api/core/v1";
option go_package = "github.com/getsynq/api/core/v1";

// Lightweight reference to an Entity
message EntityRef {
Expand Down
2 changes: 1 addition & 1 deletion protos/core/v1/entity_type.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ syntax = "proto3";

package core.v1;

option go_package = "github.com/getsynq/cloud/api/core/v1";
option go_package = "github.com/getsynq/api/core/v1";

// Type of Entity.
// This enum lists all the types currently supported by Synq.
Expand Down
2 changes: 1 addition & 1 deletion protos/core/v1/platforms.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ syntax = "proto3";

package core.v1;

option go_package = "github.com/getsynq/cloud/api/core/v1";
option go_package = "github.com/getsynq/api/core/v1";

// Platforms supported by Synq.
enum Platform {
Expand Down
2 changes: 2 additions & 0 deletions protos/example/v1/example.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package example.v1;

import "google/protobuf/timestamp.proto";

option go_package = "github.com/getsynq/api/example/v1";


/**
* Example is the core model of this package.
Expand Down
2 changes: 2 additions & 0 deletions protos/example/v1/example_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package example.v1;

import "example/v1/example.proto";

option go_package = "github.com/getsynq/api/example/v1";


// ExamplesService is a sample service to display how documentation in protos will work.
// The service implements a basic CRUD API and contains server streaming example.
Expand Down
2 changes: 2 additions & 0 deletions protos/example/v2/example.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package example.v2;

import "google/protobuf/timestamp.proto";

option go_package = "github.com/getsynq/api/example/v2";


/**
* Example is the core model of this package.
Expand Down
2 changes: 2 additions & 0 deletions protos/example/v2/example_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package example.v2;

import "example/v2/example.proto";

option go_package = "github.com/getsynq/api/example/v2";


// ExamplesService is a sample service to display how documentation in protos will work.
// The service implements a basic CRUD API and contains server streaming example.
Expand Down
2 changes: 1 addition & 1 deletion protos/schemas/v1/lineage.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package schemas.v1;

import "core/v1/entity.proto";

option go_package = "github.com/getsynq/cloud/api/schemas/v1";
option go_package = "github.com/getsynq/api/schemas/v1";

// Lineage defines the lineage of table-like entities.
message Lineage {
Expand Down
2 changes: 1 addition & 1 deletion protos/schemas/v1/lineage_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package schemas.v1;

import "schemas/v1/lineage.proto";

option go_package = "github.com/getsynq/cloud/api/schemas/v1";
option go_package = "github.com/getsynq/api/schemas/v1";

import "core/v1/entity.proto";

Expand Down

0 comments on commit 2fbe0b5

Please sign in to comment.