Skip to content

Commit e8fffbe

Browse files
author
Roberto De Ioris
committed
completed the http api
1 parent 6e24311 commit e8fffbe

File tree

5 files changed

+83
-132
lines changed

5 files changed

+83
-132
lines changed

Source/UnrealEnginePython/Private/UEPyIHttpBase.cpp

Lines changed: 38 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,133 +2,76 @@
22
#include "UnrealEnginePythonPrivatePCH.h"
33

44

5-
/*
6-
static PyObject *py_ue_ihttp_request_set_verb(ue_PyIHttpRequest *self, PyObject * args) {
7-
8-
char *verb;
9-
if (!PyArg_ParseTuple(args, "s:set_verb", &verb)) {
10-
return NULL;
11-
}
12-
13-
self->http_request->SetVerb(UTF8_TO_TCHAR(verb));
14-
15-
Py_INCREF(Py_None);
16-
return Py_None;
5+
static PyObject *py_ue_ihttp_base_get_content_length(ue_PyIHttpBase *self, PyObject * args) {
6+
return PyLong_FromLong((int)self->http_base->GetContentLength());
177
}
188

19-
static PyObject *py_ue_ihttp_request_set_url(ue_PyIHttpRequest *self, PyObject * args) {
20-
21-
char *url;
22-
if (!PyArg_ParseTuple(args, "s:set_url", &url)) {
23-
return NULL;
24-
}
9+
static PyObject *py_ue_ihttp_base_get_url(ue_PyIHttpBase *self, PyObject * args) {
10+
return PyUnicode_FromString(TCHAR_TO_UTF8(*self->http_base->GetURL()));
11+
}
2512

26-
self->http_request->SetURL(UTF8_TO_TCHAR(url));
13+
static PyObject *py_ue_ihttp_base_get_content_type(ue_PyIHttpBase *self, PyObject * args) {
14+
return PyUnicode_FromString(TCHAR_TO_UTF8(*self->http_base->GetContentType()));
15+
}
2716

28-
Py_INCREF(Py_None);
29-
return Py_None;
17+
static PyObject *py_ue_ihttp_base_get_content(ue_PyIHttpBase *self, PyObject * args) {
18+
TArray<uint8> data = self->http_base->GetContent();
19+
return PyBytes_FromStringAndSize((char *)data.GetData(), data.Num());
3020
}
3121

32-
static PyObject *py_ue_ihttp_request_set_header(ue_PyIHttpRequest *self, PyObject * args) {
22+
static PyObject *py_ue_ihttp_base_get_header(ue_PyIHttpBase *self, PyObject * args) {
3323

3424
char *key;
35-
char *value;
36-
if (!PyArg_ParseTuple(args, "ss:set_header", &key, &value)) {
25+
if (!PyArg_ParseTuple(args, "s:get_header", &key)) {
3726
return NULL;
3827
}
3928

40-
self->http_request->SetHeader(UTF8_TO_TCHAR(key), UTF8_TO_TCHAR(value));
41-
42-
Py_INCREF(Py_None);
43-
return Py_None;
29+
return PyUnicode_FromString(TCHAR_TO_UTF8(*self->http_base->GetHeader(UTF8_TO_TCHAR(key))));
4430
}
4531

46-
static PyObject *py_ue_ihttp_request_append_to_header(ue_PyIHttpRequest *self, PyObject * args) {
32+
static PyObject *py_ue_ihttp_base_get_url_parameter(ue_PyIHttpBase *self, PyObject * args) {
4733

4834
char *key;
49-
char *value;
50-
if (!PyArg_ParseTuple(args, "ss:append_to_header", &key, &value)) {
35+
if (!PyArg_ParseTuple(args, "s:get_url_parameter", &key)) {
5136
return NULL;
5237
}
5338

54-
self->http_request->AppendToHeader(UTF8_TO_TCHAR(key), UTF8_TO_TCHAR(value));
55-
56-
Py_INCREF(Py_None);
57-
return Py_None;
39+
return PyUnicode_FromString(TCHAR_TO_UTF8(*self->http_base->GetURLParameter(UTF8_TO_TCHAR(key))));
5840
}
5941

60-
static PyObject *py_ue_ihttp_request_set_content(ue_PyIHttpRequest *self, PyObject * args) {
61-
62-
PyObject *py_obj;
63-
if (!PyArg_ParseTuple(args, "O:set_content", &py_obj)) {
64-
return NULL;
65-
}
66-
67-
if (PyUnicode_Check(py_obj)) {
68-
self->http_request->SetContentAsString(UTF8_TO_TCHAR(PyUnicode_AsUTF8(py_obj)));
42+
static PyObject *py_ue_ihttp_base_get_all_headers(ue_PyIHttpBase *self, PyObject * args) {
43+
TArray<FString> headers = self->http_base->GetAllHeaders();
44+
PyObject *py_headers = PyList_New(0);
45+
return PyUnicode_FromString(TCHAR_TO_UTF8(*self->http_base->GetContentType()));
46+
for (FString item : headers) {
47+
PyObject *py_header = PyUnicode_FromString(TCHAR_TO_UTF8(*item));
48+
PyList_Append(py_headers, py_header);
49+
Py_DECREF(py_header);
6950
}
70-
else if (PyBytes_Check(py_obj)) {
71-
char *buf = nullptr;
72-
Py_ssize_t len = 0;
73-
PyBytes_AsStringAndSize(py_obj, &buf, &len);
74-
TArray<uint8> data;
75-
data.Append((uint8 *)buf, len);
76-
self->http_request->SetContent(data);
77-
}
78-
79-
Py_INCREF(Py_None);
80-
return Py_None;
51+
return py_headers;
8152
}
8253

83-
static PyObject *py_ue_ihttp_request_tick(ue_PyIHttpRequest *self, PyObject * args) {
84-
85-
float delta_seconds;
86-
if (!PyArg_ParseTuple(args, "f:tick", &delta_seconds)) {
87-
return NULL;
88-
}
89-
90-
self->http_request->Tick(delta_seconds);
91-
92-
Py_INCREF(Py_None);
93-
return Py_None;
94-
}
95-
96-
static PyObject *py_ue_ihttp_request_process_request(ue_PyIHttpRequest *self, PyObject * args) {
97-
self->http_request->ProcessRequest();
98-
99-
Py_INCREF(Py_None);
100-
return Py_None;
101-
}
102-
103-
static PyObject *py_ue_ihttp_request_cancel_request(ue_PyIHttpRequest *self, PyObject * args) {
104-
self->http_request->CancelRequest();
105-
106-
Py_INCREF(Py_None);
107-
return Py_None;
108-
}*/
109-
110-
static PyObject *py_ue_ihttp_base_get_content_length(ue_PyIHttpBase *self, PyObject * args) {
111-
return PyLong_FromLong((int)self->http_base->GetContentLength());
112-
}
113-
114-
115-
11654
static PyMethodDef ue_PyIHttpBase_methods[] = {
117-
//{ "get_content", (PyCFunction)py_ue_ihttp_base_get_content, METH_VARARGS, "" },
118-
//{ "get_all_headers", (PyCFunction)py_ue_ihttp_base_get_all_headers, METH_VARARGS, "" },
55+
{ "get_content", (PyCFunction)py_ue_ihttp_base_get_content, METH_VARARGS, "" },
56+
{ "get_all_headers", (PyCFunction)py_ue_ihttp_base_get_all_headers, METH_VARARGS, "" },
11957
{ "get_content_length", (PyCFunction)py_ue_ihttp_base_get_content_length, METH_VARARGS, "" },
120-
//{ "get_content_type", (PyCFunction)py_ue_ihttp_base_get_content_type, METH_VARARGS, "" },
121-
//{ "get_header", (PyCFunction)py_ue_ihttp_base_get_header, METH_VARARGS, "" },
122-
//{ "get_url", (PyCFunction)py_ue_ihttp_base_get_url, METH_VARARGS, "" },
123-
//{ "get_url_parameter", (PyCFunction)py_ue_ihttp_base_get_url_parameter, METH_VARARGS, "" },
58+
{ "get_content_type", (PyCFunction)py_ue_ihttp_base_get_content_type, METH_VARARGS, "" },
59+
{ "get_header", (PyCFunction)py_ue_ihttp_base_get_header, METH_VARARGS, "" },
60+
{ "get_url", (PyCFunction)py_ue_ihttp_base_get_url, METH_VARARGS, "" },
61+
{ "get_url_parameter", (PyCFunction)py_ue_ihttp_base_get_url_parameter, METH_VARARGS, "" },
12462
{ NULL } /* Sentinel */
12563
};
12664

