Sample 7.1 - Play ALL actions in .atn file.
一、定义
样本7.1-在.atn文件中播放所有动作
export token=<YOUR_TOKEN>
export api_key =<YOUR_API_KEY>
curl -H "Authorization: Bearer $token" -H "x-api-key: $api_key" https://image.adobe.io/pie/psdService/photoshopActions -d '{
"inputs": [
{
"href": "https://as2.ftcdn.net/jpg/02/49/48/49/500_F_249484911_JifPIzjUqzkRhcdMkF9GnsUI9zaqdAsn.jpg",
"storage": "external"
}
],
"options": {
"actions": [
{
"href": "https://raw.githubusercontent.com/johnleetran/ps-actions-samples/master/actions/Oil-paint.atn",
"storage": "external"
}
]
},
"outputs": [
{
"storage": "external",
"type": "image/jpeg",
"href": "https://some-presigned-url/output.jpeg"
}
]
}'
二、c++ 代码
#include <iostream>
#include <curl/curl.h>
#include <json/json.h>
#pragma warning(disable:4996)
using namespace std;
//#define API_TEST
wstring AsciiToUnicode(const string& str)
{
// 预算-缓冲区中宽字节的长度
int unicodeLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0);
// 给指向缓冲区的指针变量分配内存
wchar_t *pUnicode = (wchar_t*)malloc(sizeof(wchar_t)*unicodeLen);
// 开始向缓冲区转换字节
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, pUnicode, unicodeLen);
wstring ret_str = pUnicode;
free(pUnicode);
return ret_str;
}
string UnicodeToUtf8(const wstring& wstr)
{
// 预算-缓冲区中多字节的长度
int ansiiLen = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
// 给指向缓冲区的指针变量分配内存
char *pAssii = (char*)malloc(sizeof(char)*ansiiLen);
// 开始向缓冲区转换字节
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, pAssii, ansiiLen, nullptr, nullptr);
string ret_str = pAssii;
free(pAssii);
return ret_str;
}
string AsciiToUtf8(const string& str)
{
return UnicodeToUtf8(AsciiToUnicode(str));
}
/* 不考虑任何异常的情况, 简单的get请求 */
/***
* buffer 接收数据所在的缓冲区
* size 要读取的字节数
* count 读写size长度的数据count次
* response 用户自定义文件指针
*/
size_t getUrlResponse(char* buffer, size_t size, size_t count, string* response) {
size_t recv_size = size * count;
response->clear();
response->append(buffer);
return recv_size;
}
string get(const string& url) {
// 请求数据
string response;
// easy handle声明
CURL *handle;
// 初始化handle
handle = curl_easy_init();
// 设置url
curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
//设置curl的请求头
struct curl_slist* header_list = NULL;
#ifndef API_TEST
header_list = curl_slist_append(header_list, "Content-Type:application/json");
#endif
header_list = curl_slist_append(header_list, "Authorization:Bearer eyJhbGciOiJSUzI1NiIsIng1dSI6Imltc19uYTEta2V5LTEuY2VyIn0.eyJpZCI6IjE2MjA4OTYxMzE4MzlfZDY0NThhYzQtZGVlMC00YjdiLTg1YWEtNjBjMTczMjljN2ZjX3VlMSIsImNsaWVudF9pZCI6ImUyMDk2MTc3ZTEzZjRhZDg5YzliZGE0ODliNzgyNGUxIiwidXNlcl9pZCI6IjUwODc3NjgxNjA4Njg2M0EwQTQ5NUM3MUB0ZWNoYWNjdC5hZG9iZS5jb20iLCJ0eXBlIjoiYWNjZXNzX3Rva2VuIiwiYXMiOiJpbXMtbmExIiwiYWFfaWQiOiI1MDg3NzY4MTYwODY4NjNBMEE0OTVDNzFAdGVjaGFjY3QuYWRvYmUuY29tIiwiZmciOiJWTjI3TzRGWEZMUDU1SEVDU01aTFZIUUFYST09PT09PSIsIm1vaSI6ImFmZTdkNjlkIiwiZXhwaXJlc19pbiI6Ijg2NDAwMDAwIiwic2NvcGUiOiJvcGVuaWQsQWRvYmVJRCxyZWFkX29yZ2FuaXphdGlvbnMiLCJjcmVhdGVkX2F0IjoiMTYyMDg5NjEzMTgzOSJ9.HrsmEVfOHBGVZTnnaHKTbFGglqhn0iovpL22FzgPaOUXNPXTbL4K_PMxKOvQ14i0S-qsy-Kz8W3wFe4jseiIppsB4_kEgxKszyBAf9RvYNDIZiVre3fQkLbqJkiahm-lzOoecRR5thWBguNCITmtCV8Ks3rRnOXkbOD_xelnEz1BlWde_D_loIJaIb7jzV19Y_5tToYWA8plWPknb8-5rpkHpZdjN3NGFAenenQA5tKhWrBEKSBAoGt78E-G7B7QEfWcZ_bOJ1ZF60ZslaaA76g7PiVMhCuS1FDBvGRfec5uiO_JiqNmFc6UmnRo5dP-HVNKnp80e_dvKu27Pr4W_A");
header_list = curl_slist_append(header_list, "x-api-key:e2096177e13f4ad89c9bda489b7824e1");
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, header_list);
//设置ssl验证
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, false);
// 构建json数组
Json::Value obj;
Json::Value arrayinputs;
Json::Value arrayoutputs;
Json::Value inputsroot;
Json::Value outputsroot;
Json::Value inputs;
Json::Value outputs;
Json::Value actionsroot;
Json::Value optionsroot;
Json::Value options;
Json::FastWriter writer;
//https://www.dropbox.com/s/zn8gj1kzktnmp4t/Example.psd?dl=0
//https://github.com/greless/photoshop-api-docs-pre-release/raw/master/sample_files/Example.psd
//https://dl-web.dropbox.com/account_photo/get/dbaphid%3AAABz1l-fZg8hPvIe63x3-neHllYtVpQPxNQ?size=128x128&vers=1620873384663
inputs["href"] = "https://as2.ftcdn.net/jpg/02/49/48/49/500_F_249484911_JifPIzjUqzkRhcdMkF9GnsUI9zaqdAsn.jpg";//
inputs["storage"] = "external";
inputsroot.append(inputs);
//obj["inputs"] = Json::Value(inputsroot);
obj["inputs"] = inputsroot;
options["href"] = "https://raw.githubusercontent.com/johnleetran/ps-actions-samples/master/actions/Oil-paint.atn";//
options["storage"] = "external";
actionsroot.append(options);
optionsroot["actions"] = actionsroot;
obj["options"] = optionsroot;
outputs["href"] = "https://some-presigned-url/output.jpeg";
outputs["storage"] = "external";
outputs["type"] = "image/jpeg";
outputsroot.append(outputs);
obj["outputs"] = outputsroot;
// 子节点挂到根节点上
//array["array"] = Json::Value(root);
//obj.append(arrayinputs);
//obj.append(arrayoutputs);
string data = writer.write(obj);
cout <<"json数据:" << endl << data << endl;
cout << "格式化json数据:" << endl << obj.toStyledString() << endl;
// std::string jsonout = item.toStyledString();
//data = AsciiToUtf8(data);
#ifndef API_TEST
//设置post请求的参数
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, data.c_str());
#endif
// 注册回调函数
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, &getUrlResponse);
// 获取信息
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &response);
// 执行请求
curl_easy_perform(handle);
// 释放
curl_easy_cleanup(handle);
return response;
}
int main() {
// 使用前初始化libcurl, 只需初始化一次
curl_global_init(CURL_GLOBAL_DEFAULT);
// 执行请求
#ifndef API_TEST
cout << endl << "返回:" << get("https://image.adobe.io/pie/psdService/photoshopActions") << endl;;
#else
cout << get("https://image.adobe.io/pie/psdService/hello");
#endif
// 释放libcurl相关资源
curl_global_cleanup();
getchar();
return 0;
}
三、返回结果
{"error_code":"401013","message":"Oauth token is not valid"}