1 /*
2  * Copyright 2009-2017 Alibaba Cloud All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <alibabacloud/oss/model/ObjectCallbackBuilder.h>
19 #include <sstream>
20 #include "utils/Utils.h"
21 
22 using namespace AlibabaCloud::OSS;
23 
ObjectCallbackBuilder(const std::string & url,const std::string & body)24 ObjectCallbackBuilder::ObjectCallbackBuilder(const std::string &url, const std::string &body):
25     ObjectCallbackBuilder(url, body, "", Type::URL)
26 {
27 }
28 
ObjectCallbackBuilder(const std::string & url,const std::string & body,const std::string & host,Type type)29 ObjectCallbackBuilder::ObjectCallbackBuilder(const std::string &url, const std::string &body,
30     const std::string &host, Type type):
31     callbackUrl_(url),
32     callbackHost_(host),
33     callbackBody_(body),
34     callbackBodyType_(type)
35 {
36 }
37 
build()38 std::string ObjectCallbackBuilder::build()
39 {
40     if (callbackUrl_.empty() || callbackBody_.empty())
41     {
42         return "";
43     }
44 
45     std::stringstream ss;
46     ss << "{";
47     ss << "\"callbackUrl\":\"" << callbackUrl_ << "\"";
48     if (!callbackHost_.empty())
49     {
50         ss << ",\"callbackHost\":\"" << callbackHost_ << "\"";
51     }
52     ss << ",\"callbackBody\":\"" << callbackBody_ << "\"";
53 
54     if (callbackBodyType_ == Type::JSON)
55     {
56         ss << ",\"callbackBodyType\":\"" << "application/json" << "\"";
57     }
58 
59     ss << "}";
60 
61     return Base64Encode(ss.str());
62 }
63 
64 
addCallbackVariable(const std::string & key,const std::string & value)65 bool ObjectCallbackVariableBuilder::addCallbackVariable(const std::string &key, const std::string &value)
66 {
67     if (!!key.compare(0, 2 , "x:", 2))
68     {
69         return false;
70     }
71     callbackVariable_[key] = value;
72     return true;
73 }
74 
build()75 std::string ObjectCallbackVariableBuilder::build()
76 {
77     if (callbackVariable_.size() == 0)
78     {
79         return "";
80     }
81 
82     std::stringstream ss;
83     ss << "{";
84     int i = 0;
85     for (auto const& var : callbackVariable_) {
86         if (i > 0) {
87             ss << ",";
88         }
89         ss << "\"" << var.first << "\":\"" << var.second << "\"";
90         i++;
91     }
92     ss << "}";
93     return Base64Encode(ss.str());
94 }
95