-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from EA31337/feature/web-class
Adds Web class [GH-13]
- Loading branch information
Showing
5 changed files
with
195 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
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,92 @@ | ||
//+------------------------------------------------------------------+ | ||
//| EA31337 framework | | ||
//| Copyright 2016-2019, 31337 Investments Ltd | | ||
//| https://github.com/EA31337 | | ||
//+------------------------------------------------------------------+ | ||
|
||
/* | ||
* This file is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
// Properties. | ||
#property strict | ||
|
||
// Prevents processing this includes file for the second time. | ||
#ifndef WEB_MQH | ||
#define WEB_MQH | ||
|
||
struct WebRequestParams { | ||
string method; | ||
string url; | ||
string headers; | ||
string cookie; | ||
string referer; | ||
int timeout; | ||
WebRequestParams(string _m, string _u, string _h = NULL, int _t = 500) | ||
: method(_m), url(_u), headers(_h), timeout(_t) {}; | ||
WebRequestParams(string _m, string _u, string _c, string _r = NULL, int _t = 500) | ||
: method(_m), url(_u), cookie(_c), referer(_r), timeout(_t) {}; | ||
WebRequestParams(string _u, string _h = NULL, int _t = 500) | ||
: method("GET"), url(_u), headers(_h), timeout(_t) {}; | ||
}; | ||
|
||
struct WebRequestResult { | ||
char data[]; | ||
char result[]; | ||
int error; | ||
int http_code; // HTTP server response code or -1 for an error. | ||
string headers; | ||
}; | ||
|
||
/** | ||
* Implements Web class. | ||
*/ | ||
class Web { | ||
|
||
private: | ||
|
||
public: | ||
|
||
/** | ||
* Class constructor. | ||
*/ | ||
Web() { | ||
} | ||
|
||
/** | ||
* Web request. | ||
* | ||
* Note: Make sure to add URL to the list of allowed URLs in the Terminal. | ||
*/ | ||
WebRequestResult Request(const WebRequestParams &_rp) { | ||
ResetLastError(); | ||
WebRequestResult _res; | ||
if (StringLen(_rp.headers) > 0) { | ||
_res.http_code = WebRequest( | ||
_rp.method, _rp.url, _rp.headers, _rp.timeout, | ||
_res.data, _res.result, _res.headers); | ||
} | ||
else { | ||
int _size = 0; // @fixme: data[] array size in bytes. | ||
_res.http_code = WebRequest( | ||
_rp.method, _rp.url, _rp.cookie, _rp.referer, _rp.timeout, | ||
_res.data, _size, _res.result, _res.headers); | ||
} | ||
_res.error = GetLastError(); | ||
return _res; | ||
} | ||
|
||
}; | ||
#endif |
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,28 @@ | ||
//+------------------------------------------------------------------+ | ||
//| EA31337 framework | | ||
//| Copyright 2016-2019, 31337 Investments Ltd | | ||
//| https://github.com/EA31337 | | ||
//+------------------------------------------------------------------+ | ||
|
||
/* | ||
* This file is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* Test functionality of Web class. | ||
*/ | ||
|
||
// Includes. | ||
#include "WebTest.mq5" |
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,67 @@ | ||
//+------------------------------------------------------------------+ | ||
//| EA31337 framework | | ||
//| Copyright 2016-2019, 31337 Investments Ltd | | ||
//| https://github.com/EA31337 | | ||
//+------------------------------------------------------------------+ | ||
|
||
/* | ||
* This file is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* Test functionality of Web class. | ||
*/ | ||
|
||
// Includes. | ||
#include "../Web.mqh" | ||
#include "../Test.mqh" | ||
#include "../Terminal.mqh" | ||
|
||
// Properties. | ||
#property strict | ||
|
||
/** | ||
* Implements OnInit(). | ||
*/ | ||
int OnInit() { | ||
// Test 1. | ||
Web *web1 = new Web(); | ||
WebRequestParams req1("http://example.com"); | ||
WebRequestResult _res1; | ||
_res1 = web1.Request(req1); | ||
if (_res1.http_code > 0) { | ||
PrintFormat("Web1: HTTP Response Code: %d", _res1.http_code); | ||
PrintFormat("Web1: Headers: %s", _res1.headers); | ||
} else { | ||
PrintFormat("%s(): Error: Code: %d, Message: %s", __FUNCTION__, _res1.error, Terminal::GetErrorText(_res1.error)); | ||
return (INIT_FAILED); | ||
} | ||
delete web1; | ||
|
||
// Test 2. | ||
Web *web2 = new Web(); | ||
WebRequestParams req2("https://example.com"); | ||
WebRequestResult _res2; | ||
_res2 = web2.Request(req2); | ||
if (_res2.http_code > 0) { | ||
PrintFormat("Web1: HTTP Response Code: %d", _res2.http_code); | ||
PrintFormat("Web1: Headers: %s", _res2.headers); | ||
} else { | ||
PrintFormat("%s(): Error: Code: %d, Message: %s", __FUNCTION__, _res2.error, Terminal::GetErrorText(_res2.error)); | ||
return (INIT_FAILED); | ||
} | ||
|
||
return (INIT_SUCCEEDED); | ||
} |
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