Enterprise Framework

Software Solutions in the Enterprise

Node.js - HTTP with Error, Done, Data, End, Timeout example


var https = require('https');
var querystring = require('querystring');
var postData = { data : "something" };
var options = {
hostname: 'www.yourdomain.com',
path: '/api/book',
method: 'POST',
headers: {
"Content-Type": "application/json",
"Content-Length": postData.length
}
};

var req = https.request(options, function (res) {
res.setEncoding('utf8');

res.on('data', function (chunk) {
console.log("[RESPONSE DATA]", chunk);
});

res.on('end', function (data) {

if (res.statusCode >= 200 && res.statusCode < 300) {
console.log("[SUCCESS]");
} else {
console.log("[HTTP RESPONSE STATUS CODE ERROR]", res.statusCode);
console.log("[HTTP RESPONSE STATUS MESSAGE ERROR]", res.statusMessage);
}
});
});
req.setNoDelay(true);
req.setTimeout(28000, function(e) {
console.log("Timeout Fired at 29000 ms");
});

req.on('error', function (e) {
console.log("[FORWARD ERROR]", e);

if (e.statusCode === "ECONNRESET") {
console.log("Timeout received");
}
});

req.write(postData);
req.end();

Lambda Node.js Callback

http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html


Using the Callback Parameter

The Node.js runtimes v4.3 and v6.10 support the optional callback parameter. You can use it to explicitly return information back to the caller. The general syntax is:

callback(Error error, Object result);



Where:

  • error – is an optional parameter that you can use to provide results of the failed Lambda function execution. When a Lambda function succeeds, you can pass null as the first parameter.

  • result – is an optional parameter that you can use to provide the result of a successful function execution. The result provided must be JSON.stringify compatible. If an error is provided, this parameter is ignored.

Note

Using the callback parameter is optional. If you don't use the optional callback parameter, the behavior is same as if you called the callback() without any parameters. You can specify the callback in your code to return information to the caller.

AWS Gateway Resource Expression Format

http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html

The API-executing Resource expression has the following general format:

