blog.pleb.in

Life of Apps

Showing posts with label API. Show all posts
Showing posts with label API. Show all posts

A Cursory Look at Auth0

Auth0 provides an identity infrastructure for applications helping them authenticate users. Auth0 supports authentication using OAuth using social media providers, username/password and other means. Auth0 offers a free trial and supports signup using social media. I signed up using my github account. It offers good tutorials to setup the central dashboard that has a host of features, I only tried two basic features: setting up clients and APIs.

Setting up clients that would use Auth0 for authentication: I used single page webapps as they pointed to a node implementation. Native iOS apps and non-interactive apps are supported.

APIs can be protected using JWT and other means. These can be defined in the dashboard.

Users that want to use your app can signup using the Auth0 endpoint shared. The signing up as mentioned earlier would be primarily through social media accounts and username/password. The users signed up are displayed in the dashboard.

Auth0 supports both SAML and OAuth. It also supports OAuth using JWT. JWT or JSON Web Tokens offer a compact way to authenticate users and the flow is as follows:

  • User authenticates using username/password
  • Receives a JWT token 
  • Uses the JWT token until it expires for further server interaction by passing it in the header


JWT is more compact than SAML as SAML is more lengthy due it being XML based. I had initially thought of writing a post on JWT authentication but found that this post by Jonathan  explains it well.


Node.js Code Organization

As mentioned in my previous post, I learnt Node.js and Restify through the Udemy course titled "A Simple Node.js/Mongo/Restify API in Less Than 3 Hours" by Jim Hlad. I made a copy of the Node.js API sample app that was taught in the course and called it Cwitiq. This app would manage reviews instead of users. 

A rough diagram of the code is depicted below. On the right, the directories for the various layers are shown. 


  • include (require) the restify, restify plugins, validator and mongoose modules
  • create a restify server using the createServer function
  • the config directory has the db connection and helper functions to provide responses (output, error etc.)
  • the models directory has the schema and model for storing the reviews in MongoDB using Mongoose
  • the business logic is in the reviewController that is present in the controllers directory along with setupController that utilizes Restify features to control APIs - authorization, whitelisting, throttling
I have been using this layout and code organization while creating other apps in Node.js.


Learning Node.js on Udemy


After a bit of a struggle, I finally would like to believe that I have become comfortable with Node.js. I had initially tried learning Node.js through the course titled "Learn and Understand NodeJS" on Udemy by Anthony Alicea. The course is a bit unconventional as it goes deep into the guts of Node.js right at the start instead of providing an overview. I lost interest in it after a few lectures and almost gave up learning Node.js.

After a while, I decided to look for courses of shorter duration and found a course titled "A Simple Node.js/Mongo/Restify API in Less Than 3 Hours" by Jim Hlad. The USP of this course is that the lectures are of a short duration ~5-7 min. This helps in assimilating the content learned easily. The instructor explains the concepts while coding, instead of talking theory first, making the lectures short and crisp.

This course helped me grasp the basics of Node.js and try out things such as Web Sockets using Socket.io. It would be nicer if the instructor offers this course with Express or other more popular libraries instead of Restify. Also, it is worth noting that this course is aimed at building APIs using Node.js rather than a complete application thereby skipping the UI layer.


Extracting the Main Image from an Article/Web Page

For an app that I am building, I have been looking to extract the main image for an article from a webpage. Search on the net indicated that JSoup was probably "the" library to do it. But it too does not mention how to get the main image associated with the article. Any modern webpage has several images in it - advertising banners, site logo, social media sharing icons, article related images and more advertising. How do we extract the main image from an article? I was not willing to think and write code to do this; partly because I am lazy and also because I knew that this was done before and code should be available for this.

My initial search took me to Mashape. Searching through the APIs there, I found the free Article Analysis API by adlegant. While not required, I also decided to use the Unirest Java library by Mashape to access the API. My initial tests with the API were all positive, a simple call to the API endpoint with the URL to be analyzed and the credentials provided by Mashape was all that was required. It provided a JSON response similar to the one below:


