-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-request.js
More file actions
91 lines (73 loc) · 1.93 KB
/
api-request.js
File metadata and controls
91 lines (73 loc) · 1.93 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const requestId = args[0];
const location = args[1];
const keyword = args[2];
const prompt = `Please create an image of a ticket for the event "${keyword}" at "${location}`;
const openaiApiKey = secrets.openaiApiKey || '';
if (!openaiApiKey) {
throw Error('openaiApiKey is not set');
}
const pinataJwt = secrets.pinataJwt || '';
if (!pinataJwt) {
throw Error('pinataJwt is not set');
}
const openAIRequest = Functions.makeHttpRequest({
url: 'https://api.openai.com/v1/images/generations',
method: 'POST',
headers: {
Authorization: `Bearer ${openaiApiKey}`,
'Content-Type': 'application/json',
},
data: { model: 'dall-e-2', prompt: prompt, size: '256x256' },
timeout: 9000,
});
const openAiResponse = await openAIRequest;
if (openAiResponse.error) {
throw Error(
openAiResponse.error.message
? openAiResponse.error.message
: 'openai response error'
);
}
const data = openAiResponse.data;
if (!data || !data.data) {
throw Error('No data in response');
}
const imageUrl = data.data[0].url;
console.log('image url:', imageUrl);
// ipfs에 업로드
const ipfsPinRequest = Functions.makeHttpRequest({
url: 'https://api.pinata.cloud/pinning/pinJSONToIPFS',
method: 'POST',
headers: {
Authorization: `Bearer ${pinataJwt}`,
'Content-Type': 'application/json',
},
data: {
pinataContent: {
url: imageUrl,
},
pinataMetadata: {
name: `${requestId}.json`,
keyvalues: {
keyword: keyword,
location: location,
},
},
},
timeout: 9000,
});
const ipfsPinResponse = await ipfsPinRequest;
if (ipfsPinResponse.error) {
throw Error(
ipfsPinResponse.error.details
? ipfsPinResponse.error.details
: 'ipfs pin response error'
);
}
const ipfsData = ipfsPinResponse.data;
if (!ipfsData) {
throw Error('No data in response');
}
const ipfsHash = ipfsData.IpfsHash;
console.log('IpfsHash:', ipfsHash);
return Functions.encodeString(ipfsHash);