Skip to content

Instantly share code, notes, and snippets.

View AntonNguyen's full-sized avatar

Anton Nguyen AntonNguyen

View GitHub Profile
@AntonNguyen
AntonNguyen / gist:5349783
Created April 9, 2013 21:56
Writing Clean Code With Nested Promises

I'm writing an app that talks to Apple to verifyReceipts. They have both a sandbox and production url that you can post to.

When communicating with Apple, if you receive a 21007 status, it means you were posting to the production url, when you should be posting to the sandbox one.

So I wrote some code to facilitate the retry logic. Here's a simplified version of my code:

var request = require('request')
  , Q = require('q')
  ;
exports.create = function(req, res) {
Subscriptions
.saveToDatabase(req.body)
.then(function(subscription) {
return subscription.verify();
}).then(function(body) {
returnVerifyResponse(body, res);
}).fail(function(body) {

The OAuth Dance with FreshBooks

Terminology

  • User - The FreshBooks account, whose information we are trying to access via OAuth
  • Consumer - The FreshBooks account, who we authorize to access the user's information
  • Consumer key - The consumer's subdomain, consumer_key.freshbooks.com
  • Consumer Secret - The consumer's secret, that we will use to make OAuth requests.
  • Request Token - A value used by the consumer to obtain authorization and an access token from the user.
  • Access Token - A value used by the consumer to access the user's information.
  • Access Token Secret - A secret used by the consumer to establish ownership of a given Token.
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.*;
import org.apache.http.entity.mime.content.*;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.StringEntity;
import javax.net.ssl.*;
<request method="invoice.create">
<invoice>
<client_id>2</client_id>
<lines>
<line>
<name>Time Entry</name>
<time_entries>
<time_entry>
<time_entry_id>14</time_entry_id>
</time_entry>
<!---- Adding Paypal ----->
<request method="invoice.create">
<invoice>
<client_id>1</client_id>
<gateways>
<gateway>
<name>Paypal</name>
</gateway>
</gateways>
</invoice>

Suppose we have the following contacts with the following ids: 1, 2, 3

This call would delete contacts 2 and 3

<request method="client.update">
	<client>
		<client_id>1</client_id>
		<contacts>
 
# Description:
# Manipulate the queue for releasing
#
# Commands:
# singles? - Start looking for a singles partner
# doubles? - Start looking for doubles partners
# hyper <singles/doubles> - Start looking for hyper pong partners
# stop - Stop searching for partners
# o/ or \o or \o/ - Join a game
# add <username> - Manually add a player
#!/usr/bin/python
import sys
import os
import base64
import requests
def generateMultipartBody(xmlContent, imageFile, boundary, cidPrefix):
CRLF = '\r\n'
newLine = ''
<?xml version="1.0" encoding="utf-8"?>
<request method="receipt.create">
<receipt>
<expense_id>1</expense_id>
<image>cid:imagefile</image>
</receipt>
</request>