| 59 | }; |
| 60 | |
| 61 | bool VariableQueryHandler::HandleRequest( |
| 62 | const WaitGroup::Ptr&, |
| 63 | const HttpApiRequest& request, |
| 64 | HttpApiResponse& response, |
| 65 | boost::asio::yield_context& yc |
| 66 | ) |
| 67 | { |
| 68 | namespace http = boost::beast::http; |
| 69 | |
| 70 | auto url = request.Url(); |
| 71 | auto user = request.User(); |
| 72 | auto params = request.Params(); |
| 73 | |
| 74 | if (url->GetPath().size() > 3) |
| 75 | return false; |
| 76 | |
| 77 | if (request.method() != http::verb::get) |
| 78 | return false; |
| 79 | |
| 80 | QueryDescription qd; |
| 81 | qd.Types.insert("Variable"); |
| 82 | qd.Permission = "variables"; |
| 83 | qd.Provider = new VariableTargetProvider(); |
| 84 | |
| 85 | params->Set("type", "Variable"); |
| 86 | |
| 87 | if (url->GetPath().size() >= 3) |
| 88 | params->Set("variable", url->GetPath()[2]); |
| 89 | |
| 90 | std::vector<Value> objs; |
| 91 | |
| 92 | try { |
| 93 | objs = FilterUtility::GetFilterTargets(qd, params, user, "variable"); |
| 94 | } catch (const MissingPermissionError& ex) { |
| 95 | HttpUtility::SendJsonError(response, params, 403, ex.what()); |
| 96 | return true; |
| 97 | } catch (const std::exception& ex) { |
| 98 | HttpUtility::SendJsonError(response, params, 404, |
| 99 | "No variables found.", |
| 100 | DiagnosticInformation(ex)); |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | auto generatorFunc = [](const Dictionary::Ptr& var) -> Value { |
| 105 | return new Dictionary{ |
| 106 | { "name", var->Get("name") }, |
| 107 | { "type", var->Get("type") }, |
| 108 | { "value", Serialize(var->Get("value"), 0) } |
| 109 | }; |
| 110 | }; |
| 111 | |
| 112 | Dictionary::Ptr result = new Dictionary{{"results", new ValueGenerator{objs, generatorFunc}}}; |
| 113 | result->Freeze(); |
| 114 | |
| 115 | response.result(http::status::ok); |
| 116 | HttpUtility::SendJsonBody(response, params, result, yc); |
| 117 | |
| 118 | return true; |