12765

12866
static PyObject *ue_PyIHttpBase_str(ue_PyIHttpBase *self)
12967
{
68+
char *s = (char*)"";
69+
FString url = self->http_base->GetURL();
70+
if (!url.IsEmpty()) {
71+
s = TCHAR_TO_UTF8(*url);
72+
}
13073
return PyUnicode_FromFormat("<unreal_engine.IHttpBase {'url': '%s'}>",
131-
PyUnicode_FromString(TCHAR_TO_UTF8(*self->http_base->GetURL())));
74+
PyUnicode_FromString(s));
13275
}
13376

13477
PyTypeObject ue_PyIHttpBaseType = {

Source/UnrealEnginePython/Private/UEPyIHttpRequest.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ static PyObject *py_ue_ihttp_request_get_elapsed_time(ue_PyIHttpRequest *self, P
120120
static PyObject *py_ue_ihttp_request_get_response(ue_PyIHttpRequest *self, PyObject * args) {
121121
FHttpResponsePtr response = self->http_request->GetResponse();
122122
if (!response.IsValid()) {
123-
return PyErr_Format(PyExc_Exception, "unable to retrieve Http Response");
123+
return PyErr_Format(PyExc_Exception, "unable to retrieve IHttpResponse");
124124
}
125-
return PyFloat_FromDouble(self->http_request->GetElapsedTime());
125+
return py_ue_new_ihttp_response(response);
126126
}
127127

128128
static PyMethodDef ue_PyIHttpRequest_methods[] = {
@@ -145,8 +145,13 @@ static PyMethodDef ue_PyIHttpRequest_methods[] = {
145145

146146
static PyObject *ue_PyIHttpRequest_str(ue_PyIHttpRequest *self)
147147
{
148+
char *s = (char*)"";
149+
FString url = self->http_request->GetURL();
150+
if (!url.IsEmpty()) {
151+
s = TCHAR_TO_UTF8(*url);
152+
}
148153
return PyUnicode_FromFormat("<unreal_engine.IHttpRequest {'url': '%s'}>",
149-
PyUnicode_FromString(TCHAR_TO_UTF8(*self->http_request->GetURL())));
154+
PyUnicode_FromString(s));
150155
}
151156

152157
static PyTypeObject ue_PyIHttpRequestType = {

Source/UnrealEnginePython/Private/UEPyIHttpResponse.cpp

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,31 @@
22
#include "UnrealEnginePythonPrivatePCH.h"
33

44

5-
/*
6-
static PyObject *py_ue_ihttp_request_get_response(ue_PyIHttpRequest *self, PyObject * args) {
7-
FHttpResponsePtr response = self->http_request->GetResponse();
8-
if (!response.IsValid()) {
9-
return PyErr_Format(PyExc_Exception, "unable to retrieve Http Response");
10-
}
11-
return PyFloat_FromDouble(self->http_request->GetElapsedTime());
5+
6+
static PyObject *py_ue_ihttp_response_get_response_code(ue_PyIHttpResponse *self, PyObject * args) {
7+
return PyLong_FromLong(self->http_response->GetResponseCode());
8+
}
9+
10+
static PyObject *py_ue_ihttp_response_get_content_as_string(ue_PyIHttpResponse *self, PyObject * args) {
11+
return PyUnicode_FromString(TCHAR_TO_UTF8(*self->http_response->GetContentAsString()));
1212
}
13-
*/
1413

1514
static PyMethodDef ue_PyIHttpResponse_methods[] = {
16-
/* { "append_to_header", (PyCFunction)py_ue_ihttp_request_append_to_header, METH_VARARGS, "" },
17-
{ "cancel_request", (PyCFunction)py_ue_ihttp_request_cancel_request, METH_VARARGS, "" },
18-
{ "get_elapsed_time", (PyCFunction)py_ue_ihttp_request_get_elapsed_time, METH_VARARGS, "" },
19-
{ "get_response", (PyCFunction)py_ue_ihttp_request_get_response, METH_VARARGS, "" },
20-
{ "get_status", (PyCFunction)py_ue_ihttp_request_get_status, METH_VARARGS, "" },
21-
{ "get_verb", (PyCFunction)py_ue_ihttp_request_get_verb, METH_VARARGS, "" },
22-
{ "process_request", (PyCFunction)py_ue_ihttp_request_process_request, METH_VARARGS, "" },
23-
{ "set_content", (PyCFunction)py_ue_ihttp_request_set_content, METH_VARARGS, "" },
24-
{ "set_header", (PyCFunction)py_ue_ihttp_request_set_header, METH_VARARGS, "" },
25-
{ "set_content", (PyCFunction)py_ue_ihttp_request_set_content, METH_VARARGS, "" },
26-
{ "set_url", (PyCFunction)py_ue_ihttp_request_set_url, METH_VARARGS, "" },
27-
{ "set_verb", (PyCFunction)py_ue_ihttp_request_set_verb, METH_VARARGS, "" },
28-
{ "tick", (PyCFunction)py_ue_ihttp_request_tick, METH_VARARGS, "" },*/
29-
{ NULL } /* Sentinel */
15+
{ "get_response_code", (PyCFunction)py_ue_ihttp_response_get_response_code, METH_VARARGS, "" },
16+
{ "get_content_as_string", (PyCFunction)py_ue_ihttp_response_get_content_as_string, METH_VARARGS, "" },
17+
{ NULL } /* Sentinel */
3018
};
3119

3220

3321
static PyObject *ue_PyIHttpResponse_str(ue_PyIHttpResponse *self)
3422
{
23+
char *s = (char*)"";
24+
FString url = self->http_response->GetURL();
25+
if (!url.IsEmpty()) {
26+
s = TCHAR_TO_UTF8(*url);
27+
}
3528
return PyUnicode_FromFormat("<unreal_engine.IHttpResponse {'url': '%s'}>",
36-
PyUnicode_FromString(TCHAR_TO_UTF8(*self->http_response->GetURL())));
29+
PyUnicode_FromString(s));
3730
}
3831

3932
static PyTypeObject ue_PyIHttpResponseType = {
@@ -79,4 +72,12 @@ void ue_python_init_ihttp_response(PyObject *ue_module) {
7972

8073
Py_INCREF(&ue_PyIHttpResponseType);
8174
PyModule_AddObject(ue_module, "IHttpResponse", (PyObject *)&ue_PyIHttpResponseType);
75+
}
76+
77+
PyObject *py_ue_new_ihttp_response(FHttpResponsePtr response) {
78+
ue_PyIHttpResponse *ret = (ue_PyIHttpResponse *)PyObject_New(ue_PyIHttpResponse, &ue_PyIHttpResponseType);
79+
ret->http_response = response;
80+
TSharedRef<IHttpBase> ref(response.Get());
81+
ret->base.http_base = ref;
82+
return (PyObject *)ret;
8283
}

Source/UnrealEnginePython/Private/UEPyIHttpResponse.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ struct ue_PyIHttpBaseType;
1010
typedef struct {
1111
ue_PyIHttpBase base;
1212
/* Type-specific fields go here. */
13-
TSharedRef<IHttpResponse> http_response;
13+
FHttpResponsePtr http_response;
1414
} ue_PyIHttpResponse;
1515

1616

1717
void ue_python_init_ihttp_response(PyObject *);
18+
PyObject *py_ue_new_ihttp_response(FHttpResponsePtr);

Source/UnrealEnginePython/Private/UnrealEnginePython.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ bool FUnrealEnginePythonModule::PythonGILAcquire() {
6161
return true;
6262
}
6363

64-
static void UESetupPythonInterpeter(bool verbose) {
64+
static void UESetupPythonInterpreter(bool verbose) {
65+
#if PY_MAJOR_VERSION >= 3
66+
wchar_t *argv[] = { UTF8_TO_TCHAR("UnrealEngine"), NULL };
67+
#else
68+
char *argv[] = { (char *)"UnrealEngine", NULL };
69+
#endif
70+
PySys_SetArgv(1, argv);
71+
6572
unreal_engine_init_py_module();
6673

6774
PyObject *py_sys = PyImport_ImportModule("sys");
@@ -98,16 +105,10 @@ void FUnrealEnginePythonModule::StartupModule()
98105
}
99106

100107
Py_Initialize();
101-
#if PY_MAJOR_VERSION >= 3
102-
wchar_t *argv[] = { UTF8_TO_TCHAR("UnrealEngine"), NULL };
103-
#else
104-
char *argv[] = { (char *)"UnrealEngine", NULL };
105-
#endif
106-
PySys_SetArgv(1, argv);
107108

108109
PyEval_InitThreads();
109110

110-
UESetupPythonInterpeter(true);
111+
UESetupPythonInterpreter(true);
111112

112113
main_module = PyImport_AddModule("__main__");
113114
main_dict = PyModule_GetDict((PyObject*)main_module);
@@ -181,7 +182,7 @@ void FUnrealEnginePythonModule::RunStringSandboxed(char *str) {
181182
PyThreadState_Swap(nullptr);
182183
PyThreadState_Swap(py_new_state);
183184

184-
UESetupPythonInterpeter(false);
185+
UESetupPythonInterpreter(false);
185186

186187
PyObject *m = PyImport_AddModule("__main__");
187188
if (m == NULL) {
@@ -265,7 +266,7 @@ void FUnrealEnginePythonModule::RunFileSandboxed(char *filename) {
265266
PyThreadState_Swap(nullptr);
266267
PyThreadState_Swap(py_new_state);
267268

268-
UESetupPythonInterpeter(false);
269+
UESetupPythonInterpreter(false);
269270

270271
PyObject *m = PyImport_AddModule("__main__");
271272
if (m == NULL) {

0 commit comments

Comments
 (0)