Monday, June 22, 2015

Webhooks and callbacks
REST based APIs can implement a publisher subscriber notification mechanism with web hooks. This can be implemented with the following:
* Storing subscriptions in a database with subscription details such as

  • event names, 
  • user relationship, 
  • target URL to send payloads, and 
  • active versus inactive state for the subscription 

* API to modify subscriptions that includes:

  • GET /api/v1/subscription/ to list 
  • POST /api/v1/subscription/ to create 
  • GET /api/v1/subscription/:id/ to lookup 
  • PUT /api/v1/subscription/:id/ to edit 
  • DELETE /api/v1/subscription/:id to delete a subscription 

* List and implement event types

  • a name in the name.verb syntax. 
  • a payload to simply mirror the representation from the standard API. 

* send hooks with POST to each of the target URLs for each matching subscription

  • compiling and POSTing the combined payload for the triggering resource and hook resource 
  • sending to known subscribers where X-Hook-Secret header has a unique string and one that matches what was issued. 
  • confirming the hook legitimacy with a X-Hook-Signature header 
  • Handling responses like the 410 Gone and optionally retrying connection or other 4xx/5xx errors.
#coding exercise
Building bridges dp problem
Consider a 2D map with a river flowing from left to right through the center. There are n cities to the south of the river and same number on the northern bank. We have to bridge city i on the northern bank to city i on the southern bank however the order of cities can be random on both the northern and southern bank. We have to draw as many bridges without having them cross each other.
Solution:
When we put down the co-ordinates, we see that the bridges won't cross only if we pick increasing numbers on both and north and south banks. This means we are trying to find the longest common subsequence.
The length of the longest common subsequence c[i,j] is :
1. 0 if i = 0 or j = 0
2. c[i-1, j-1] + 1if i,j > 0 and xi = yj
3. max(c[i,j-1], c[i-1, j]) if i, j  > 0 and xi != yj

for i = 1 to n
     c[i, 0] = 0
for j = 0 to n
     c[0,j] = 0
for i = 1 to n
      for j = 1 to n
            if xi == yi
               c[i,j] = c[i -1, j-1] + 1
               b[i,j] = up-left
            elseif c[i-1,j]  >= c[i, j-1]
               c[i,j] = c[i-1, j]
               b[i,j] = up
            else c[i,j] = c[i,j-1]
               b[i,j] = left
return c and b

The problem above assumes the bottom row of the cities is sorted.
Otherwise we could use the longest increasing subsequence.

#codingexercise
Double  GetNthRootProductAlternateTermRaisedPDividedQoverPTimesQ (Double [] A,Double  p, Double q)
{
If ( A== null) return 0;
Return A.NthRootProductAlternateTermRaisedPDividedQoverPTimesQ(p, q);
}


No comments:

Post a Comment