REST API for your WP7 App Agnius Paradnikas @Agnius101
About me • .NET web developer for more than 6 years. • Recently focused on mobile app development. • Graduated KTU. • Created first WP7 app more than 1 year ago. • Now developing Weepin WP7 app on my spare time.
My first WP7 app
www.weepin.lt
• Weepin won 2-nd place in additional round in WP7 app challenge. • At the moment is the first in The Best App category in LOGIN2012.
REST API ? Server DB
REST API REST API Server DB
REST API • Stands for REpresentational State Transfer. • Client-server: separation of concerns. • Statless: no state is saved on server, if needed it is saved on client. • Cacheable: clients can cache responses.
REST API • Easy to scale out. • Easy to build, maintain and modify. • Less overhead - higher performance. • Implementation does not require high technical skill level.
REST API example http://www.domain.com/api/names?count=2 { "Names": [ { "NameHTML": "<b>Ipr</b>amol", "NameText": "Ipramol", "IsShortname": true }, { "NameHTML": "<b>Ipr</b>atropiumbromid Arrow", "NameText": "Ipratropiumbromid Arrow", "IsShortname": false } ] }
• Simple REST and HTTP API Client for .NET. • No need to know about low-level networking. • Covers all needed cases like: – Async requests (all requests in WP7 should be async!); – Data mapping; – Authentication; – JSON/XML serialization; – Implement your own custom serialization; – Etc.
Getting Started • Add reference via NuGet. • And you’re done.
TodoApp DEMO and Example http://todoapp.agnius.lt/api/todos [ { "id": "1", "todo": "Make dinner.", "username": "Agnius101", "created": "2012-04-09", }, { "id": "2", "todo": "Finish WP7 presentation", "username": "Agnius101", "created": "2012-04-13", ] }, ... ]
var client = new RestClient(); client.BaseUrl = "http://todoapp.agnius.lt/api"; RestRequest request = new RestRequest(); request.Resource = "todos"; request.RequestFormat = DataFormat.Json; request.Method = Method.GET;
client.ExecuteAsync<List<TodoModel>>(request, (response) => { if (response.ResponseStatus != ResponseStatus.Error && response.StatusCode == System.Net.HttpStatusCode.OK) { this.todosListBox.ItemsSource = response.Data; } else { MessageBox.Show("Ups! Error occured."); } });
REST API Server side • Choose any technology you want: – .NET – JAVA – PHP – Python – Ruby – and so on…
• Cheap and easy way to implement server side REST API. • For PHP and MySQL. • Supports all needed tools to build REST API: – MVC – Routing – ORM – Logging & Profiling – and so on…
Getting Started • Go to http://www.doophp.com/ • Download latest version. • Create MySQL database tables. • Make configuration changes in: – appprotectedconfigcommon.conf.php – appprotectedconfigdb.conf.php
Define routes appprotectedconfigroutes.conf.php <?php $route['get']['/todos'] = array('MainController', 'getAll'); $route['get']['/todos/:id'] = array('MainController', 'getById'); $route['post']['/todos/new'] = array('MainController', 'createNew'); $route['post']['/todos/:id'] = array('MainController', 'deleteTodo'); ?>
Create models appprotectedmodelTodo.php <?php class Todo{ public $id; public $todo; public $username; public $created; public $_table = 'todos'; public $_primarykey = 'id'; public $_fields = array('id','user_id','todo','username', 'created'); } ?>
Implement controllers appprotectedcontrollerMainController.php <?php class MainController extends DooController{ public function getAll(){ $todos = $this->db()->find('Todo'); $result = json_encode($todos); $this->setContentType('json', 'utf-8'); echo $result; } } ?>
Upload everything to the server If you are lucky everything should work.
My own experience • Never try to do everything yourself. • Be fast. • Think how to add Facebook and YouTube effect to your app. • Read carefully certification requirements before submitting your app.
How can I help you • Support creating WP7 app from scratch. • Setup REST API and DB in both .NET and PHP technologies. • Share my experience if you decide to move with advanced techniques like MvvM. • Give advice on usability. • Give advice how to improve functionality. • I don’t have experience creating games on XNA.
Follow me on Twitter @Agnius101