where:

  • region is the AWS region (such as us-east-1 or * for all AWS regions) that corresponds to the deployed API for the method.

  • account-id is the 12-digit AWS account Id of the REST API owner.

  • api-id is the identifier API Gateway has assigned to the API for the method. (* can be used for all APIs, regardless of the API's identifier.)

  • stage-name is the name of the stage associated with the method (* can be used for all stages, regardless of the stage's name.)

  • HTTP-VERB is the HTTP verb for the method. It can be one of the following: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS.

  • resource-path-specifier is the path to the desired method. (* can be used for all paths).

Some example resource expressions include:

  • arn:aws:execute-api:*:*:* for any resource path in any stage, for any API in any AWS region. (This is equivalent to *).

  • arn:aws:execute-api:us-east-1:*:* for any resource path in any stage, for any API in the AWS region of us-east-1.

  • arn:aws:execute-api:us-east-1:*:api-id/* for any resource path in any stage, for the API with the identifier of api-id in the AWS region of us-east-1.

  • arn:aws:execute-api:us-east-1:*:api-id/test/* for resource path in the stage of test, for the API with the identifier of api-id in the AWS region of us-east-1.

  • arn:aws:execute-api:us-east-1:*:api-id/test/*/mydemoresource/* for any resource path along the path of mydemoresource, for any HTTP method in the stage of test, for the API with the identifier of api-id in the AWS region of us-east-1.

  • arn:aws:execute-api:us-east-1:*:api-id/test/GET/mydemoresource/* for GET methods under any resource path along the path of mydemoresource, in the stage of test, for the API with the identifier of api-id in the AWS region of us-east-1.


Virtual Box - Resize VMDK by Changing Type To VDI and Converting Back to VMDK

 Found a great article on stack overflow for increasing Virtual Box VMDK Storage Size.  It's by converting to another type VDI  and then converting back to VMDK


VBoxManage clonehd "source.vmdk" "cloned.vdi" --format vdi
VBoxManage modifyhd "cloned.vdi" --resize 51200
VBoxManage clonehd "cloned.vdi" "resized.vmdk" --format vmdk


Stripe Events Switch Statement


switch ($event)

{

    case "account.updated": //describes an account, Occurs whenever an account status or property has changed.

    case "account.application.deauthorized": //describes an application, Occurs whenever a user deauthorizes an application. Sent to the related application only., child parameters

    case "account.external_account.created": //describes an external account (e.g., card or bank account), Occurs whenever an external account is created.

    case "account.external_account.deleted": //describes an external account (e.g., card or bank account), Occurs whenever an external account is deleted.

    case "account.external_account.updated": //describes an external account (e.g., card or bank account), Occurs whenever an external account is updated.

    case "application_fee.created": //describes an application fee, Occurs whenever an application fee is created on a charge.

    case "application_fee.refunded": //describes an application fee, Occurs whenever an application fee is refunded, whether from refunding a charge or from refunding the application fee directly, including partial refunds.

    case "application_fee.refund.updated": //describes a fee refund, Occurs whenever an application fee refund is updated.

    case "balance.available": //describes a balance, Occurs whenever your Stripe balance has been updated (e.g., when a charge is available to be paid out). By default, Stripe automatically transfers funds in your balance to your bank account on a daily basis.

    case "bitcoin.receiver.created": //describes a bitcoin receiver, Occurs whenever a receiver has been created.

    case "bitcoin.receiver.filled": //describes a bitcoin receiver, Occurs whenever a receiver is filled (i.e., when it has received enough bitcoin to process a payment of the same amount).

    case "bitcoin.receiver.updated": //describes a bitcoin receiver, Occurs whenever a receiver is updated.

    case "bitcoin.receiver.transaction.created": //describes a bitcoin receiver, Occurs whenever bitcoin is pushed to a receiver., Occurs whenever an account capability is updated.

    case "charge.captured": //describes a charge, Occurs whenever a previously uncaptured charge is captured.

    case "charge.failed": //describes a charge, Occurs whenever a failed charge attempt occurs.

    case "charge.pending": //describes a charge, Occurs whenever a pending charge is created.

    case "charge.refunded": //describes a charge, Occurs whenever a charge is refunded, including partial refunds.

    case "charge.succeeded": //describes a charge, Occurs whenever a new charge is created and is successful.

    case "charge.updated": //describes a charge, Occurs whenever a charge description or metadata is updated.

    case "charge.dispute.closed": //describes a dispute, Occurs when a dispute is closed and the dispute status changes to charge_refunded, lost, warning_closed, or won.

    case "charge.dispute.created": //describes a dispute, Occurs whenever a customer disputes a charge with their bank.

    case "charge.dispute.funds_reinstated": //describes a dispute, Occurs when funds are reinstated to your account after a dispute is won.

    case "charge.dispute.funds_withdrawn": //describes a dispute, Occurs when funds are removed from your account due to a dispute.

    case "charge.dispute.updated": //describes a dispute, Occurs when the dispute is updated (usually with evidence).

    case "charge.refund.updated": //describes a refund, Occurs whenever a refund is updated on selected payment methods.

    case "coupon.created": //describes a coupon, Occurs whenever a coupon is created.

    case "coupon.deleted": //describes a coupon, Occurs whenever a coupon is deleted.

    case "coupon.updated": //describes a coupon, Occurs whenever a coupon is updated.

    case "customer.created": //describes a customer, Occurs whenever a new customer is created.

    case "customer.deleted": //describes a customer, Occurs whenever a customer is deleted.

    case "customer.updated": //describes a customer, Occurs whenever any property of a customer changes.

    case "customer.discount.created": //describes a discount, Occurs whenever a coupon is attached to a customer.

    case "customer.discount.deleted": //describes a discount, Occurs whenever a coupon is removed from a customer.

    case "customer.discount.updated": //describes a discount, Occurs whenever a customer is switched from one coupon to another.

    case "customer.source.created": //describes a source (e.g., card), Occurs whenever a new source is created for a customer.

    case "customer.source.deleted": //describes a source (e.g., card), Occurs whenever a source is removed from a customer.

    case "customer.source.updated": //describes a source (e.g., card), Occurs whenever a source's details are changed.

    case "customer.subscription.created": //describes a subscription, Occurs whenever a customer is signed up for a new plan.

    case "customer.subscription.deleted": //describes a subscription, Occurs whenever a customer's subscription ends.

    case "customer.subscription.trial_will_end": //describes a subscription, Occurs three days before the trial period of a subscription is scheduled to end.

    case "customer.subscription.updated": //describes a subscription, Occurs whenever a subscription changes (e.g., switching from one plan to another or changing the status from trial to active).

    case "invoice.created": //describes an invoice, Occurs whenever a new invoice is created. See Using Webhooks with Subscriptions to learn how webhooks can be used with, and affect, this event.

    case "invoice.payment_failed": //describes an invoice, Occurs whenever an invoice payment attempt fails, either due to a declined payment or the lack of a stored payment method.

    case "invoice.payment_succeeded": //describes an invoice, Occurs whenever an invoice payment attempt succeeds.

    case "invoice.sent": //describes an invoice, Occurs whenever an invoice email is sent out.

    case "invoice.upcoming": //describes an invoice, Occurs X number of days before a subscription is scheduled to create an invoice that is charged automatically, where X is determined by your subscriptions settings.

    case "invoice.updated": //describes an invoice, Occurs whenever an invoice changes (e.g., the invoice amount).

    case "invoiceitem.created": //describes an invoiceitem, Occurs whenever an invoice item is created.

    case "invoiceitem.deleted": //describes an invoiceitem, Occurs whenever an invoice item is deleted.

    case "invoiceitem.updated": //describes an invoiceitem, Occurs whenever an invoice item is updated.

    case "order.created": //describes an order, Occurs whenever an order is created.

    case "order.payment_failed": //describes an order, Occurs whenever an order payment attempt fails.

    case "order.payment_succeeded": //describes an order, Occurs whenever an order payment attempt succeeds.

    case "order.updated": //describes an order, Occurs whenever an order is updated.

    case "order_return.created": //describes an order return, Occurs whenever an order return is created.

    case "payout.canceled": //describes a payout, Occurs whenever a payout is canceled.

    case "payout.created": //describes a payout, Occurs whenever a payout is created.

    case "payout.failed": //describes a payout, Occurs whenever a payout attempt fails.

    case "payout.paid": //describes a payout, Occurs whenever a payout is expected to be available in the destination account. If the payout fails, a payout.failed notification is additionally sent at a later time.

    case "payout.updated": //describes a payout, Occurs whenever the metadata of a payout is updated.

    case "plan.created": //describes a plan, Occurs whenever a plan is created.

    case "plan.deleted": //describes a plan, Occurs whenever a plan is deleted.

    case "plan.updated": //describes a plan, Occurs whenever a plan is updated.

    case "product.created": //describes a product, Occurs whenever a product is created.

    case "product.deleted": //describes a product, Occurs whenever a product is deleted.

    case "product.updated": //describes a product, Occurs whenever a product is updated.

    case "recipient.created": //describes a recipient, Occurs whenever a recipient is created.

    case "recipient.deleted": //describes a recipient, Occurs whenever a recipient is deleted.

    case "recipient.updated": //describes a recipient, Occurs whenever a recipient is updated.

    case "review.closed": //describes a review, Occurs whenever a review is closed. The review's reason field indicates why (e.g., approved, refunded, refunded_as_fraud, disputed.

    case "review.opened": //describes a review, Occurs whenever a review is opened.

    case "scheduled_query_run.created": //describes a scheduled query run, Occurs whenever a sigma scheduled query run completes.

    case "sku.created": //describes a sku, Occurs whenever a SKU is created.

    case "sku.deleted": //describes a sku, Occurs whenever a SKU is deleted.

    case "sku.updated": //describes a sku, Occurs whenever a SKU is updated.

    case "source.canceled": //describes a source (e.g., card), Occurs whenever a source is canceled.

    case "source.chargeable": //describes a source (e.g., card)    case "capability.updated": //describes a capability, Occurs whenever a source transitions to chargeable.

    case "source.failed": //describes a source (e.g., card)" Occurs whenever a source fails.

    case "source.transaction.created": //describes a transaction    

    case "capability.updated": //describes a capability    

    case "source.failed": //describes a source (e.g., card)" Occurs whenever a source fails., Occurs whenever a source transaction is created.

    case "transfer.created": //describes a transfer": //, Occurs whenever a transfer is created.

    case "transfer.reversed": //describes a transfer": //    

    case "capability.updated": //describes a capability, Occurs whenever a transfer is reversed, including partial reversals.

    case "transfer.updated": // describes a transfer, Occurs whenever the description or metadata of a transfer is updated.

    case "ping": //has no descriptionm May be sent by Stripe at any time to see if a provided webhook URL is working.

      break;

  }


Azure Application Gateway with SSL Offloading - IIS Url Rewrite from HTTP to HTTPS Redirect

<rewrite> 

<rules> 

<rule name="HTTP To HTTPS Redirect Behind App Gtwy" stopProcessing="true"> 

<match url="^(.*)$" ignoreCase="false" /> 

<conditions logicalGrouping="MatchAny"> 

<add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" ignoreCase="false" /> 

</conditions> 

<action type="Redirect" url="https://{SERVER_NAME}{URL}" redirectType="Found" /> 

</rule> 

</rules> 

</rewrite>


You can also do this HTTP to HTTPS redirect through the Application Gateway using PowerShell.

https://docs.microsoft.com/en-us/azure/application-gateway/application-gateway-configure-redirect-powershell


Create PFX from Azure App Service Certificate Service

I recently created a Azure App Service Certificate that I wanted to use with Azure Application Gateway.  However, this requires you to upload an PFX file and there isn't an option to generate one from Azure App Service Certificate.  However, I found an article that can generate the PFX for you from the App Service Certificate;

https://blogs.msdn.microsoft.com/appserviceteam/2017/02/24/creating-a-local-pfx-copy-of-app-service-certificate/



Mac Sierra : Git Completion and Bash Prompt With Branch Update

The following will help get GIT completion setup locally on Mac Sierra.  Not correctly loading the git-completion.bash or git-prompt.sh can result in getting the following error: 


ERROR:   __git_ps1: command not found


Check that you did not mistype the file name as that could result in the script not being found and ran.


STEPS:

$ cd ~/Downloads       

$ curl -OL https://github.com/git/git/raw/master/contrib/completion/git-completion.bash

$ mv git-completion.bash ~/.git-completion.bash

$ sudo nano ~/.bashrc

ADD .bashrc CONTENTS
===============================
 
if [ -f ~/.git-completion.bash ]; then
    source ~/.git-completion.bash
fi
 


$ cd ~/Downloads/

$ curl -OL https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
 
$ mv ~/Downloads/git-prompt.sh ~/.git-prompt.sh

 
UPDATE .bash_profile CONTENTS
===============================
 
if [ -f ~/.git-prompt.sh ]; then
    source ~/.git-prompt.sh
fi
 
export PS1='\h:\u \W$(__git_ps1 "(%s)") $ '

Error: Microsoft Graph Development connecting to Outlook API returns Sign In Trouble Bad Request

After registering your app in https://apps.dev.microsoft.com/ and trying to connect to the Microsoft Outlook API using Microsoft Graph, you get the following Error:

Sign In
Sorry, but we’re having trouble signing you in.

We received a bad request.


CODE : THINGS TO CHECK

Make sure your API key and Password are correct in your app code.

API : THINGS TO CHECK

  1. Go to the Application Registration Portal at https://apps.dev.microsoft.com/
  2. Select your application
  3. Add your local development urls, 
    1. http://localhost
    2. http://localhost:8080
    3. https://localhost:44300
Make sure the redirect being passed from the code to Graph is passing a Url registered in the Application Registration Portal.  If successful, you should see:

App publisher website: localhost