Sunday, November 15, 2015

iOS - How to call web APIs with Alamofire (Swift2)

AFNetworking is one of the most popular third-party libraries on iOS and OS X. It was awarded the 2012 Best iOS Library Award in our 2012 Reader’s Choice Awards, and is one of the most widely-used open source projects on Github with over 14K stars and 4K forks.
Recently the creator and maintainer of AFNetworking, Mattt Thompson, released a new networking library like AFNetworking, but designed specifically to use modern Swift conventions and language features: Alamofire.

Retrieving Data from API

You might be asking why you need Alamofire in the first place. Apple provides the NSURLSession class and related classes for downloading content via HTTP, so why complicate things with another third party library?
The short answer is that Alamofire is based on NSURLSession, but it frees you from writing boilerplate code and makes writing networking code much easier. You can access data on the Internet with very little effort, and your code will be much cleaner and easier to read.

Sample API call

import Alamofire

Next, add the following code to viewDidLoad() just below setupView():

Alamofire.request(.GET, "https://api.500px.com/v1/photos").responseJSON() {
  (_, _, data, _) in
  println(data)
}


You'll see the following message appear in the console:

Optional({
    error = "Consumer key missing.";
    status = 401;
})


Saturday, November 14, 2015

How to build a REST API using PHP and MySQL

This article contains how I built the API for the Wedding Planner application. The API is RESTFULL and will return JSON objects for the requests. The databaseI used is MySQL and the API is written using pure PHP.
REST API's job is to get the request from client, interact with database and finally give the response back to client.


The tools I have used


  • XAMP - for APACHE and MySQL servers
  • PHP MyAdmin as a MySQL client
  • NetBeans to implement PHP project



Monday, November 2, 2015

Working with Entity Framework Code First

What is Entity Framework

Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write.


How to install Entity Framework

  1. Open Package Manager Console
  2. Run,
        Install-Package EntityFramework


What is Code First 

Entity Framework introduced Code-First approach from Entity Framework 4.1. Code-First is mainly useful in Domain Driven Design. With the Code-First approach, you can focus on the domain design and start creating classes as per your domain requirement rather than design your database first and then create the classes which match your database design. Code-First APIs will create the database on the fly based on your entity classes and configuration.

Code First Work Flow

Write application domain classes and context class→ configure domain classes for additional mapping requirements → Hit F5 to run the application → Code First API creates new database or map existing database with domain classes → Seed default/test data into the database → Finally launches the application