From c961faa8d7ba02e601586c3ce531b2329c7b258e Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Mon, 14 Feb 2022 13:32:02 +0100 Subject: Python: Expose getting servers list Signed-off-by: jwijenbergh --- exports/exports.go | 13 ++++++++++++- wrappers/python/eduvpncommon/discovery.py | 12 ++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/exports/exports.go b/exports/exports.go index c4943f1..9f2d83a 100644 --- a/exports/exports.go +++ b/exports/exports.go @@ -10,7 +10,7 @@ import "github.com/jwijenbergh/eduvpn-common/src" // Functions here should probably not take string parameters, see https://pkg.go.dev/cmd/cgo#hdr-C_references_to_Go // GetOrganizationsList gets the list of organizations from the disco server. -// Returns the unix timestamp of the data. This is used as key for looking up data. +// Returns the json data as a string and an error code. This is used as key for looking up data. //export GetOrganizationsList func GetOrganizationsList() (*C.char, int8) { body, err := eduvpn.GetOrganizationsList() @@ -20,6 +20,17 @@ func GetOrganizationsList() (*C.char, int8) { return C.CString(body), 0 } +// GetServersList gets the list of servers from the disco server. +// Returns the json data as a string and an error code. This is used as key for looking up data. +//export GetServersList +func GetServersList() (*C.char, int8) { + body, err := eduvpn.GetOrganizationsList() + if err != nil { + return nil, int8(err.(eduvpn.RequestError).Code) + } + return C.CString(body), 0 +} + //export FreeString func FreeString(addr *C.char) { C.free(unsafe.Pointer(addr)) diff --git a/wrappers/python/eduvpncommon/discovery.py b/wrappers/python/eduvpncommon/discovery.py index 48089da..8559eb1 100644 --- a/wrappers/python/eduvpncommon/discovery.py +++ b/wrappers/python/eduvpncommon/discovery.py @@ -1,17 +1,19 @@ from . import lib, GoSlice, DataError from ctypes import * +from typing import Callable from enum import Enum # We have to use c_void_p instead of c_char_p to free it properly # See https://stackoverflow.com/questions/13445568/python-ctypes-how-to-free-memory-getting-invalid-pointer-error lib.GetOrganizationsList.argtypes, lib.GetOrganizationsList.restype = [], DataError +lib.GetServersList.argtypes, lib.GetServersList.restype = [], DataError lib.FreeString.argtypes, lib.FreeString.restype = [c_void_p], None lib.Verify.argtypes, lib.Verify.restype = [GoSlice, GoSlice, GoSlice, c_uint64], c_int64 lib.InsecureTestingSetExtraKey.argtypes, lib.InsecureTestingSetExtraKey.restype = [GoSlice], None -def getOrganizationsList() -> str: - dataError = lib.GetOrganizationsList() +def getList(func: Callable) -> str: + dataError = func() ptr = dataError.data error = dataError.error body = "" @@ -22,6 +24,12 @@ def getOrganizationsList() -> str: raise RequestError(error) return body +def GetOrganizationsList() -> str: + return getList(lib.GetOrganizationsList) + +def GetServersList() -> str: + return getList(lib.GetServersList) + class GoError(Exception): message_dict: dict -- cgit v1.2.3