This repository has been archived by the owner on Jan 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #38 from The-B1T-Foundation/added_meme_api
Added meme api
- Loading branch information
Showing
13 changed files
with
336 additions
and
7 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
87 changes: 87 additions & 0 deletions
87
source/business_logic/controller/api/meme_api/meme_api_controller.cpp
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,87 @@ | ||
// MIT License | ||
|
||
// Copyright (c) 2024 The B1T Foundation | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
|
||
#include "meme_api_controller.hpp" | ||
|
||
// --------------------------------------------------------------------------------------------------------------------- | ||
nljson AMeme_API_Controller::Json_Response{}; | ||
|
||
// --------------------------------------------------------------------------------------------------------------------- | ||
AMeme_API_Controller::AMeme_API_Controller(const AMeme_API_Config& meme_api_config) : | ||
ABase_API_Controller{ "https://programming-memes-images.p.rapidapi.com/v1/memes" }, Headers{ nullptr } | ||
{ | ||
Headers = curl_slist_append(Headers, std::format("X-RapidAPI-Key: {}", meme_api_config.Get_API_Key()).c_str()); | ||
Headers = curl_slist_append(Headers, std::format("X-RapidAPI-Host: {}", meme_api_config.Get_API_Host()).c_str()); | ||
} | ||
|
||
// --------------------------------------------------------------------------------------------------------------------- | ||
AMeme_API_Controller::~AMeme_API_Controller() | ||
{ | ||
curl_slist_free_all(Headers); | ||
} | ||
|
||
// --------------------------------------------------------------------------------------------------------------------- | ||
std::string AMeme_API_Controller::Get_Meme() | ||
{ | ||
if (Meme_Links_Stack.empty()) | ||
{ | ||
Send_Request("GET"); | ||
|
||
for (std::size_t i{}; !Json_Response.empty() && i < 12; ++i) // 12 - default count of meme links | ||
{ | ||
Meme_Links_Stack.push(Json_Response[i]["image"]); | ||
} | ||
} | ||
|
||
auto tmp{ Meme_Links_Stack.top() }; | ||
Meme_Links_Stack.pop(); | ||
|
||
return tmp; | ||
} | ||
|
||
// --------------------------------------------------------------------------------------------------------------------- | ||
bool AMeme_API_Controller::Send_Request(std::string_view request_type) | ||
{ | ||
if (Curl = curl_easy_init(); Curl != nullptr) | ||
{ | ||
curl_easy_setopt(Curl, CURLOPT_URL, Url.data()); | ||
curl_easy_setopt(Curl, CURLOPT_CUSTOMREQUEST, request_type.data()); | ||
curl_easy_setopt(Curl, CURLOPT_HTTPHEADER, Headers); | ||
curl_easy_setopt(Curl, CURLOPT_WRITEFUNCTION, &ABase_API_Controller::Write_Callback); | ||
curl_easy_setopt(Curl, CURLOPT_WRITEDATA, &AMeme_API_Controller::Json_Response); | ||
|
||
curl_easy_perform(Curl); | ||
|
||
std::size_t resp_code{}; | ||
curl_easy_getinfo(Curl, CURLINFO_RESPONSE_CODE, &resp_code); | ||
|
||
curl_easy_cleanup(Curl); | ||
|
||
if (resp_code != EHTTP_STATUS::OK) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return Curl != nullptr; | ||
} |
50 changes: 50 additions & 0 deletions
50
source/business_logic/controller/api/meme_api/meme_api_controller.hpp
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,50 @@ | ||
// MIT License | ||
|
||
// Copyright (c) 2024 The B1T Foundation | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
|
||
#pragma once | ||
|
||
#include <format> | ||
#include <stack> | ||
#include <vector> | ||
#include <string> | ||
|
||
#include <controller/api/base_api/base_api_controller.hpp> | ||
|
||
#include <model/config/meme_api_config/meme_api_config_model.hpp> | ||
|
||
class AMeme_API_Controller : public ABase_API_Controller | ||
{ | ||
public: | ||
explicit AMeme_API_Controller(const AMeme_API_Config& meme_api_config); | ||
~AMeme_API_Controller() override; | ||
|
||
std::string Get_Meme(); | ||
|
||
private: | ||
bool Send_Request(std::string_view request_type); | ||
|
||
private: | ||
std::stack<std::string, std::vector<std::string>> Meme_Links_Stack; | ||
static nljson Json_Response; | ||
curl_slist* Headers; | ||
}; |
42 changes: 42 additions & 0 deletions
42
source/business_logic/controller/config/meme_api_config/meme_api_config_controller.cpp
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,42 @@ | ||
// MIT License | ||
|
||
// Copyright (c) 2024 The B1T Foundation | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
|
||
#include "meme_api_config_controller.hpp" | ||
|
||
// --------------------------------------------------------------------------------------------------------------------- | ||
std::optional<AMeme_API_Config> AMeme_API_Config_Controller::Load_Config() | ||
{ | ||
char* api_key{ std::getenv("MEME_API_KEY") }; | ||
if (!api_key) | ||
{ | ||
return std::nullopt; | ||
} | ||
|
||
char* api_host{ std::getenv("MEME_API_HOST") }; | ||
if (!api_host) | ||
{ | ||
return std::nullopt; | ||
} | ||
|
||
return AMeme_API_Config{ api_key, api_host }; | ||
} |
37 changes: 37 additions & 0 deletions
37
source/business_logic/controller/config/meme_api_config/meme_api_config_controller.hpp
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,37 @@ | ||
// MIT License | ||
|
||
// Copyright (c) 2024 The B1T Foundation | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
|
||
#pragma once | ||
|
||
#include <optional> | ||
|
||
#include <model/config/meme_api_config/meme_api_config_model.hpp> | ||
|
||
class AMeme_API_Config_Controller | ||
{ | ||
public: | ||
constexpr AMeme_API_Config_Controller() = default; | ||
constexpr ~AMeme_API_Config_Controller() = default; | ||
|
||
static std::optional<AMeme_API_Config> Load_Config(); | ||
}; |
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
41 changes: 41 additions & 0 deletions
41
source/business_logic/model/config/meme_api_config/meme_api_config_model.cpp
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,41 @@ | ||
// MIT License | ||
|
||
// Copyright (c) 2024 The B1T Foundation | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
|
||
#include "meme_api_config_model.hpp" | ||
|
||
// --------------------------------------------------------------------------------------------------------------------- | ||
AMeme_API_Config::AMeme_API_Config(std::string_view api_key, std::string_view api_host) : | ||
API_Key{ api_key }, API_Host{ api_host } | ||
{ } | ||
|
||
// --------------------------------------------------------------------------------------------------------------------- | ||
std::string_view AMeme_API_Config::Get_API_Key() const | ||
{ | ||
return API_Key; | ||
} | ||
|
||
// --------------------------------------------------------------------------------------------------------------------- | ||
std::string_view AMeme_API_Config::Get_API_Host() const | ||
{ | ||
return API_Host; | ||
} |
41 changes: 41 additions & 0 deletions
41
source/business_logic/model/config/meme_api_config/meme_api_config_model.hpp
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,41 @@ | ||
// MIT License | ||
|
||
// Copyright (c) 2024 The B1T Foundation | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <string> | ||
|
||
class AMeme_API_Config | ||
{ | ||
public: | ||
explicit AMeme_API_Config(std::string_view api_key, std::string_view api_host); | ||
constexpr ~AMeme_API_Config() = default; | ||
|
||
[[nodiscard]] std::string_view Get_API_Key() const; | ||
[[nodiscard]] std::string_view Get_API_Host() const; | ||
|
||
private: | ||
std::string_view API_Key; | ||
std::string_view API_Host; | ||
}; |
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
Oops, something went wrong.