/
CatalogAlgorithmHelper.cpp
59 lines (53 loc) · 2.24 KB
/
CatalogAlgorithmHelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "MantidICat/CatalogAlgorithmHelper.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/assign/list_of.hpp>
namespace Mantid
{
namespace ICat
{
/**
* Create a catalog to use in the algorithms.
* @return A pointer to the catalog class.
*/
API::ICatalog_sptr CatalogAlgorithmHelper::createCatalog()
{
API::ICatalog_sptr catalog;
try
{
catalog = API::CatalogFactory::Instance().create(Kernel::ConfigService::Instance().getFacility().catalogInfo().catalogName());
}
catch(Kernel::Exception::NotFoundError&)
{
throw std::runtime_error("Your current Facility: " + Kernel::ConfigService::Instance().getFacility().name() + " does not have catalog information.\n");
}
return catalog;
}
/**
* Obtain the error message returned by the IDS.
* @param HTTPStatus :: The HTTPStatus returned by the IDS.
* @param responseStream :: The contents of the stream (a JSON stream) returned from the IDS.
* @returns An appropriate error message for the user if it exists. Otherwise an empty string.
*/
const std::string CatalogAlgorithmHelper::getIDSError(Poco::Net::HTTPResponse::HTTPStatus &HTTPStatus, std::istream& responseStream)
{
std::set<Poco::Net::HTTPResponse::HTTPStatus> successHTTPStatus = boost::assign::list_of
(Poco::Net::HTTPResponse::HTTPStatus::HTTP_OK)
(Poco::Net::HTTPResponse::HTTPStatus::HTTP_CREATED)
(Poco::Net::HTTPResponse::HTTPStatus::HTTP_ACCEPTED);
// Cancel the algorithm and output message if status returned
// from the server is not in our successHTTPStatus set.
if (successHTTPStatus.find(HTTPStatus) == successHTTPStatus.end())
{
// Stores the contents of `jsonResponseData` as a json property tree.
boost::property_tree::ptree json;
// Convert the stream to a JSON tree.
boost::property_tree::read_json(responseStream, json);
// Return the message returned by the server.
return json.get<std::string>("code") + ": " + json.get<std::string>("message");
}
// No error occurred, so return an empty string for verification.
return "";
}
}
}