rest api for your wp7 app

25
REST API for your WP7 App Agnius Paradnikas @Agnius101

Upload: agnius-paradnikas

Post on 04-Jul-2015

1.854 views

Category:

Technology


1 download

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

Page 1: REST API for your WP7 App

REST API for your WP7 App

Agnius Paradnikas

@Agnius101

Page 2: REST API for your WP7 App

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.

Page 3: REST API for your WP7 App

My first WP7 app

Page 4: REST API for your WP7 App

www.weepin.lt

Page 5: REST API for your WP7 App

• 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.

Page 6: REST API for your WP7 App

REST API

DBServer

?

Page 7: REST API for your WP7 App

REST API

DBServer

REST API

Page 8: REST API for your WP7 App

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.

Page 9: REST API for your WP7 App

REST API

• Easy to scale out.

• Easy to build, maintain and modify.

• Less overhead - higher performance.

• Implementation does not require high technical skill level.

Page 10: REST API for your WP7 App

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

}]

}

Page 11: REST API for your WP7 App

• 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.

Page 12: REST API for your WP7 App

Getting Started• Add reference via NuGet.

• And you’re done.

Page 13: REST API for your WP7 App

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",]

},

...]

Page 14: REST API for your WP7 App

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;

Page 15: REST API for your WP7 App

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.");

}

});

Page 16: REST API for your WP7 App

REST API Server side

• Choose any technology you want:

– .NET

– JAVA

– PHP

– Python

– Ruby

– and so on…

Page 17: REST API for your WP7 App

• 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…

Page 18: REST API for your WP7 App

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

Page 19: REST API for your WP7 App

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');

?>

Page 20: REST API for your WP7 App

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');

}

?>

Page 21: REST API for your WP7 App

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;

}

}

?>

Page 22: REST API for your WP7 App

Upload everything to the serverIf you are lucky everything should work.

Page 23: REST API for your WP7 App

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.

Page 24: REST API for your WP7 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.

Page 25: REST API for your WP7 App

Follow me on Twitter

@Agnius101