Google APIs Client Library for C++
|
00001 /* 00002 * \copyright Copyright 2013 Google Inc. All Rights Reserved. 00003 * \license @{ 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 * 00017 * @} 00018 */ 00019 00020 // Author: ewiseblatt@google.com (Eric Wiseblatt) 00021 00022 #ifndef APISERVING_CLIENTS_CPP_UTIL_URI_TEMPLATE_H_ 00023 #define APISERVING_CLIENTS_CPP_UTIL_URI_TEMPLATE_H_ 00024 #include <set> 00025 using std::multiset; 00026 using std::set; 00027 #include <string> 00028 using std::string; 00029 #include "googleapis/base/callback.h" 00030 #include "googleapis/strings/stringpiece.h" 00031 #include "googleapis/util/status.h" 00032 namespace googleapis { 00033 00034 namespace client { 00035 00036 struct UriTemplateConfig; 00037 00038 /* 00039 * Provides the ability to produce concrete URLs from templated ones. 00040 * @ingroup PlatformLayerUri 00041 * 00042 * The UriTemplate class produces concrete URLs required to make HTTP 00043 * invocations from the templated URIs following the 00044 * <a href='http://tools.ietf.org/html/rfc6570'>RFC 6570 URI Template</a> 00045 * standard commonly used by REST services on the Google Cloud Platform. 00046 * 00047 * This class is a collection of static methods that build up the URL 00048 * from a URI template by calling an AppendVariableCallback to resolve 00049 * the templated variables that it encounters. 00050 * 00051 * The callback is always a repeatable one created with NewPermanentCallback. 00052 * 00053 * @see NewPermanentCallback 00054 * @see Expand 00055 */ 00056 class UriTemplate { 00057 public: 00058 /* 00059 * Serves as a helper function for supplying variable values. 00060 * 00061 * This callback should call the appropriate Append* methods with the 00062 * value and target string. If the value is a map or list then also pass the 00063 * opaque UriTemplateConfig parameter. 00064 * 00065 * If the variable is a list of map then the callback should use the 00066 * Append*First method to add the first element (if any) then call 00067 * Append*Next for each of the remaining elements (if any). 00068 * 00069 * @param[in] StringPiece The name of the variable to append. 00070 * @param[in] UriTemplateConfig Opaque pass through argument. 00071 * @param[in,out] The target string to append to 00072 * @return success if the variabel coudl be resolved, or failure if not. 00073 */ 00074 typedef ResultCallback3< util::Status, 00075 const StringPiece&, 00076 const UriTemplateConfig&, 00077 string*> 00078 AppendVariableCallback; 00079 00080 /* 00081 * Expand variables without collecting their names. 00082 * 00083 * @param[in] uri The templated uri to expand. 00084 * @param[in] supplier The callback that supplies variable values. 00085 * @param[out] target The string to resolve the templated uri into. 00086 * 00087 * @return success if all the variables found could be resolved. 00088 * failure if some unresovled variables still remain in the 00089 * target string. 00090 */ 00091 static util::Status Expand( 00092 const StringPiece& uri, 00093 AppendVariableCallback* supplier, 00094 string* target) { 00095 return Expand(uri, supplier, target, NULL); 00096 } 00097 00098 /* 00099 * Expand variables. 00100 * 00101 * @param[in] uri The templated uri to expand. 00102 * @param[in] supplier The callback that supplies variable values. 00103 * @param[out] target The string to resolve the templated uri into. 00104 * @param[out] found_parameters Populated with the list of parameter names 00105 * that were resolved while expanding the uri. NULL is permitted 00106 * if the caller does not wish to collect these. 00107 * 00108 * @return success if all the variables found could be resolved. 00109 * failure if some unresovled variables still remain in the 00110 * target string. 00111 */ 00112 static util::Status Expand( 00113 const StringPiece& uri, 00114 AppendVariableCallback* supplier, 00115 string* target, 00116 set<StringPiece>* found_parameters); 00117 00118 /* 00119 * Appends the first value of a list. 00120 * 00121 * This method is intended to support AppendVariableCallback. 00122 * 00123 * @param[in] value The first value. 00124 * @param[in] config The opaque passthrough parameter from the 00125 * AppendVariableCallback. 00126 * @param[in,out] target The string to append the value to. 00127 */ 00128 static void AppendListFirst( 00129 const StringPiece& value, 00130 const UriTemplateConfig& config, 00131 string* target); 00132 00133 /* 00134 * Appends the value of a list, other than the first. 00135 * 00136 * This method is intended to support AppendVariableCallback. 00137 * 00138 * @param[in] value The element value to append. 00139 * @param[in] config The opaque passthrough parameter from the 00140 * AppendVariableCallback. 00141 * @param[in,out] target The string to append the value to. 00142 */ 00143 static void AppendListNext( 00144 const StringPiece& value, 00145 const UriTemplateConfig& config, 00146 string* target); 00147 00148 /* 00149 * Appends the first key,value pair of a map. 00150 * 00151 * This method is intended to support AppendVariableCallback. 00152 * 00153 * @param[in] key The first key. 00154 * @param[in] value The value of the first key. 00155 * @param[in] config The opaque passthrough parameter from the 00156 * AppendVariableCallback. 00157 * @param[in,out] target The string to append the value to. 00158 */ 00159 static void AppendMapFirst( 00160 const StringPiece& key, 00161 const StringPiece& value, 00162 const UriTemplateConfig& config, 00163 string* target); 00164 00165 /* 00166 * Appends a key,value pair of a map, other than the first. 00167 * 00168 * This method is intended to support AppendVariableCallback. 00169 * 00170 * @param[in] key The element's key to append. 00171 * @param[in] value The value of the key. 00172 * @param[in] config The opaque passthrough parameter from the 00173 * AppendVariableCallback. 00174 * @param[in,out] target The string to append the value to. 00175 */ 00176 static void AppendMapNext( 00177 const StringPiece& key, 00178 const StringPiece& value, 00179 const UriTemplateConfig& config, 00180 string* target); 00181 00182 private: 00183 UriTemplate(); // Is never instantiated. 00184 ~UriTemplate(); 00185 }; 00186 00187 } // namespace client 00188 00189 } // namespace googleapis 00190 #endif // APISERVING_CLIENTS_CPP_UTIL_URI_TEMPLATE_H_