<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Saga do programador &#187; restfulie</title>
	<atom:link href="http://www.sagadoprogramador.com.br/tag/restfulie/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sagadoprogramador.com.br</link>
	<description></description>
	<lastBuildDate>Wed, 18 Aug 2010 13:34:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cliente para Restfulie C#</title>
		<link>http://www.sagadoprogramador.com.br/2010/01/cliente-para-restfulie-c/</link>
		<comments>http://www.sagadoprogramador.com.br/2010/01/cliente-para-restfulie-c/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 23:43:52 +0000</pubDate>
		<dc:creator>Luiz Costa</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[hipermedia]]></category>
		<category><![CDATA[restfulie]]></category>

		<guid isPermaLink="false">http://www.sagadoprogramador.com.br/?p=430</guid>
		<description><![CDATA[No final do ano passado o Guilherme Silveira da Caelum , lançou a api Restfulie.
Basicamente o Restfulie implementa as idéias do Jim Webber e Ian Robson em relação a media types que suportam hipermídia, content negotiation e muito mais relacionado a Rest.
Este projeto fez bastante barulho na comunidade, inclusive recebendo elogios do próprio Jim.
O Restfulie [...]]]></description>
			<content:encoded><![CDATA[<p>No final do ano passado o <a href="http://guilhermesilveira.wordpress.com/" target="_blank">Guilherme Silveira</a> da <a href="http://www.caelum.com.br" target="_blank">Caelum </a>, lançou a api <a href="http://restfulie.caelum.com.br/" target="_blank">Restfulie</a>.<br />
Basicamente o Restfulie implementa as idéias do <a href="http://jim.webber.name/" target="_blank">Jim Webber</a> e <a href="http://iansrobinson.com/" target="_blank">Ian Robson</a> em relação a media types que suportam hipermídia, content negotiation e muito mais relacionado a <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm" target="_blank">Rest</a>.<br />
Este projeto fez <a href="http://www.infoq.com/news/2009/11/restfulie-hypermedia-services" target="_blank">bastante barulho</a> na <a href="http://www.infoq.com/news/2009/12/restfulie-interview" target="_blank">comunidade</a>, inclusive recebendo <a href="http://jim.webber.name/2009/11/03/0a252e61-a132-42ab-8547-69e269c3c075.aspx">elogios do próprio Jim</a>.</p>
<p>O Restfulie teve sua versão original implementada em<a href="http://restfulie.caelum.com.br/restful_rails" target="_blank"> ruby</a> e logo depois foi lançada a versão para<a href="http://github.com/caelum/restfulie-java" target="_blank"> Java</a>, mas ainda não havia nenhuma versão para .Net. Foi então que eu e o Sergio Junior aqui do blog resolvemos escrever uma versão para <a href="http://restfulie.caelum.com.br/restful_csharp" target="_blank">C# da api</a>.<br />
Começamos a implementar a parte cliente do Restfulie e fizemos isto utilizando as características dinâmicas da versão 4.0 do C#.<br />
A idéia é que podemos fazer o client em C# bem próximo da idéia inicial que foi escrita em Ruby.<br />
<span id="more-430"></span><br />
Aqui vamos mostrar alguns exemplos de utilização desta versão em C#.<br />
Um recurso Pedido pode ter sua representação em xml como a que segue:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pedido<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>26/12/2009 11:40<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;total<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>300.00<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/total<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;atom:link</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;refresh&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://www.caelum.com.br/pedidos/1&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">xmlns:atom</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2005/Atom&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;atom:link</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;update&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://www.caelum.com.br/pedidos/1&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">xmlns:atom</span>=<span style="color: #ff0000;">&quot;...&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;atom:link</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;pagar&quot;</span></span>
<span style="color: #009900;">		  <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://www.caelum.com.br/pedidos/1/pagar&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">xmlns:atom</span>=<span style="color: #ff0000;">&quot;...&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;atom:link</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;excluir&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://www.caelum.com.br/pedidos/1</span>
<span style="color: #009900;">          xmlns:atom=&quot;</span>...<span style="color: #ff0000;">&quot;/&gt;</span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;atom:link</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;GetCliente&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://www.caelum.com.br/pedidos/1/cliente</span>
<span style="color: #009900;">          xmlns:atom=&quot;</span>...<span style="color: #ff0000;">&quot;/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pedido<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Respare que a representação deste recurso além de conter  os dados do recurso, possui também  todas as possíveis transições de estado do recurso.<br />
Além  disso, ainda é possível seguir os links e obter outros recursos relacionados: este é o conceito de Hipermedia tanto utilizado na web humana.</p>
<p><strong>Utilizando o Restfulie client C#</strong></p>
<p>Primeiro começamos fazendo o download da <a href="http://code.google.com/p/restfulie/downloads/list" target="_blank">DLL </a>e então para obter a representação de um recurso em C#, podemos utilizar algo bem parecido com ruby, o acesso a um entry point:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">dynamic  pedido <span style="color: #008000;">=</span> Restfulie.<span style="color: #0000FF;">At</span><span style="color: #000000;">&#40;</span>“www.<span style="color: #0000FF;">caelum</span>.<span style="color: #0000FF;">com</span>.<span style="color: #0000FF;">br</span><span style="color: #008000;">/</span>pedidos<span style="color: #008000;">/</span>1.<span style="color: #0000FF;">xml</span>”<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Get</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Uma vez com a representação do recurso em mãos, podemos obter as informações do mesmo e seguir seus links.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>
   <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">format</span><span style="color: #000000;">&#40;</span>“O valor total <span style="color: #0600FF;">do</span> pedido é<span style="color: #008000;">:</span><span style="color: #000000;">&#123;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#125;</span>”, pedido.<span style="color: #0000FF;">Total</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>
   <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">format</span><span style="color: #000000;">&#40;</span>“A data <span style="color: #0600FF;">do</span> pedido é<span style="color: #008000;">:</span> <span style="color: #000000;">&#123;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#125;</span>”, pedido.<span style="color: #0000FF;">Data</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>É possível invocar métodos e alterar o estado do pedido, além de navegar em suas relações com outros recursos.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">   pedido.<span style="color: #0000FF;">Pagar</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   pedido.<span style="color: #0000FF;">Cancelar</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   pedido.<span style="color: #0000FF;">Excluir</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Estas chamadas de métodos são remotas, baseadas nos links fornecidos pelo xml e nos protocolos http e media types bem conhecidos.<br />
Para recuperar um recurso relacionado através dos links:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">  dynamic cliente <span style="color: #008000;">=</span> pedido.<span style="color: #0000FF;">GetCliente</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>
     <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">format</span><span style="color: #000000;">&#40;</span>“Nome <span style="color: #0600FF;">do</span> cliente <span style="color: #000000;">&#123;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#125;</span>”, cliente.<span style="color: #0000FF;">nome</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Ainda podemos obter as informações sobre sua resposta web, para analisar headers ou response codes:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">  Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>pedido.<span style="color: #0000FF;">WebResponse</span>.<span style="color: #0000FF;">StatusCode</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>As vezes a representação xml do recurso pode conter tags com o caracter “-”.<br />
Por exemplo:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;city<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Sao Paulo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;population<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;size<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>18000000<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/size<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;growth<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/growth<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/population<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;updated-at<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10/01/2010<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/updated-at<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;next_largest&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://localhost:3000/cities/18.xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/city<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Obtendo o recurso:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">dynamic city <span style="color: #008000;">=</span> Restfulie.<span style="color: #0000FF;">At</span><span style="color: #000000;">&#40;</span>“http<span style="color: #008000;">:</span><span style="color: #008080; font-style: italic;">//localhost:3000/cities/17.xml”).Get();</span></pre></div></div>

<p>Acessando o atributo “update-at”:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">  Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>
     <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">format</span><span style="color: #000000;">&#40;</span>“A data de atualização é <span style="color: #000000;">&#123;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#125;</span>”,city.<span style="color: #0000FF;">Update_At</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>
     <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">format</span><span style="color: #000000;">&#40;</span>“População <span style="color: #000000;">&#123;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#125;</span>”, city.<span style="color: #0000FF;">Population</span>.<span style="color: #0000FF;">size</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Note que em na vesão C#, não acessamos o atributo update-at com caracter “-”, este é substituído por “_”. Então quando for necessário acessar um atributo ou método que utilize o “-”, substituída por “_”.<br />
Com as novas capacidades dinâmicas do C# 4.0, conseguimos atingir um resultado bem próximo ao que é possível na versão ruby.<br />
Aqui segue um comparativo básico:</p>
<p>Em Ruby:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  pedido = Restfulie.<span style="color:#9900CC;">at</span><span style="color:#006600; font-weight:bold;">&#40;</span>“http:<span style="color:#006600; font-weight:bold;">//</span>restfulie<span style="color:#006600; font-weight:bold;">-</span>test.<span style="color:#9900CC;">heroku</span>.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">/</span>orders<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">14</span>”<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">get</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> pedido.<span style="color:#9900CC;">customer</span><span style="color:#006600; font-weight:bold;">-</span>name
  pedido.<span style="color:#9900CC;">cancel</span></pre></div></div>

<p>Em C#</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">  pedido <span style="color: #008000;">=</span> Restfulie.<span style="color: #0000FF;">At</span><span style="color: #000000;">&#40;</span>“http<span style="color: #008000;">:</span><span style="color: #008080; font-style: italic;">//restfulie-test.heroku.com/orders/14.xml”).Get();</span>
  Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>pedido.<span style="color: #0000FF;">customer_name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  pedido.<span style="color: #0000FF;">Cancel</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Por enquanto só temos a versão do client em C#. Ainda falta uma série de features a serem implementadas. No site do projeto, tem uma lista de coisas que necessitam de ser feitas no cliente<br />
para que ele fique tão maduro quanto as versões em ruby ou Java. Vamos continuar trabalhando no cliente para isso.</p>
<p>Se você está interessado em ajudar neste projeto, deixe um comentário ou mande um e-mail para nós.</p>
<p>Para saber mais:</p>
<p><a href="http://github.com/caelum/restfulie-csharp">http://github.com/caelum/restfulie-csharp</a><br />
<a href="http://restfulie.caelumobjects.com/">http://restfulie.caelumobjects.com/</a></p>
<p>Para saber mais sobre as idéias por trás do Restfulie, segue o link do artigo na infoq.<br />
<a href="http://www.infoq.com/articles/webber-rest-workflow">http://www.infoq.com/articles/webber-rest-workflow</a></p>
<p>Até a Próxima<br />
Luiz Costa<br />
Sérgio Júnior.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sagadoprogramador.com.br/2010/01/cliente-para-restfulie-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
