rest api for your wp7 app
DESCRIPTION
My presentation done on Windows Phone App Camp Lithuania. I was also helping as a mentor. It was very interesting experience for me.TRANSCRIPT
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
DBServer
?
REST API
DBServer
REST API
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:
– app\protected\config\common.conf.php
– app\protected\config\db.conf.php
Define routesapp\protected\config\routes.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 modelsapp\protected\model\Todo.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 controllersapp\protected\controller\MainController.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 serverIf 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