Skip to content
This repository has been archived by the owner on Jul 3, 2022. It is now read-only.

Commit

Permalink
Merge branch 'test-service'.
Browse files Browse the repository at this point in the history
  • Loading branch information
s3rvac committed Mar 14, 2016
2 parents a695518 + a66e1c7 commit 41da2f7
Show file tree
Hide file tree
Showing 23 changed files with 471 additions and 48 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ dev
* Added basic support for the [file-analyzing
service](https://retdec.com/api/docs/fileinfo.html). Also added a sample tool
that uses this service to analyze binary files (`fileinfo`).
* Added basic support for the [test
service](https://retdec.com/api/docs/test.html). It can be used to verify
API-key authentications.
* Added a new subclass of `ApiError`: `AuthError`. It is thrown when there is
an authentication or authorization error.

0.1 (2015-12-06)
----------------
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ Status
------
The library currently provides basic support of the
[decompilation](https://retdec.com/api/docs/decompiler.html) and
[file-analyzing](https://retdec.com/api/docs/fileinfo.html) services.
[decompilation](https://retdec.com/api/docs/decompiler.html),
[file-analyzing](https://retdec.com/api/docs/fileinfo.html), and
[test](https://retdec.com/api/docs/test.html) services.
Requirements
------------
Expand Down
1 change: 1 addition & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(PUBLIC_INCLUDES
retdec/retdec.h
retdec/service.h
retdec/settings.h
retdec/test.h
)

install(FILES ${PUBLIC_INCLUDES} DESTINATION "${INSTALL_INCLUDE_DIR}/retdec")
8 changes: 8 additions & 0 deletions include/retdec/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ class ApiError: public Error {
const std::string description;
};

///
/// Exception thrown when there is an authentication or authorization error.
///
class AuthError: public ApiError {
public:
using ApiError::ApiError;
};

///
/// Base class of resource exceptions.
///
Expand Down
1 change: 1 addition & 0 deletions include/retdec/fwd_decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Analysis;
class AnalysisArguments;
class AnalysisError;
class ApiError;
class AuthError;
class Decompilation;
class DecompilationArguments;
class DecompilationError;
Expand Down
28 changes: 2 additions & 26 deletions include/retdec/internal/service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
#include <memory>
#include <string>

#include <json/json.h>

#include "retdec/internal/connection_manager.h"
#include "retdec/internal/utilities/connection.h"
#include "retdec/settings.h"

Expand All @@ -32,9 +29,8 @@ class ServiceImpl {
public:
ServiceImpl(const Settings &settings,
const std::shared_ptr<ConnectionManager> &connectionManager,
const std::string &serviceName,
const std::string &resourcesName);
virtual ~ServiceImpl();
const std::string &serviceName);
virtual ~ServiceImpl() = 0;

/// @name Request Arguments Creation
/// @{
Expand All @@ -44,23 +40,6 @@ class ServiceImpl {
const ResourceArguments &args) const;
/// @}

///
/// Runs a new resource with the given arguments.
///
template <typename ResourceType>
std::unique_ptr<ResourceType> runResource(const ResourceArguments &args) {
auto conn = connectionManager->newConnection(settings);
auto response = conn->sendPostRequest(
resourcesUrl,
createRequestArguments(args),
createRequestFiles(args)
);
verifyRequestSucceeded(*response);
auto jsonBody = response->bodyAsJson();
auto id = jsonBody.get("id", "?").asString();
return std::make_unique<ResourceType>(id, conn);
}

/// Settings.
const Settings settings;

Expand All @@ -69,9 +48,6 @@ class ServiceImpl {

/// Base URL.
const std::string baseUrl;

/// URL to resources.
const std::string resourcesUrl;
};

} // namespace internal
Expand Down
57 changes: 57 additions & 0 deletions include/retdec/internal/service_with_resources_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
///
/// @file retdec/internal/service_with_resources_impl.h
/// @copyright (c) 2015-2016 by Petr Zemek (s3rvac@gmail.com) and contributors
/// @license MIT, see the @c LICENSE file for more details
/// @brief Base class of private implementation of services with resources.
///

#ifndef RETDEC_INTERNAL_SERVICE_WITH_RESOURCES_IMPL_H
#define RETDEC_INTERNAL_SERVICE_WITH_RESOURCES_IMPL_H

#include <memory>
#include <string>

#include <json/json.h>

#include "retdec/internal/connection_manager.h"
#include "retdec/internal/service_impl.h"

namespace retdec {
namespace internal {

///
/// Base class of private implementation of services.
///
class ServiceWithResourcesImpl: public ServiceImpl {
public:
ServiceWithResourcesImpl(const Settings &settings,
const std::shared_ptr<ConnectionManager> &connectionManager,
const std::string &serviceName,
const std::string &resourcesName);
virtual ~ServiceWithResourcesImpl() override;

///
/// Runs a new resource with the given arguments.
///
template <typename ResourceType>
std::unique_ptr<ResourceType> runResource(const ResourceArguments &args) {
auto conn = connectionManager->newConnection(settings);
auto response = conn->sendPostRequest(
resourcesUrl,
createRequestArguments(args),
createRequestFiles(args)
);
verifyRequestSucceeded(*response);
auto jsonBody = response->bodyAsJson();
auto id = jsonBody.get("id", "?").asString();
return std::make_unique<ResourceType>(id, conn);
}

/// URL to resources.
const std::string resourcesUrl;
};

} // namespace internal
} // namespace retdec

#endif
2 changes: 1 addition & 1 deletion include/retdec/internal/utilities/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void verifyRequestSucceeded(const Connection::Response &response);
///
/// This class wraps an existing connection. Then, when a response from a
/// GET/POST request is received, it verifies that the request succeeded. If it
/// failed, it throws ApiError.
/// failed, it throws ApiError or its subclass.
///
class ResponseVerifyingConnection: public Connection {
public:
Expand Down
2 changes: 2 additions & 0 deletions include/retdec/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#ifndef RETDEC_SERVICE_H
#define RETDEC_SERVICE_H

#include <memory>

namespace retdec {

namespace internal {
Expand Down
53 changes: 53 additions & 0 deletions include/retdec/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
///
/// @file retdec/test.h
/// @copyright (c) 2015-2016 by Petr Zemek (s3rvac@gmail.com) and contributors
/// @license MIT, see the @c LICENSE file for more details
/// @brief Testing service.
///

#ifndef RETDEC_TEST_H
#define RETDEC_TEST_H

#include <memory>

#include "retdec/service.h"

namespace retdec {

class Settings;

namespace internal {

class ConnectionManager;
class TestImpl;

} // namespace internal

///
/// Runner of analyses.
///
class Test: public Service {
public:
/// @name Construction and Destruction
/// @{
Test(const Settings &settings);
/// @cond internal
Test(const Settings &settings,
const std::shared_ptr<::retdec::internal::ConnectionManager> &connectionManager);
/// @endcond
virtual ~Test() override;
/// @}

/// @name Testing
/// @{
void auth();
/// @}

private:
internal::TestImpl *impl() noexcept;
const internal::TestImpl *impl() const noexcept;
};

} // namespace retdec

#endif
2 changes: 2 additions & 0 deletions src/retdec/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set(RETDEC_SOURCES
internal/files/string_file.cpp
internal/resource_impl.cpp
internal/service_impl.cpp
internal/service_with_resources_impl.cpp
internal/utilities/connection.cpp
internal/utilities/json.cpp
internal/utilities/os.cpp
Expand All @@ -31,6 +32,7 @@ set(RETDEC_SOURCES
resource_arguments.cpp
service.cpp
settings.cpp
test.cpp
)

add_library(retdec ${RETDEC_SOURCES})
Expand Down
15 changes: 10 additions & 5 deletions src/retdec/decompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
/// @file retdec/decompiler.cpp
/// @copyright (c) 2015-2016 by Petr Zemek (s3rvac@gmail.com) and contributors
/// @license MIT, see the @c LICENSE file for more details
/// @brief Implementation of the decompiler.
/// @brief Implementation of the decompilation service.
///

#include "retdec/decompilation.h"
#include "retdec/decompilation_arguments.h"
#include "retdec/decompiler.h"
#include "retdec/internal/connection_managers/real_connection_manager.h"
#include "retdec/internal/service_impl.h"
#include "retdec/internal/service_with_resources_impl.h"
#include "retdec/settings.h"

using namespace retdec::internal;
Expand All @@ -20,7 +20,7 @@ namespace internal {
///
/// Private implementation of Decompiler.
///
class DecompilerImpl: public ServiceImpl {
class DecompilerImpl: public ServiceWithResourcesImpl {
public:
DecompilerImpl(const Settings &settings,
const std::shared_ptr<ConnectionManager> &connectionManager);
Expand All @@ -35,10 +35,15 @@ class DecompilerImpl: public ServiceImpl {
///
DecompilerImpl::DecompilerImpl(const Settings &settings,
const std::shared_ptr<ConnectionManager> &connectionManager):
ServiceImpl(settings, connectionManager, "decompiler", "decompilations") {}
ServiceWithResourcesImpl(
settings,
connectionManager,
"decompiler",
"decompilations"
) {}

// Override.
DecompilerImpl::~DecompilerImpl() {}
DecompilerImpl::~DecompilerImpl() = default;

} // namespace internal

Expand Down
15 changes: 10 additions & 5 deletions src/retdec/fileinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
/// @file retdec/fileinfo.cpp
/// @copyright (c) 2015-2016 by Petr Zemek (s3rvac@gmail.com) and contributors
/// @license MIT, see the @c LICENSE file for more details
/// @brief Implementation of the fileinfo.
/// @brief Implementation of the fileinfo service.
///

#include "retdec/analysis.h"
#include "retdec/analysis_arguments.h"
#include "retdec/fileinfo.h"
#include "retdec/internal/connection_managers/real_connection_manager.h"
#include "retdec/internal/service_impl.h"
#include "retdec/internal/service_with_resources_impl.h"
#include "retdec/settings.h"

using namespace retdec::internal;
Expand All @@ -20,7 +20,7 @@ namespace internal {
///
/// Private implementation of Fileinfo.
///
class FileinfoImpl: public ServiceImpl {
class FileinfoImpl: public ServiceWithResourcesImpl {
public:
FileinfoImpl(const Settings &settings,
const std::shared_ptr<ConnectionManager> &connectionManager);
Expand All @@ -35,10 +35,15 @@ class FileinfoImpl: public ServiceImpl {
///
FileinfoImpl::FileinfoImpl(const Settings &settings,
const std::shared_ptr<ConnectionManager> &connectionManager):
ServiceImpl(settings, connectionManager, "fileinfo", "analyses") {}
ServiceWithResourcesImpl(
settings,
connectionManager,
"fileinfo",
"analyses"
) {}

// Override.
FileinfoImpl::~FileinfoImpl() {}
FileinfoImpl::~FileinfoImpl() = default;

} // namespace internal

Expand Down
7 changes: 2 additions & 5 deletions src/retdec/internal/service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ namespace internal {
/// @param[in] settings Settings for the service.
/// @param[in] connectionManager Manager of connections.
/// @param[in] serviceName Name of the service.
/// @param[in] resourcesName Name of the resources (plural).
///
ServiceImpl::ServiceImpl(const Settings &settings,
const std::shared_ptr<ConnectionManager> &connectionManager,
const std::string &serviceName,
const std::string &resourcesName):
const std::string &serviceName):
settings(settings),
connectionManager(connectionManager),
baseUrl(settings.apiUrl() + "/" + serviceName),
resourcesUrl(baseUrl + "/" + resourcesName) {}
baseUrl(settings.apiUrl() + "/" + serviceName) {}

///
/// Destructs the private implementation.
Expand Down
35 changes: 35 additions & 0 deletions src/retdec/internal/service_with_resources_impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
///
/// @file retdec/internal/service_with_resources_impl.cpp
/// @copyright (c) 2015-2016 by Petr Zemek (s3rvac@gmail.com) and contributors
/// @license MIT, see the @c LICENSE file for more details
/// @brief Implementation of the base class of private implementations of
/// services with resources.
///

#include "retdec/internal/service_with_resources_impl.h"

namespace retdec {
namespace internal {

///
/// Constructs a private implementation.
///
/// @param[in] settings Settings for the service.
/// @param[in] connectionManager Manager of connections.
/// @param[in] serviceName Name of the service.
/// @param[in] resourcesName Name of the resources (plural).
///
ServiceWithResourcesImpl::ServiceWithResourcesImpl(const Settings &settings,
const std::shared_ptr<ConnectionManager> &connectionManager,
const std::string &serviceName,
const std::string &resourcesName):
ServiceImpl(settings, connectionManager, serviceName),
resourcesUrl(baseUrl + "/" + resourcesName) {}

///
/// Destructs the private implementation.
///
ServiceWithResourcesImpl::~ServiceWithResourcesImpl() = default;

} // namespace internal
} // namespace retdec
Loading

0 comments on commit 41da2f7

Please sign in to comment.