So Wikipedia has a pretty neat API that allows you to grab information and images from the Wikipedia 1
site. 2
So if you head over here mediawiki.org, then you can actually see a description of how the Wikipedia API works. 3
And they give you a simple example of how you can make a GET request looking for this exact page,API 4
main page. 5
And it also shows you what is the endpoint and all of the various parameters. 6
So what we want to do is to pull back data about the Barberton Daisy. And in order to do that, we have 7
to construct our API call. 8
So let's have a look at this you URL, so that we can break down all of its various parameters. 9
So this is the base URL and this is provided over here, as well as you can see. 10
This is the same endpoint and this goes to the English version of Wikipedia. 11
So after that php is where all of our parameters come in, and the parameters are preceded with a question 12
mark, and then we start listing the keys and the values. 13
So the first key is what is the format of the data you want to receive and we want it in the form of 14
a JSON. The next parameter is what type of action are you trying to perform. 15
Were trying to perform a query. We're looking for something specific. 16
And then there's this part of the code which specifies that we want the extract and just the intro explain 17
text, not the whole article so that we can cut down the amount of data that were transferring. Some Wikipedia 18
articles can go on for reams and reams and reams. 19
We don't want to import all of that data. All we want is just that top explainer text that you always 20
get at the beginning of every single Wikipedia article. 21
And the last parameter is this thing called titles and this is the thing that we're going to search 22
for. 23
And in my case, it's the Barberton Daisy. 24
You might wonder what is that percentage mark 20. 25
Well, if you delete it and replace it with a space, and hit enter, 26
you'll see this is actually the ASCII code for a space. 27
So when we hit enter with all of that, we get a JSON back. 28
Let's visualize this inside a JSON editor. 29
This will make it a lot easier to see for our human eyes. 30
So if I expand all of these keys, you can see that it's a tree structure, and we have to go quite a few 31
levels deep before we actually get the information that we want which is this extract. 32
Now, if you remember our lesson on Jason's then you'll realize that if our result was called results, 33
then in order to get access to this extract, we have to specify all of the keys that led us there. 34
So everything inside the object is the JSON. 35
So the first level is the query, right? 36
So result, then it will be query. Then the next level after query will be pages. 37
So we'll write pages, 38
right?? 39
But then we're kind of stuck. 40
What is this number? In order to get any further within pages, 41
we have to specify this number. And this number is the page ID 42
as you can see here. But we wouldn't know the page ID when we're searching for this particular object. 43
So how can we go any further? It seems like we're a bit stuck, right? 44
Well, what we can actually do is to add another parameter. 45
And this is called index page IDs. 46
And I found this from the documentation. 47
So, now the JSON that we get back actually contains a reference to that page ID. 48
So you can see that it's still query, but now you've got pageids, and you've got pages. 49
So we can, first, work out the page ID by saying result ["query"] [pageids] first object, and that is going 50
to give us this 102158-- 51
So then we can install this inside a constant, say, code pageid, and then we can use this pageid here. 52
So then that allows us to go further down and we can go into the extract. 53
And the result of this JSON passing will equal this extract here which we'll be able to display inside 54
a label. 55
So that's just a little work around around this particular structure of Wikipedia's API call. 56
Now, there's just one more problem with our http request. 57
You can see that here it says that this is a redirect from a vernacular name to the scientific 58
name. And you can see that the extract doesn't actually give us what we wanted which is the information 59
about this particular flower. 60
Instead, it's telling us that this term is a vernacular term 61
and the article is actually under the scientific name. So we can modify our URL to make that redirect 62
for us. 63
So to do that, we're going to add yet another parameter, 64
and this is called redirect and it's going to equal 1 which stands for true. 65
And if you hit enter, 66
now if you have a look at that extract of ours, you can see it's actually taking us to the information 67
that we want. 68
So inside this lesson, we provided a text file for you to download. And this contains everything that 69
you need in order to make that API call. 70
So we've got the base URL for you. 71
So this is the English version of Wikipedia and this is the URL that you're going to build 72
on top of in order to access the data that we've got here. 73
We've also created a array of dictionary items that will form as the parameters for your HTTP request. 74
So we've included everything that's in this URL including the format, the action, the properties, 75
the titles, the index page IDs, as well as that redirect equals true. 76
So now it's time for your challenge. 77
Can you use the information that you learned in Clima and in FlashChat to format a HTTP request using 78
Alamofire passing it the parameters and the URL, and you should be able to print this entire JSON 79
into your debug area. 80
And if in your console, you can see exactly what I see here on screen, then you would have succeeded. 81
So go ahead give it a try. And if you really get stuck, I'll be there in the next lesson to give you the 82
solution.