{
  "author": [
    "Verge Staff"
  ],
  "categories": [
    "economy, business and finance"
  ],
  "cleaned_text": "Black Friday is right around the corner, and there are plenty of great deals to be had. We've been covering the deals for weeks now, but if you want to cut through the mess and just score the best deals you can find, you've come to the right place.\n\nAs to be expected, this year there are lots of deals to be had on TVs small and large, 1080p to 4K. You can also get a great price on last year's iPads, which are still better tablets than pretty much anything save for this year's iPads. If you're in the market for a smartwatch or fitness tracker, you can save some money on some really great options this weekend. And if you want to pick up a laptop or new headphones, there are deals to be found too.\n\nKeep in mind that the best deals won't last long and many of them are limited to Friday itself (or in rare occasions, Thursday too). To win the Black Friday game, you have to be aggressive and quick, there's no time to sleep when deals are to be had. That said, here are the 20 best Black Friday deals this year. Warm up your credit cards and get a good night's sleep, it's time to make the consumer machine work.",
  "date": "2014-11-26T19:15:45Z",
  "entities": [
    "Warm",
    "Black",
    "TVs"
  ],
  "image": "https://cdn1.vox-cdn.com/thumbor/IkFo5ddhDEXA2t_mUfezCdvGbUI=/35x0:606x381/1280x854/cdn0.vox-cdn.com/uploads/chorus_image/image/44236174/black-friday-branding-jc3.0.png",
  "language": "en",
  "link": "http://www.theverge.com/2014/11/26/7292895/best-black-friday-deals",
  "main_body": "<div class=\"m-article__entry\" gravityScore=\"204\" gravityNodes=\"3\"><p>Black Friday is right around the corner, and there are plenty of great deals to be had. We've been&#160;covering the deals for weeks now, but if you want to cut through the mess and just score the best deals you can find, you've come to the right place.</p>&#13;\n<p>As to be expected, this year there are lots of deals to be had on TVs small and large, 1080p to 4K. You can also get a great price on last year's iPads, which are still better tablets than pretty much anything save for this year's iPads. If you're in the market for a smartwatch or fitness tracker, you can save some money on some really great options this weekend. And if you want to pick up a laptop or new headphones, there are deals to be found too.</p>\n \n&#13;\n<p>Keep in mind that the best deals won't last long and many of them are limited to Friday itself (or in rare occasions, Thursday too). To win the Black Friday game, you have to be aggressive and quick, there's no time to sleep when deals are to be had. That said, here are the 20 best Black Friday deals this year. Warm up your credit cards and get a good night's sleep, it's time to make the consumer machine work.</p>&#13;\n &#13;\n &#13;\n &#13;\n &#13;\n &#13;\n &#13;\n \n       \n\n    </div>\n  ",
  "summary": [
    "That said, here are the 20 best Black Friday deals this year.",
    "Black Friday is right around the corner, and there are plenty of great deals to be had.",
    "We've been covering the deals for weeks now, but if you want to cut through the mess and just score the best deals you can find, you've come to the right place.",
    "Keep in mind that the best deals won't last long and many of them are limited to Friday itself (or in rare occasions, Thursday too).",
    "To win the Black Friday game, you have to be aggressive and quick, there's no time to sleep when deals are to be had."
  ],
  "tags": [
    "best deals",
    "laptop",
    "expected year",
    "said 20",
    "sleep",
    "best",
    "nights",
    "20",
    "tracker",
    "black",
    "friday game",
    "time make",
    "save",
    "mess",
    "right corner",
    "friday",
    "really great",
    "save money",
    "year ipads",
    "friday deals",
    "great",
    "headphones",
    "deals",
    "best black",
    "black friday"
  ],
  "text_sentiment": {
    "sentiment": 0.2986327561327561,
    "subjectivity": 0.4551911976911977,
    "word": "positive"
  },
  "title": "The 20 best Black Friday deals"
}

It perfectly extracts the main image associated with the article and provides the link. All one needs to do is to use the org.json package and get the image's value. Nice and clean!

But... as I was testing the API inside the app for extracting the images, I found that most of the time it was unavailable. I agree that it is free and there should be no expectation from my side, yet I felt that the downtime was too much. As I type this post, I see that it seems to fail for a few URLs that are perfectly valid. While Mashape provides some basic support for even free APIs, I did not utilize it.

Instead, I decided to go with a parser based code to get the main image. I found a class called ImageExtractor by Gavin Bisesi (Daenyth) written using JSoup. The code employs the technique used by Google Plus to extract images from shared URLs.  It does the job well and there is no dependency on external API endpoints.

While there would be many options available to scrape webpages and get the images, JSoup will probably be the primary option and APIs would be the next. If you are short on time and willing to spend, paid APIs maybe an option. If not, you would have to try out classes such as the one listed above to get the job done.


Search APIs - Cheap, Fast, Good (choose only 2)

I tried Faroo - a search API that I was considering to use for a mobile app. Faroo is free for up to a million requests a month. Probably the most generous among the free plans available from the competition. Signup is straight forward but it took a few days to receive the key. Just append the key to the search requests and you are done.

It appears that Faroo is more focused on Europe or the US as search results for India focused products did not produce any results. Since the app that I am looking to build is India-centric, I am looking at a search API that can bring in results related to India. Many of the searches produced no results.

I next tried Webhose.io, another search API but a more restrictive plan of 1000 requests per month. Signup done and received the key in less than a minute. Search results brought in results on India-specific terms. The quality of results was impressive too. It is the pricing that is steep and preventing me from going ahead with using the API.

As it stands, I have to decide between free/low price with not much quality or expensive results with quality. Or maybe I will figure out a third option. I will continue exploring for now.