Coder Social home page Coder Social logo

Comments (3)

javidigani avatar javidigani commented on May 25, 2024

Hi @DigitalDuquette ! Have you seen the examples in our documentation? Javascript example and Python example.

These should work in either Azure Functions, AWS Lambda, or Google Cloud Functions – Fivetran essentially kicks off the function and awaits a valid response from the function. Hope this helps!

from functions.

DigitalDuquette avatar DigitalDuquette commented on May 25, 2024

Yeah, I see and have been working with the boiler plate example you have there. I'm just running into a few roadblocks and was hoping to see an example using a simple API. I know I'm doing something stupid (lots of things), but I dunno what. Usually, I can find an example of an easy to use API like https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY and I can reverse my way into something that works.

Basically, with javascript, I'm dealing with an async issue. Just looking for an example that works so I can laugh at myself. Here's an example of what I'm talking about:

var https = require('https');

var options = {
    "method": "GET",
    "hostname": "website.com",
    "path": "/thing",
    "headers": {
        "Authorization": "Bearer shhhhhhhhhhhhhhh"
    }
};

async function apiResponse(){
    
    https.get(options, (resp) => {
        let data = '';

        // A chunk of data has been received.
        resp.on('data', (chunk) => {
            data += chunk;
        });

        // The whole response has been received. Print out the result.
        resp.on('end', () => {
            
            let insertTransactions = [];

            insertTransactions.push({
                campaign_id: JSON.parse(data).campaign_id,
                name: JSON.parse(data).name,
                last_phish_prone_percentage: JSON.parse(data).last_phish_prone_percentage,
                last_run: JSON.parse(data).last_run,
                status: JSON.parse(data).status,
                hidden: JSON.parse(data).hidden,
                send_duration: JSON.parse(data).send_duration,
                track_duration: JSON.parse(data).track_duration,
                frequency: JSON.parse(data).frequency,
                create_date: JSON.parse(data).create_date,
                psts_count: JSON.parse(data).psts_count
            });
            console.log(data);
            console.log(insertTransactions);
            console.log('inside apiResponse');
            
            return insertTransactions;
        });

 

        }).on("error", (err) => {
                console.log("Error: " + err.message);
            }
        )
}; /*end function apiResponse() */


module.exports = function(context, req) {

    // if (req.body.secrets === undefined) {
    //     context.res = {
    //         status: 400,
    //         body: "No secret is defined!"
    //     };
    // }

    context.res = {
        status: 200,
        body: update()
    };
    context.done();
};



function update() {
    try {
        console.log('starting update() function...');
        let insertTransactions = apiResponse().then();
        console.log(insertTransactions);
        console.log('if you see data above, things are working kinda');

        // Populate records and 
        return ({
            insert: {
                transactions: insertTransactions
            },
            schema: {
                transactions: {
                    primary_key: ['date']
                }
            },
            hasMore: false
        });
    }
    catch(e) {
        console.log('error: ', e);
    }
 
};

I'm prepared to be flamed.

from functions.

DigitalDuquette avatar DigitalDuquette commented on May 25, 2024

Here's an update, we can close this. I figured it out, async is hard. I'm also not a software dev. In case some future poor soul comes across this on a google search, here's a summary:

var https = require('https');
let campaigns = []; 
let campaignGroupsBridge = [];
var options = {
    "method": "GET",
    "hostname": "us.api.knowbe4.com",
    "path": "/v1/phishing/campaigns/",
    "headers": {
        "Authorization": "Bearer shhhhhhhh",
        "Accept": "application/json",
        "Cache-Control": "no-cache",
        "Host": "us.api.knowbe4.com",
        "Accept-Encoding": "gzip, deflate",
        "Connection": "keep-alive",
        "cache-control": "no-cache"
    }
};

module.exports = async function(context, req) {

    // if (req.body.secrets === undefined) {
    //     context.res = {
    //         status: 400,
    //         body: "No secret is defined!"
    //     };
    // }

    context.res = { /*returned function can't be async*/
        status: 200,
        body: await apiResponse2() 
            .then(results => update()), 
        headers: {
            'Content-Type': 'application/json'
        }
    };
    // context.done(); /*with module.exports as async, context.done is implicit */  
};

async function apiResponse2(){
   console.log('starting apiResponse2');

   return new Promise(resolve => {
        https.get(options, (resp) => {
            let data = '';

            // A chunk of data has been received.
            resp.on('data', (chunk) => {
                data += chunk;
            });

            // The whole response has been received. Print out the result.
            resp.on('end', () => {
                let incomingCampaigns = JSON.parse(data);
                for (var i = 0; i < incomingCampaigns.length; i++){
                    /* groups */
                    let currentCampaign = JSON.parse(data)[i]; 
                    for (var j = 0; j < currentCampaign.groups.length; j++){
                        campaignGroupsBridge.push({
                            campaign_id: currentCampaign.campaign_id, 
                            group_id: currentCampaign.groups[j].group_id, 
                            name: currentCampaign.groups[j].name 
                        })
                    };
                };
                for (var i = 0; i < incomingCampaigns.length; i++){
                    let currentCampaign = JSON.parse(data)[i];    
                    campaigns.push({ 
                        campaign_id: currentCampaign.campaign_id,
                        name: currentCampaign.name,
                        last_phish_prone_percentage: currentCampaign.last_phish_prone_percentage,
                        last_run: currentCampaign.last_run,
                        status: currentCampaign.status,
                        hidden: currentCampaign.hidden,
                        send_duration: currentCampaign.send_duration,
                        track_duration: currentCampaign.track_duration,
                        frequency: currentCampaign.frequency,
                        create_date: currentCampaign.create_date,
                        psts_count: currentCampaign.psts_count
                    })
                }; /*end for loop */
                resolve(campaigns);
            });
            }).on("error", (err) => {
                    console.log("Error: " + err.message);
                }
        );
        
   })
    
}; /*end function apiResponse() */

function update() {
    try {
        // console.log('starting update() function...');
        // console.log(campaigns);
        // console.log('if you see data above, things are working');

        // Populate records and 
        return ({
            state: {
                campaignCursor: 'n/a'
            },
            insert: {
                campaigns: campaigns, 
                campaign_groups_bridge: campaignGroupsBridge
            },
            schema: {
                campaigns: {
                    primary_key: ['campaign_id']
                }
                , 
                campaign_groups_bridge: {
                    primary_key: ['campaign_id', 'group_id']
                }
            },
            hasMore: false
        });
    }
    catch(e) {
        console.log('error: ', e);
    }
};

So you could do this, but with like good practice and standards. YMMV.

from functions.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.