COAP:COAP-3110/week7: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
(Created page with "<categorytree mode="pages" depth="1" style="float:right; clear:right; margin-left:3px; border:1px solid gray; padding:0.7ex; background-color:#f9f9f9;">COAP 3110</categorytree...")
 
mNo edit summary
Line 9: Line 9:


... details, TBA
... details, TBA
== Monday ==
=== A first glance at REST ===
According to [https://en.wikipedia.org/wiki/Web_service Wikipedia], {{quotation|A Web service is a service offered by an electronic device to another electronic device, communicating with each other via the World Wide Web. In a Web service, Web technology such as HTTP, originally designed for human-to-machine communication, is utilized for machine-to-machine communication, more specifically for transferring machine readable file formats such as XML and JSON. In practice, the Web service typically provides an object-oriented Web-based interface to a database server, utilized for example by another Web server, or by a mobile application, that provides a user interface to the end user. Another common application offered to the end user may be a mashup, where a Web server consumes several Web services at different machines, and compiles the content into one user interface.}}
There exist several types of web services, e.g. the more traditional stack built around [https://en.wikipedia.org/wiki/SOAP SOAP].
Currently, REST is the most popular mechanism used by more light-weight portals and more generally speaking all sorts of services that deal with information, as opposed to commercial transactions for example.
=== Hands on wordpress ===
By default, WordPress does not have a REST API, so we will have to install a plugin
(1) Please install the Wordpress plugin:
* [http://v2.wp-api.org/ WordPress REST API] (V2)
(2) Get a list of posts from one of your colleagues web site.
Have a look at:
* http://v2.wp-api.org/reference/posts/
Do it:
* http://yoursite.com/wp-json/wp/v2/posts
* https://wtfismyip.com/ (will give you the IP number of your PC)
=== Hands on data from a MediaWiki ===
(1)  Retrieve all COAP 3110 pages from edutechwiki
Have a look at:
* http://edutechwiki.unige.ch/mediawiki/api.php
* http://edutechwiki.unige.ch/mediawiki/api.php?action=help&modules=query
* http://edutechwiki.unige.ch/mediawiki/api.php?action=query&list=categorymembers&cmtitle=Category:COAP_2170
(2) Example that returns JSON code for COAP 3110
* http://edutechwiki.unige.ch/mediawiki/api.php?action=query&list=categorymembers&cmtitle=Category:COAP_3110&format=json
Parameters
* action=query - will tell the API to do a query
* list=categorymembers - returns a list of members of a category
* cmtitle=  - defines the Category to look for
* format=... - defines the format to return.
Resulting datastructure (example)
<source lang="json">
{"batchcomplete" : "",
"query" : {"categorymembers":[
                              {"pageid":9883,"ns":106,"title":"COAP:COAP-3110"},
                              {"pageid":9902,"ns":106,"title":"COAP:COAP-3110/week1"},
                              {"pageid":10230,"ns":106,"title":"COAP:COAP-3110/week2"},
                              {"pageid":10232,"ns":106,"title":"COAP:COAP-3110/week3"},
                              {"pageid":10278,"ns":106,"title":"COAP:COAP-3110/week4"},
                              {"pageid":10279,"ns":106,"title":"COAP:COAP-3110/week5"},
                              {"pageid":10280,"ns":106,"title":"COAP:COAP-3110/week6"},
                              {"pageid":10293,"ns":106,"title":"COAP:COAP-3110/week7"},
                              {"pageid":10292,"ns":106,"title":"COAP:COAP-3110/week8"}
          ]}
}
</source>
By changing some parameters, you could simplify the results returned. You only need the title for the page !
(3) Get category pages from from Wikipedia, i.e. complete the URL below
* https://en.wikipedia.org/w/api.php?action=query&........
Idea: WP has a complex category system for games, e.g. look at [https://en.wikipedia.org/wiki/Category:Video_game_genres Video Game genres] or [https://en.wikipedia.org/wiki/Category:Video_games_by_genre Video games by genre]. Pick a sub sub category and list the pages, e.g. [https://en.wikipedia.org/wiki/Category:Civilization_(series) the Civilization series].
* https://en.wikipedia.org/w/api.php?action=query&list=categorymembers&format=json&cmtitle=Category:Civilization_(series)
You will have to adapt the query if a category includes more than 10 entries.
* Read https://en.wikipedia.org/w/api.php?action=help&modules=query%2Bcategorymembers
=== Doing it with a PHP script ===
How would you display information obtained from a REST call with PHP ? Do you think that there are tools that might help ?
=== Doing it with JavaScript ===
You can do REST calls in JavaScript. However, the browser will require that the JavaScript page sits on the same server or that you use a more complex procedure like CORS headings.
'''Works''' (HTML file sits on the same server as EduTechWiki from which we get the data)
* http://edutechwiki.unige.ch/api-en/rest-example.html
'''Does not work:'''
* file:///web/guides/js/ex/rest/rest-example.html
* http://tecfa.unige.ch/guides/js/ex/rest/rest-example.html
Firefox (49,Window), for example shows the following error:
<source lang="JavaScript">
: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://edutechwiki.unige.ch/mediawiki/api.php?action=query&list=categorymembers&format=json&cmprop=title&cmtype=page&cmlimit=500&cmtitle=Category:COAP_3110. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).(unknown)
HELLOrest-example.html:45:3
Object { readyState: 0, getResponseHeader: .ajax/v.getResponseHeader(), getAllResponseHeaders: .ajax/v.getAllResponseHeaders(), setRequestHeader: .ajax/v.setRequestHeader(), overrideMimeType: .ajax/v.overrideMimeType(), statusCode: .ajax/v.statusCode(), abort: .ajax/v.abort(), state: .Deferred/d.state(), always: .Deferred/d.always(), then: .Deferred/d.then(), 11 more… }rest-example.html:48:17
rest-example.html:49:17
</source>
== Homework 5 ==
* Retrieve and display information from a wordpress site or another site
* Details TBA.

Revision as of 19:10, 28 September 2016

Learning goals

  • Using Webservices (REST)
  • Term project report writing

... details, TBA

Monday

A first glance at REST

According to Wikipedia, “A Web service is a service offered by an electronic device to another electronic device, communicating with each other via the World Wide Web. In a Web service, Web technology such as HTTP, originally designed for human-to-machine communication, is utilized for machine-to-machine communication, more specifically for transferring machine readable file formats such as XML and JSON. In practice, the Web service typically provides an object-oriented Web-based interface to a database server, utilized for example by another Web server, or by a mobile application, that provides a user interface to the end user. Another common application offered to the end user may be a mashup, where a Web server consumes several Web services at different machines, and compiles the content into one user interface.”

There exist several types of web services, e.g. the more traditional stack built around SOAP.

Currently, REST is the most popular mechanism used by more light-weight portals and more generally speaking all sorts of services that deal with information, as opposed to commercial transactions for example.

Hands on wordpress

By default, WordPress does not have a REST API, so we will have to install a plugin

(1) Please install the Wordpress plugin:

(2) Get a list of posts from one of your colleagues web site.

Have a look at:

Do it:

Hands on data from a MediaWiki

(1) Retrieve all COAP 3110 pages from edutechwiki

Have a look at:

(2) Example that returns JSON code for COAP 3110

Parameters

  • action=query - will tell the API to do a query
  • list=categorymembers - returns a list of members of a category
  • cmtitle= - defines the Category to look for
  • format=... - defines the format to return.

Resulting datastructure (example)

{"batchcomplete" : "",
 "query" : {"categorymembers":[
                              {"pageid":9883,"ns":106,"title":"COAP:COAP-3110"},
                              {"pageid":9902,"ns":106,"title":"COAP:COAP-3110/week1"},
                              {"pageid":10230,"ns":106,"title":"COAP:COAP-3110/week2"},
                              {"pageid":10232,"ns":106,"title":"COAP:COAP-3110/week3"},
                              {"pageid":10278,"ns":106,"title":"COAP:COAP-3110/week4"},
                              {"pageid":10279,"ns":106,"title":"COAP:COAP-3110/week5"},
                              {"pageid":10280,"ns":106,"title":"COAP:COAP-3110/week6"},
                              {"pageid":10293,"ns":106,"title":"COAP:COAP-3110/week7"},
                              {"pageid":10292,"ns":106,"title":"COAP:COAP-3110/week8"}
           ]}
}

By changing some parameters, you could simplify the results returned. You only need the title for the page !

(3) Get category pages from from Wikipedia, i.e. complete the URL below


Idea: WP has a complex category system for games, e.g. look at Video Game genres or Video games by genre. Pick a sub sub category and list the pages, e.g. the Civilization series.

You will have to adapt the query if a category includes more than 10 entries.

Doing it with a PHP script

How would you display information obtained from a REST call with PHP ? Do you think that there are tools that might help ?

Doing it with JavaScript

You can do REST calls in JavaScript. However, the browser will require that the JavaScript page sits on the same server or that you use a more complex procedure like CORS headings.

Works (HTML file sits on the same server as EduTechWiki from which we get the data)

Does not work:

Firefox (49,Window), for example shows the following error:

: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://edutechwiki.unige.ch/mediawiki/api.php?action=query&list=categorymembers&format=json&cmprop=title&cmtype=page&cmlimit=500&cmtitle=Category:COAP_3110. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).(unknown)
HELLOrest-example.html:45:3
Object { readyState: 0, getResponseHeader: .ajax/v.getResponseHeader(), getAllResponseHeaders: .ajax/v.getAllResponseHeaders(), setRequestHeader: .ajax/v.setRequestHeader(), overrideMimeType: .ajax/v.overrideMimeType(), statusCode: .ajax/v.statusCode(), abort: .ajax/v.abort(), state: .Deferred/d.state(), always: .Deferred/d.always(), then: .Deferred/d.then(), 11 more }rest-example.html:48:17
rest-example.html:49:17

Homework 5

  • Retrieve and display information from a wordpress site or another site
  • Details TBA.