Web Services and APIs

So, far we’ve seen that HTTP can be used to request content like what’s in a webpage; images, text, video. But for the apps that we’ll be writing, we’ll use HTTP in a slightly different way. Instead of using URLs like Udacity.com, we’ll you special URLs that correspond to methods like getimageData that are made available by Web services. Web services sometimes they’re also referred to as APIs but don’t get confused, they mean the same thing. I want to take a look at a quick example from the Flickr API. We’re going to look at flickr.people.getPublicPhotos. This method returns some kind of image data back to our client or an app. We’re going to look at Flickr’s documentation for this method now. I posted a link in the instructor notes. So, this is the documentation for getPublicPhotos and there’s something that I want to draw to your attention first and that is the use of arguments. So, the arguments listed here are a lot like the arguments that you’d see for a swift function call and you’ll also notice that some of them are required like api_key while others are not like safe_search. Now, this is really cool here at the bottom. This is the API Explorer. Not all APIs have this but Flickr does and it’s extremely useful. The API Explorer allows us to test the APIs methods before using them in our apps. For this method, it shows the only required argument is the user ID. This is only a half truth. The API key is also required but in the API Explorer it automatically includes a test API key for us. For the user ID argument, I’ll go ahead and use my user ID that I’ve copied ahead of time, and then under the extras argument I’ll type url_m. If you read the documentation, the url_m option specifies that we want the result of this method to include URLs for medium images. You’ll see that in just a second. And last, we want to change the output type to JSON or JavaScript Object Notation which we’ll talk about more later in this lesson. And let’s call the method, and after it’s called we can see the results below. In the results we see this information about photos and then within photos it looks like there’s a group of individual photos. Judging by the total value we see of 13, it appears there’s probably 13 total photos, and for each photo we can see this owner ID which is the same as my user ID and we also see an ID for the image. If we keep scrolling to the right there’s more values and here is the url_m value that we specified in the extras argument. If we copy one of these, we can paste it into the browser to see the image. The URL seems to be not working and I think it’s because these extra slash characters that have been added before each of the forward slashes. So, let me remove these and I’m removing them manually now but normally these slash characters will get removed when we create an NSURL and code, and let’s try this. Nice. This is a picture of Eloise. This is my wife’s cat and I’d actually didn’t really like Eloise very much at first but she’s kind of grown in me over time. Back in the API Explorer let’s recap on what’s happening. The results being returned by this getPublicPhotos method is this JSON data representing the photos and my public photo stream and they’re being returned as JSON because that’s what we specified here in the output. If we scroll all the way at the bottom we see this really long and ugly URL. I’ll copy this and paste it back into our browser. If we visit this link, we get the exact same thing that we saw on the API Explorer. In fact, the URL that we’re using here is extremely similar to the URL or App we’ll use receive data from Flickr. Now if we look more closely at the URL itself we can see the arguments that we passed in. We didn’t pass in the method argument that was implicit in the Explorer but if we keep scrolling here to the right past the API key, we can see my user ID here and then we can see the extras argument and the url_m. There are also these ampersands that seem to separate the different argument values. So, there’s one there. There’s one there. You can see them continuing on. Now the ampersands themselves are part of a standard for how arguments are sent in a URL. Speaking of standards, I should note that URLs can only be sent over the Internet using the ASCII character set. The ASCII character set is a simple way of representing characters on a computer where character values can also be represented using a number. Here’s some example ASCII characters and their corresponding values. I’ve posted a link in the instructor notes if you want to read more about them. In some cases, we may want to use a URL that contains characters that are considered unsafe ASCII characters such as the plus sign. When this happens, we have to encode or escape these unsafe characters into valid ASCII characters in the URL. The escape version of a plus sign is percent 2B. In each of those characters the percent sign, the two and the B are valid, safe ASCII characters. Here’s another example. This is a URL that uses Flickr’s flickr.photos.search method. One of the arguments and this method is called text which is a string representing a text search. In this URL, the text is equal to red percent 2B blue. This is the escaped or encoded version of the actual text which is red plus blue. Since the plus sign is considered unsafe ASCII, it’s escaped with a character’s percent 2B.

Leave a Reply

Your email address will not be published. Required fields are marked *