Skip to content

Commit 1f5d904

Browse files
committed
Implemented requestIpApiCom
1 parent ccac2dc commit 1f5d904

File tree

13 files changed

+122
-27
lines changed

13 files changed

+122
-27
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ addons:
1313
- make
1414
- g++
1515
- pkg-config
16+
- libcurl4-openssl-dev
1617

1718
# Build steps
1819
script:

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,27 @@ C++. Call request to another services for get location coordinates by IP-address
66

77
Example:
88
```
9+
WSJCppGeoIPResult res = WSJCppGeoIP::requestToIpApiCom("1.1.1.1");
10+
if (res.hasError()) {
11+
std::cout << "FAILED: " << res.getErrorDescription() << std::endl;
12+
} else {
13+
std::cout << "Service Name: " << res.getServiceName() << std::endl;
14+
std::cout << "IP Address: " << res.getIpAddress() << std::endl;
15+
std::cout << "Country: " << res.getCountry() << std::endl;
16+
std::cout << "Region Name: " << res.getRegionName() << std::endl;
17+
std::cout << "City: " << res.getCity() << std::endl;
18+
std::cout << "Latitude: " << res.getLatitude() << std::endl;
19+
std::cout << "Longitude: " << res.getLongitude() << std::endl;
20+
}
21+
```
22+
23+
Output:
24+
```
25+
Service Name: ip-api.com
26+
IP Address: 1.1.1.1
27+
Country: Australia
28+
Region Name: New South Wales
29+
City: Sydney
30+
Latitude: -33.8688
31+
Longitude: 151.209
932
```

src.wsjcpp/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ set (WSJCPP_LIBRARIES "")
1414
set (WSJCPP_INCLUDE_DIRS "")
1515
set (WSJCPP_SOURCES "")
1616

17-
# wsjcpp/wsjcpp-core:v0.0.4
18-
list (APPEND WSJCPP_INCLUDE_DIRS "./src.wsjcpp/wsjcpp_wsjcpp_core/")
19-
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_wsjcpp_core/wsjcpp_core.cpp")
20-
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_wsjcpp_core/wsjcpp_core.h")
17+
# wsjcpp-core:v0.0.4
18+
list (APPEND WSJCPP_INCLUDE_DIRS "./src.wsjcpp/wsjcpp_core/")
19+
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp")
20+
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_core.h")
2121

2222
# nlohmann/json:v3.7.3
2323
list (APPEND WSJCPP_INCLUDE_DIRS "./src.wsjcpp/nlohmann_json/")
File renamed without changes.
File renamed without changes.
File renamed without changes.

src.wsjcpp/wsjcpp_wsjcpp_core/wsjcpp.hold.yml renamed to src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ wsjcpp_version: v0.0.1
22
cmake_cxx_standard: 11
33
cmake_minimum_required: 3.0
44

5-
name: wsjcpp/wsjcpp-core
5+
name: wsjcpp-core
66
version: v0.0.4
77
description: Basic Utils for wsjcpp
88
issues: https://github.com/wsjcpp/wsjcpp-core/issues

src.wsjcpp/wsjcpp_wsjcpp_core/wsjcpp_core.cpp renamed to src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ std::string WSJCppCore::createUuid() {
413413

414414
// ---------------------------------------------------------------------
415415

416-
bool WSJCppCore::isIPv4(std::string& str) {
416+
bool WSJCppCore::isIPv4(const std::string& str) {
417417
int n = 0;
418418
std::string s[4] = {"", "", "", ""};
419419
for (int i = 0; i < str.length(); i++) {
@@ -443,7 +443,7 @@ bool WSJCppCore::isIPv4(std::string& str) {
443443

444444
// ---------------------------------------------------------------------
445445

446-
bool WSJCppCore::isIPv6(std::string& str) {
446+
bool WSJCppCore::isIPv6(const std::string& str) {
447447
unsigned char buf[sizeof(struct in6_addr)];
448448
bool isValid = inet_pton(AF_INET6, str.c_str(), buf);
449449
return isValid;

src.wsjcpp/wsjcpp_wsjcpp_core/wsjcpp_core.h renamed to src.wsjcpp/wsjcpp_core/wsjcpp_core.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ class WSJCppCore {
4848

4949
static void initRandom();
5050
static std::string createUuid();
51-
static bool isIPv4(std::string& str);
52-
static bool isIPv6(std::string& str);
51+
static bool isIPv4(const std::string& str);
52+
static bool isIPv6(const std::string& str);
5353
};
5454

5555

src/main.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,24 @@ int main(int argc, const char* argv[]) {
2525
vArgs.push_back(std::string(argv[i]));
2626
}
2727

28-
/*if (vArgs.size() != 3) {
29-
std::cout << "Usage: " << vArgs[0] << " <string1> <string2>" << std::endl;
28+
if (vArgs.size() != 2) {
29+
std::cout << "Usage: " << vArgs[0] << " <ip-adderss>" << std::endl;
3030
return -1;
3131
}
3232

33-
std::string s1 = vArgs[1];
34-
std::string s2 = vArgs[2];
35-
int nDistance = WSJCppLevenshtein::distance(s1,s2);
36-
std::cout << "" << nDistance << std::endl;*/
33+
std::string sIpAddress = vArgs[1];
34+
WSJCppGeoIPResult res = WSJCppGeoIP::requestToIpApiCom(sIpAddress);
35+
if (res.hasError()) {
36+
std::cout << "FAILED: " << res.getErrorDescription() << std::endl;
37+
} else {
38+
std::cout << "Service Name: " << res.getServiceName() << std::endl;
39+
std::cout << "IP Address: " << res.getIpAddress() << std::endl;
40+
std::cout << "Country: " << res.getCountry() << std::endl;
41+
std::cout << "Region Name: " << res.getRegionName() << std::endl;
42+
std::cout << "City: " << res.getCity() << std::endl;
43+
std::cout << "Latitude: " << res.getLatitude() << std::endl;
44+
std::cout << "Longitude: " << res.getLongitude() << std::endl;
45+
}
46+
// std::cout << "" << nDistance << std::endl;
3747
return 0;
3848
}

0 commit comments

Comments
 (0)