-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Showing
7 changed files
with
989 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"github.com/BigJk/snd/database/memory" | ||
"github.com/BigJk/snd/server" | ||
"os" | ||
"time" | ||
) | ||
|
||
var supportedLanguages = map[string]bool{ | ||
"python": true, | ||
} | ||
|
||
func main() { | ||
lang := flag.String("lang", "python", "The language to generate the SDK for.") | ||
out := flag.String("out", "snd_api.py", "The output file for the SDK.") | ||
flag.Parse() | ||
|
||
if !supportedLanguages[*lang] { | ||
panic(fmt.Sprintf("Unsupported language: %s", *lang)) | ||
} | ||
|
||
fmt.Println("Starting S&D server to fetch functions...") | ||
server, err := server.New(memory.New()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
go func() { | ||
_ = server.Start("127.0.0.1:7123") | ||
}() | ||
|
||
time.Sleep(time.Second) | ||
fmt.Println("Shutting down server...") | ||
|
||
var source string | ||
switch *lang { | ||
case "python": | ||
source = pythonSDK() | ||
} | ||
|
||
fmt.Println("Writing SDK to file...") | ||
if err := os.WriteFile(*out, []byte(source), 0666); err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println("Wrote: ", *out) | ||
} |
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 main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/BigJk/snd/rpc/bind" | ||
"github.com/iancoleman/strcase" | ||
"strings" | ||
) | ||
|
||
const pythonAPIHeader = `import requests | ||
# This class is a simple Python SDK for the Sales & Dungeon API. | ||
# It is automatically generated from the API definition. | ||
# | ||
# Requires the requests library to be installed. | ||
# python -m pip install requests | ||
class SndAPI: | ||
def __init__(self, base_url): | ||
""" | ||
Initialize the Sales & Dungeon API class with the base URL of the API. | ||
Parameters: | ||
- base_url (str): The base URL of the API. Most likely "http://127.0.0.1:7123" | ||
""" | ||
self.base_url = base_url | ||
def _make_request(self, endpoint, method='GET', params=None): | ||
url = f"{self.base_url}/{endpoint}" | ||
if method == 'GET': | ||
response = requests.get(url, params=params) | ||
elif method == 'POST': | ||
response = requests.post(url, json=params) | ||
elif method == 'DELETE': | ||
response = requests.delete(url, json=params) | ||
else: | ||
raise ValueError(f"Unsupported HTTP method: {method}") | ||
if response.status_code == 200: | ||
return response.json() | ||
else: | ||
response.raise_for_status() | ||
` | ||
|
||
func pythonArgType(argType string) string { | ||
slice := strings.HasSuffix(argType, "[]") | ||
if slice { | ||
argType = argType[:len(argType)-2] | ||
} | ||
|
||
typeName := "" | ||
switch argType { | ||
case "string": | ||
typeName = "str" | ||
case "int": | ||
typeName = "int" | ||
case "float": | ||
typeName = "float" | ||
case "bool": | ||
typeName = "bool" | ||
case "interface{}": | ||
typeName = "dict" | ||
default: | ||
typeName = strings.Replace(argType, "snd.", "", -1) | ||
} | ||
|
||
if slice { | ||
return fmt.Sprintf("list of %s", typeName) | ||
} | ||
return typeName | ||
} | ||
|
||
func pythonDefineFunction(function bind.Function) string { | ||
args := make([]string, len(function.Args)) | ||
for i, _ := range function.Args { | ||
args[i] = fmt.Sprintf("arg%d", i) | ||
} | ||
|
||
parameters := make([]string, len(function.Args)) | ||
for i, arg := range function.Args { | ||
parameters[i] = fmt.Sprintf(" - %s (%s): parameter", args[i], pythonArgType(arg)) | ||
} | ||
|
||
snakeName := strcase.ToSnake(function.Name) | ||
snakeName = strings.Replace(snakeName, "5_e", "5e", -1) | ||
|
||
return fmt.Sprintf(` def %s(self, %s): | ||
""" | ||
Perform an action using the %s API endpoint. | ||
Parameters: | ||
%s | ||
""" | ||
endpoint = "api/%s" | ||
return self._make_request(endpoint, method='POST', params=[%s])`, snakeName, strings.Join(args, ", "), function.Name, strings.Join(parameters, "\n"), function.Name, strings.Join(args, ", ")) | ||
} | ||
|
||
func pythonSDK() string { | ||
functions := bind.Functions() | ||
functionsStr := make([]string, len(functions)) | ||
i := 0 | ||
for _, function := range functions { | ||
functionsStr[i] = pythonDefineFunction(function) | ||
i++ | ||
} | ||
|
||
return fmt.Sprintf("%s\n%s", pythonAPIHeader, strings.Join(functionsStr, "\n\n")) | ||
} |
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
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,5 @@ | ||
import snd_sdk | ||
|
||
api = snd_sdk.SndAPI("http://127.0.0.1:7123") | ||
response = api.get_templates() | ||
print(response[0]['name']) |
Oops, something went wrong.