github.com/anuvu/tyk@v2.9.0-beta9-dl-apic+incompatible/coprocess/python/tyk/gateway_wrapper.c (about)

     1  // +build coprocess
     2  // +build python
     3  
     4  #include <Python.h>
     5  #include "coprocess/api.h"
     6  
     7  
     8  static PyObject *store_data(PyObject *self, PyObject *args) {
     9  	char *key, *value;
    10  	int ttl;
    11  
    12  	if (!PyArg_ParseTuple(args, "ssi", &key, &value, &ttl))
    13  		return NULL;
    14  
    15  	TykStoreData(key, value, ttl);
    16  
    17  	Py_RETURN_NONE;
    18  }
    19  
    20  static PyObject *get_data(PyObject *self, PyObject *args) {
    21  	char *key, *value;
    22  	PyObject *ret;
    23  
    24  	if (!PyArg_ParseTuple(args, "s", &key))
    25  		return NULL;
    26  
    27  	value = TykGetData(key);
    28  	// TykGetData doesn't currently handle storage errors so let's at least safeguard against null pointer
    29  	if (value == NULL) {
    30  		PyErr_SetString(PyExc_ValueError, "Null pointer from TykGetData");
    31  		return NULL;
    32  	}
    33  	ret = Py_BuildValue("s", value);
    34  	// CGO mallocs it in TykGetData and Py_BuildValue just copies strings, hence it's our responsibility to free it now
    35  	free(value);
    36  
    37  	return ret;
    38  }
    39  
    40  static PyObject *trigger_event(PyObject *self, PyObject *args) {
    41  	char *name, *payload;
    42  
    43  	if (!PyArg_ParseTuple(args, "ss", &name, &payload))
    44  		return NULL;
    45  
    46  	TykTriggerEvent(name, payload);
    47  
    48  	Py_RETURN_NONE;
    49  }
    50  
    51  static PyObject *coprocess_log(PyObject *self, PyObject *args) {
    52  	char *message, *level;
    53  
    54  	if (!PyArg_ParseTuple(args, "ss", &message, &level))
    55  		return NULL;
    56  
    57  	CoProcessLog(message, level);
    58  
    59  	Py_RETURN_NONE;
    60  }
    61  
    62  
    63  static PyMethodDef module_methods[] = {
    64  	{"store_data", store_data, METH_VARARGS, "Stores the data in gateway storage by given key and TTL"},
    65  	{"get_data", get_data, METH_VARARGS, "Retrieves the data from gateway storage by given key"},
    66  	{"trigger_event", trigger_event, METH_VARARGS, "Triggers a named gateway event with given payload"},
    67  	{"log", coprocess_log, METH_VARARGS, "Logs a message with given level"},
    68  	{NULL, NULL, 0, NULL} /* Sentinel */
    69  };
    70  
    71  static PyModuleDef module = {
    72  	PyModuleDef_HEAD_INIT, "gateway_wrapper", NULL, -1, module_methods,
    73  	NULL, NULL, NULL, NULL
    74  };
    75  
    76  PyMODINIT_FUNC PyInit_gateway_wrapper(void) {
    77  	return PyModule_Create(&module);
    78  }