REST API for your WP7 App

  • 1.
    REST API foryour WP7 App Agnius Paradnikas @Agnius101
  • 2.
    About me • .NET web developer for more than 6 years. • Recently focused on mobile app development. • Graduated KTU. • Created first WP7 app more than 1 year ago. • Now developing Weepin WP7 app on my spare time.
  • 3.
  • 4.
  • 5.
    • Weepin won2-nd place in additional round in WP7 app challenge. • At the moment is the first in The Best App category in LOGIN2012.
  • 6.
    REST API ? Server DB
  • 7.
  • 8.
    REST API • Standsfor REpresentational State Transfer. • Client-server: separation of concerns. • Statless: no state is saved on server, if needed it is saved on client. • Cacheable: clients can cache responses.
  • 9.
    REST API • Easy to scale out. • Easy to build, maintain and modify. • Less overhead - higher performance. • Implementation does not require high technical skill level.
  • 10.
    REST API example http://www.domain.com/api/names?count=2 { "Names": [ { "NameHTML": "<b>Ipr</b>amol", "NameText": "Ipramol", "IsShortname": true }, { "NameHTML": "<b>Ipr</b>atropiumbromid Arrow", "NameText": "Ipratropiumbromid Arrow", "IsShortname": false } ] }
  • 11.
    • Simple RESTand HTTP API Client for .NET. • No need to know about low-level networking. • Covers all needed cases like: – Async requests (all requests in WP7 should be async!); – Data mapping; – Authentication; – JSON/XML serialization; – Implement your own custom serialization; – Etc.
  • 12.
    Getting Started • Addreference via NuGet. • And you’re done.
  • 13.
    TodoApp DEMO andExample http://todoapp.agnius.lt/api/todos [ { "id": "1", "todo": "Make dinner.", "username": "Agnius101", "created": "2012-04-09", }, { "id": "2", "todo": "Finish WP7 presentation", "username": "Agnius101", "created": "2012-04-13", ] }, ... ]
  • 14.
    var client =new RestClient(); client.BaseUrl = "http://todoapp.agnius.lt/api"; RestRequest request = new RestRequest(); request.Resource = "todos"; request.RequestFormat = DataFormat.Json; request.Method = Method.GET;
  • 15.
    client.ExecuteAsync<List<TodoModel>>(request, (response) => { if (response.ResponseStatus != ResponseStatus.Error && response.StatusCode == System.Net.HttpStatusCode.OK) { this.todosListBox.ItemsSource = response.Data; } else { MessageBox.Show("Ups! Error occured."); } });
  • 16.
    REST API Serverside • Choose any technology you want: – .NET – JAVA – PHP – Python – Ruby – and so on…
  • 17.
    • Cheap andeasy way to implement server side REST API. • For PHP and MySQL. • Supports all needed tools to build REST API: – MVC – Routing – ORM – Logging & Profiling – and so on…
  • 18.
    Getting Started • Go to http://www.doophp.com/ • Download latest version. • Create MySQL database tables. • Make configuration changes in: – appprotectedconfigcommon.conf.php – appprotectedconfigdb.conf.php
  • 19.
    Define routes appprotectedconfigroutes.conf.php <?php $route['get']['/todos'] =array('MainController', 'getAll'); $route['get']['/todos/:id'] = array('MainController', 'getById'); $route['post']['/todos/new'] = array('MainController', 'createNew'); $route['post']['/todos/:id'] = array('MainController', 'deleteTodo'); ?>
  • 20.
    Create models appprotectedmodelTodo.php <?php class Todo{ public $id; public $todo; public $username; public $created; public $_table = 'todos'; public $_primarykey = 'id'; public $_fields = array('id','user_id','todo','username', 'created'); } ?>
  • 21.
    Implement controllers appprotectedcontrollerMainController.php <?php class MainControllerextends DooController{ public function getAll(){ $todos = $this->db()->find('Todo'); $result = json_encode($todos); $this->setContentType('json', 'utf-8'); echo $result; } } ?>
  • 22.
    Upload everything tothe server If you are lucky everything should work.
  • 23.
    My own experience •Never try to do everything yourself. • Be fast. • Think how to add Facebook and YouTube effect to your app. • Read carefully certification requirements before submitting your app.
  • 24.
    How can Ihelp you • Support creating WP7 app from scratch. • Setup REST API and DB in both .NET and PHP technologies. • Share my experience if you decide to move with advanced techniques like MvvM. • Give advice on usability. • Give advice how to improve functionality. • I don’t have experience creating games on XNA.
  • 25.
    Follow me onTwitter @Agnius101