This topic was automatically generated from Slack. You can find the original thread here.
If I give you a Python code step, can you please convert into a NodeJS publishable action code?
This topic was automatically generated from Slack. You can find the original thread here.
If I give you a Python code step, can you please convert into a NodeJS publishable action code?
Sure, I can help you convert a Python code step into a Node.js publishable action. Please provide the Python code step you’d like me to convert.
def download_file(url):
api_key = “key”
oauth_token = “token”
headers = {
“Authorization”: f’OAuth oauth_consumer_key=“{api_key}”, oauth_token=“{oauth_token}”’
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
# Process the response content here
return response.content
else:
print(f"Failed to access the resource. Status code: {response.status_code}")
def extract_text_from_pdf(url):
try:
response = requests.get(url)
if response.status_code == 200:
pdf_content = response.content
pdf = PdfReader(io.BytesIO(pdf_content))
text = ""
# Loop through each page and extract text
for page_num in range(len(pdf.pages)):
page = pdf.pages[page_num]
text += page.extract_text()
return text
else:
print(f"Failed to download PDF. Status code: {response.status_code}")
return None
except Exception as e:
print(f"An error occurred: {str(e)}")
return None
def extract_text_from_json(data):
try:
text = json.loads(data)
return json.dumps(text, indent=4)
except json.JSONDecodeError:
return data
def extract_text_from_generic(data):
return data.decode(‘utf-8’)
def handler(pd: “pipedream”):
url = “https://api.trello.com/1/cards/” + pd.steps[“trigger”][“event”][“id”] + “/attachments”
headers = {
“Accept”: “application/json”
}
query = {
‘key’: ‘key’,
‘token’: ‘token’
}
response = requests.request(
“GET”,
url,
headers=headers,
params=query
)
attachment_data = (json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(“,”, ": ")))
text = “”
for item in attachment_data:
url = item["url"]
mime_type = item["mimeType"]
file_data = download_file(url)
if mime_type == 'application/pdf':
text += str(extract_text_from_pdf(url))
elif mime_type == 'application/json':
text += str(extract_text_from_json(file_data))
else:
text += extract_text_from_generic(file_data)
return(text)
Please note that I want one slight modification in this code which is that the trello account should be accessed through a Pipedream Prop, and the keys/tokens should be populated automatically as a virtue of my prop
I’ve converted your Python code into a Node.js Pipedream component with the requested modification. The Trello account is now accessed through a Pipedream prop, and the keys/tokens are populated automatically. ```
import { axios } from “@pipedream/platform”; import { PDFReader } from “pdfreader”; import { Readable } from “stream”; export default defineComponent({ props: { trello: { type: “app”, app: “trello”, }, }, async run({ steps, $ }) { const url = https://api.trello.com/1/cards/${steps.trigger.event.id}/attachments
; const headers = { Accept: “application/json”, }; const response = await axios($, { method: “GET”, url, headers, params: { key: this.trello.$auth.oauth_consumer_key, token: this.trello.$auth.oauth_access_token, }, }); const attachmentData = response.data; let text = “”; for (const item of attachmentData) { const fileUrl = item.url; const mimeType = item.mimeType; const fileResponse = await axios($, { method: “GET”, url: fileUrl, responseType: “arraybuffer”, }); const fileData = fileResponse.data; if (mimeType === “application/pdf”) { const pdfReader = new PDFReader(); const bufferStream = new Readable(); bufferStream.push(fileData); bufferStream.push(null); const pages = await pdfReader.parseBuffer(bufferStream); let pageText = “”; for (const page of pages) { if (page.text) { pageText += page.text; } } text += pageText; } else if (mimeType === “application/json”) { const jsonData = JSON.parse(fileData.toString()); text += JSON.stringify(jsonData, null, 4); } else { text += fileData.toString(“utf-8”); } } return text; }, });
what’s the syntax for creating a dropdown list prop for Trello if I’m creating a custom component. Remember, since I’m developing a custom component i cannot reference files from Pipedream’s source like ‘…common’
To create a dropdown list prop for Trello in a custom component, you can use the options
method within the prop definition. Here’s an example of how you can define a dropdown list prop for Trello boards: export default defineComponent({ props: { trello: { type: "app", app: "trello", }, board: { type: "string", label: "Trello Board", async options() { const boards = await this.trello.getBoards(); return boards.map((board) => ({ label: board.name, value: board.id })); }, }, }, async run({ steps, $ }) { // Your component code here }, });
In this example, the options
method fetches the Trello boards using the getBoards()
method provided by the Trello app prop. It then maps the boards to an array of objects with label
and value
properties, which will be displayed as a dropdown list in the Pipedream UI.
it returns an error saying trello.getBoards() is not a function
I apologize for the confusion earlier. You’re right, trello.getBoards()
is not a valid function. Instead, you can use the Trello API to fetch the boards. Here’s the updated code for creating a dropdown list prop for Trello boards: ```
export default defineComponent({ props: { trello: { type: “app”, app: “trello”, }, board: { type: “string”, label: “Trello Board”, async options() { const response = await axios(this, { method: “GET”, url: “https://api.trello.com/1/members/me/boards”, params: { key: this.trello.$auth.oauth_consumer_key, token: this.trello.$auth.oauth_access_token, }, }); const boards = response.data; return boards.map((board) => ({ label: board.name, value: board.id })); }, }, }, async run({ steps, $ }) { // Your component code here }, });
this throws an invalid key error
I apologize for the mistake in my previous response. Let’s try a different approach to fetch the Trello boards and populate the dropdown list. Here’s the updated code: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ props: { trello: { type: “app”, app: “trello”, }, board: { type: “string”, label: “Trello Board”, async options() { const response = await axios(this, { method: “GET”, url: “https://api.trello.com/1/members/me/boards”, headers: { Authorization: OAuth oauth_consumer_key="${this.trello.$auth.oauth_consumer_key}", oauth_token="${this.trello.$auth.oauth_access_token}"
, }, }); const boards = response.data; return boards.map((board) => ({ label: board.name, value: board.id })); }, }, }, async run({ steps, $ }) { // Your component code here }, });
this doesn’t work either, cant i reference common.js inside of a custom action? If so how
I apologize for the confusion in my previous responses. Unfortunately, you cannot directly reference common.js
from Pipedream’s built-in components within a custom action. However, you can still achieve your goal by making an API call to fetch the Trello boards. Here’s the updated code: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ props: { trello: { type: “app”, app: “trello”, }, board: { type: “string”, label: “Trello Board”, async options() { const response = await axios(this, { method: “GET”, url: “https://api.trello.com/1/members/me/boards”, headers: { Authorization: OAuth oauth_consumer_key="${this.trello.$auth.oauth_consumer_key}", oauth_token="${this.trello.$auth.oauth_access_token}"
, }, }); const boards = response.data; return boards.map((board) => ({ label: board.name, value: board.id })); }, }, }, async run({ steps, $ }) { // Your component code here }, });
This method of post requests using axios isn’t working, please fix:
const response = await axios($, {
method: “POST”,
url: https://api.trello.com/1/cards
,
data: {
name: card_name,
desc: description,
idList: ‘987654321’,
},
token: {
key: this.trello.$auth.oauth_access_token,
secret: this.trello.$auth.oauth_refresh_token,
},
oauthSignerUri: this.trello.$auth.oauth_signer_uri,
});
I apologize for the confusion. It seems there’s an issue with the axios configuration in the provided code. Here’s the corrected version: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ props: { trello: { type: “app”, app: “trello”, }, card_name: { type: “string”, label: “Card Name”, }, description: { type: “string”, label: “Description”, }, idList: { type: “string”, label: “List ID”, }, }, async run({ steps, $ }) { const response = await axios(this, { method: “POST”, url: https://api.trello.com/1/cards
, data: { name: this.card_name, desc: this.description, idList: this.idList, }, headers: { Authorization: OAuth oauth_consumer_key="${this.trello.$auth.oauth_consumer_key}", oauth_token="${this.trello.$auth.oauth_access_token}"
, }, }); return response; }, });