<?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>PAW &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/paw/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Wed, 10 Feb 2021 18:49:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Making An iOS App For The Daily WTF API</title>
		<link>https://blog.xojo.com/2016/01/20/making-an-ios-app-for-the-daily-wtf-api/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Wed, 20 Jan 2016 00:00:00 +0000</pubDate>
				<category><![CDATA[iOS]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Cats]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[PAW]]></category>
		<category><![CDATA[REST]]></category>
		<guid isPermaLink="false">http://blogtemp.xojo.com/2016/01/20/making-an-ios-app-for-the-daily-wtf-api/</guid>

					<description><![CDATA[Use web services to display articles from The Daily WTF technology snafu web site.]]></description>
										<content:encoded><![CDATA[<p>If you work in the technologiy industry, I&#8217;m sure you&#8217;ve heard of the <a href="http://thedailywtf.com">Daily WTF</a> site. Their fun stories about technology gone wrong makes it one of my favorite web sites.</p>
<p><span id="more-233"></span></p>
<p>Recently they just <a href="http://thedailywtf.com/articles/tdwtf-api">announced a web services API</a> for accessing their articles and other information. And as I&#8217;ve talked about <a href="../../../com/xojo/blog/test-web-services-with-rest-tester.html" target="_blank" rel="noopener">in</a> <a href="../../../com/xojo/blog/format-sql.html" target="_blank" rel="noopener">other</a> <a href="../../../com/xojo/blog/how-to-write-a-slackbot-in-less-than-20-lines-of-code.html" target="_blank" rel="noopener">posts</a> and <a href="https://www.youtube.com/playlist?list=PLPoq910Q9jXiH5A32myqHwd1WLuUnBTuO" target="_blank" rel="noopener">webinars</a>, it&#8217;s often quite easy to use web services with Xojo apps.</p>
<p>I&#8217;ll show you how you can quickly create an iOS app that shows the most recent TDWTF posts and then displays them when you tap on their title.</p>
<p>To start, create a Xojo iOS project.</p>
<p>On the default View (View1), use the Inspector to set a couple properties:</p>
<ul>
<li>BackButtonTitle: Articles</li>
<li>Title: The Daily WTF</li>
</ul>
<p>Now drag a <a href="http://developer.xojo.com/iostable">Table</a> from the Library onto the View and stretch it to fit the entire layout area. Name the table <strong>ArticleTable</strong>. This Table will be used to display the list of recent articles.</p>
<p>To get the articles, you just have to make the appropriate API call. Looking at the <a href="https://github.com/tdwtf/WtfWebApp/blob/master/Docs/API.md">API docs</a>, there is an API command called /articles/recent which returns a JSON document of the recent articles and related information. This is the actual API call. To see its JSON results, use a tool like <a href="https://luckymarmot.com/paw">Paw</a> (now with <a href="../../../com/xojo/blog/generating-xojo-code-from-paw.html">Xojo extensions</a>) or <a href="../../../com/xojo/blog/test-web-services-with-rest-tester.html">RESTy</a> with this URL (a browser will also show the unformatted JSON):</p>
<p><a href="http://thedailywtf.com/api/articles/recent">http://thedailywtf.com/api/articles/recent</a></p>
<p>If you look at the JSON results, you&#8217;ll see that it returns an array of articles with a &#8220;Title&#8221; entry containing the title of the article and a &#8220;Url&#8221; entry containing the URL of the article.</p>
<p>To call this in the Xojo app, I&#8217;ll use an <a href="http://developer.xojo.com/xojo-net-httpsocket">HTTPSocket</a>. In the Library, drag the item called &#8220;Generic Object&#8221; onto the Layout. It will appear in the Shelf at the bottom. In its Inspector, change the name to <strong>WTFSocket</strong> and set its super to &#8220;Xojo.Net.HTTPSocket&#8221;.</p>
<p>Now add an event handler to the socket, right-&gt;click on the socket, select &#8220;Add To&#8221; and then &#8220;Event Handler&#8221;. Add the PageReceived event handler. In it, put this code to request the articles:</p>
<pre>// Convert the binary Content data to JSON text and then
// parse it to an array.
Dim jsonText As Text = TextEncoding.UTF8.ConvertDataToText(Content)
Dim jsonArray() As Auto = Data.ParseJSON(jsonText)

// Loop through the array and add each article to
// the table.
ArticleTable.AddSection("")
For i As Integer = 0 To jsonArray.Ubound
  Dim article As Dictionary = jsonArray(i)

  Dim cell As iOSTableCellData = ArticleTable.CreateCell
  cell.Text = article.Value("Title")
  cell.Tag = article.Value("Url")

  Dim author As Dictionary = article.Value("Author")
  cell.DetailText = article.Value("DisplayDate") + " by " + author.Value("Name")
  ArticleTable.AddRow(0, cell)
Next</pre>
<p>The URL is being put into the Tag for the cell so that it can be used later to display the article when its row is tapped.</p>
<p>You&#8217;ll also see I&#8217;m also accessing the &#8220;DisplayDate&#8221; value and grabbing the &#8220;Name&#8221; from the &#8220;Author&#8221; object in the JSON.</p>
<p>Now you&#8217;ll want to call the web service. Add the Open event handler to the View with this code:</p>
<pre>// Call the /articles/recent web service
WTFSocket.Send("GET", "http://thedailywtf.com/api/articles/recent")</pre>
<p>Run the project and you&#8217;ll see the list of recent article appear:</p>
<p><img fetchpriority="high" decoding="async" class="aligncenter" title="WTFArticleList.png" src="https://blog.xojo.com/wp-content/uploads/2016/01/WTFArticleList.pngt1466486449161ampwidth480ampheight742" sizes="(max-width: 480px) 100vw, 480px" alt="WTFArticleList.png" width="480" height="742" /></p>
<p>The final step is to display the article when it is tapped in the list. You&#8217;ll want another View to display the article (Insert-&gt;View). Name the view <strong>ArticleView</strong>. On this View, set the NavigationBarVisible property to ON and drag an HTML Viewer onto it. Make the HTML Viewer fill the entire View layout and name it <strong>ArticleViewer</strong>.</p>
<p>Add a method to ArticleView called <strong>ShowArticle(url As Text)</strong> with this code:</p>
<pre>ArticleViewer.LoadURL(url)</pre>
<p>This method loads the URL into the HTML Viewer and is called when its row is tapped in the list of articles.</p>
<p>Speaking of which, back on View1 add the Action event handler to the table with this code:</p>
<pre>// Get the URL from the tag for the tapped row
Dim url As Text = Me.RowData(section, row).Tag

// Display the article on the new view and show it
Dim v As New ArticleView
v.ShowArticle(url)
PushTo(v)</pre>
<p>Now run the project. You&#8217;ll see the list of article as before. Tap on an article to show the new view with the article.</p>
<p><img decoding="async" class="aligncenter" title="WTFArticle.png" src="https://blog.xojo.com/wp-content/uploads/2016/01/WTFArticle.pngt1466486449161ampwidth480ampheight742" sizes="(max-width: 480px) 100vw, 480px" alt="WTFArticle.png" width="480" height="742" /></p>
<p>Take a look at the full API to see what other cool features you can add to the app. Here are some suggestions:</p>
<ul>
<li>Display a random article</li>
<li>Display more than the last 8 recent articles</li>
<li>Cache the article contents on the device and use the cache when the article is displayed again</li>
<li>Display articles by month/year</li>
</ul>
<p><!--HubSpot Call-to-Action Code --> <span id="hs-cta-wrapper-7bcc3f87-4442-4a30-90b2-c9264729660a" class="hs-cta-wrapper"> <span id="hs-cta-7bcc3f87-4442-4a30-90b2-c9264729660a" class="hs-cta-node hs-cta-7bcc3f87-4442-4a30-90b2-c9264729660a"><br />
<!-- [if lte IE 8]>


<div id="hs-cta-ie-element"></div>


<![endif]--> <a href="http://blog.xojo.com/2015/10/01/cats-up-using-httpsocket-with-the-cat-rest-api/" target="_blank" rel="noopener"><img decoding="async" id="hs-cta-img-7bcc3f87-4442-4a30-90b2-c9264729660a" class="hs-cta-img aligncenter" style="border-width: 0px; margin: 0 auto; display: block; margin-top: 20px; margin-bottom: 20px;" src="https://blog.xojo.com/wp-content/uploads/2016/01/7bcc3f87-4442-4a30-90b2-c9264729660a.png" alt="Use HTTPSocket with Cat REST API" width="432" height="73" align="middle" /></a></span></span> <!-- end HubSpot Call-to-Action Code --></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Generating Xojo Code From Paw</title>
		<link>https://blog.xojo.com/2016/01/06/generating-xojo-code-from-paw/</link>
		
		<dc:creator><![CDATA[Greg O'Lone]]></dc:creator>
		<pubDate>Wed, 06 Jan 2016 00:00:00 +0000</pubDate>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[PAW]]></category>
		<category><![CDATA[REST]]></category>
		<guid isPermaLink="false">http://blogtemp.xojo.com/2016/01/06/generating-xojo-code-from-paw/</guid>

					<description><![CDATA[Easy HTTP RESTful requests in Xojo and Paw]]></description>
										<content:encoded><![CDATA[<p>At Xojo, we work with a lot of HTTP REST APIs. So many in fact that I&#8217;ve spent time creating custom test harnesses to make sure that whatever I was currently coding would be compatible as well as being a test suite just in case the API changed in some subtle way (whether it be a bug fix or an API refactor gone awry). The problem with the custom test harnesses is that they&#8217;re not very portable and you end up having to create a new one for each API that you interface with.</p>
<p><span id="more-274"></span></p>
<p>When we created the iOS app for XDC 2015 last spring, I decided to take a different route and try out a few of the commercial REST API clients to see if any of them would make my life easier. The one I settled on was <a href="https://luckymarmot.com/paw" target="_blank" rel="nofollow noopener">Paw from LuckyMarmot</a>, primarily for its support of extensions written in JavaScript.</p>
<p><img loading="lazy" decoding="async" style="display: block; margin-left: auto; margin-right: auto;" title="PAW.png" src="https://blog.xojo.com/wp-content/uploads/2016/01/PAW.pngt1466486449161ampwidth253ampheight268" sizes="auto, (max-width: 253px) 100vw, 253px" alt="PAW.png" width="253" height="268" /></p>
<p>There is a <a href="https://luckymarmot.com/paw/extensions/" target="_blank" rel="nofollow noopener">small library</a> of extensions available on the Paw website, most of which are for code examples and that got me thinking&#8230; why not have examples of the request in Xojo code, both old and new framework so that I don&#8217;t need to do that by hand every time.</p>
<p><strong>Examples</strong></p>
<p>For the sake of this example, let&#8217;s say that we&#8217;re making a request to login to a fictitious API. The URL of this request is https://www.example.com/api/login, it expects that you will pass the username (name) and password (1234) as URL parameters and that a preshared key is to be included in the body of the request as JSON.</p>
<p>Paw comes with an extension for creating cURL requests and the output of that extension looks like this:</p>
<pre>curl -X "GET" "https://www.example.com/api/login?username=name&amp;password=1234" 
 -H "Content-Type: application/json" 
 -d "{"preshared-key":"QU^k9h36zdF8nx7Y"}"</pre>
<p>Using our Paw extensions, the Old Framework Xojo extension generates code that looks like this:</p>
<pre>// Login (Old Framework)

// Set up the socket
dim h as new HTTPSecureSocket
h.Secure = True
h.ConnectionType = h.TLSv12
h.setRequestHeader("Content-Type","application/json")

// JSON
Dim js As New JSONItem
js.Value("preshared-key") = "QU^k9h36zdF8nx7Y"

// Convert Dictionary to JSON Text
Dim data As String = js.toString()

// Assign to the Request's Content
h.SetRequestContent(data,"application/json")

// Set the URL
dim url as string = "https://www.example.com/api/login?username=name&amp;password=1234"

// Send Synchronous Request
dim s as string = h.SendRequest("GET",url,30)</pre>
<p>If you&#8217;re using the new framework extension, it generates code that looks like this:</p>
<pre>// Login (New Framework)

// Set up the socket
// "mySocket" should be a property stored elsewhere so it will not go out of scope
mySocket = new Xojo.Net.HTTPSocket
mySocket.RequestHeader("Content-Type") = "application/json"

// JSON
Dim d As New Dictionary
d.Value("preshared-key") = "QU^k9h36zdF8nx7Y"

// Convert Dictionary to JSON Text
Dim json As Text = Xojo.Data.GenerateJSON(d)

// Convert Text to Memoryblock
Dim data As Xojo.Core.MemoryBlock = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(json)

// Assign to the Request's Content
mySocket.SetRequestContent(data,"application/json")

// Set the URL
dim url as Text = "https://www.example.com/api/login?username=name&amp;password=1234"

// Send Asynchronous Request
mySocket.Send("GET",url)</pre>
<p>These extensions are available for download using the link below. Once downloaded, extract the folders into the Paw Extensions folder which you can reach by going to the Paw menu and selecting Extensions-&gt;Open Extensions Directory. Once they&#8217;re in there, you can make them available by selecting Extensions-&gt;Reload All Extensions.</p>
<p>Versions for the Classic Framework and Xojo Framework are available on GitHub:</p>
<ul>
<li><a href="https://github.com/xojo/Paw-XojoClassicGenerator">Paw Extension for HTTPSocket</a></li>
<li><a href="https://github.com/xojo/Paw-XojoiOSGenerator">Paw Extension for Xojo.Net.HTTPSocket</a></li>
<li><a href="https://github.com/xojo/Paw-XojoURLConnectionGenerator">Paw Extension for URLConnection</a></li>
</ul>
<p><strong>CONCLUSION:</strong></p>
<p>Paw is an excellent tool for storing and managing API configurations and examples, plus adding the ability to copy and paste Xojo HTTP socket code makes it an even better part of your library if you work with a lot of HTTP RESTful APIs.<!-- end HubSpot Call-to-Action Code --></p>
<hr />
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
