Programowanie JavaScript 09
Zaliczenie? (propozycja)
Weryfikacja:
- pytania teoretyczne
- projekt wyslany na UPLOADER (dwa(2) dni przed zaliczeniem) - archiwum bez
.node_modules/.git - termin: data_T0 - 1.day
- termin: data_T1 - 1.day
- ocena/oddawanie projeków wg. zgłoszenia (FCFS)
Zgloszenia T0/T1
Adam (nie przyszedł)
Piotrek
Robert
Piotrek
Robert
Adam (ten co nie przyszedł idzie na koniec ;)
T0 i T1 mają rozdzielne zgłoszenia/listy
HTTP
Client
- curl
- google chrome
- firefox
- axios
- custom
Server
- apache2
- nginx
- caddy
- custom
Client ( Wrapper ) GitHub
Aplikacja (skrypt - nasz klient) komunikujący się z zewnętrzmym API usługi (GitHub - nasz serwer) z użyciem protokołu HTTP/HTTPS. Klient powinien realizować
- dodawanie gist-ów
- edycje gist-ów
- usuwanie gist-ów
- listing gist-ów
- filtrowanie gist-ów
Sample
const axios = require('axios');
class GithHubWrapper {
constructor(token) {
this.token = token
this.client = axios.create({
baseURL: 'https://api.github.com/',
responseType: 'json',
headers: {
'X-Custom-Header': this.token,
'Accept': 'application/vnd.github.v3+json',
'Authorization': 'token ' + this.token
}
})
}
getRequest(path) {
return this.client.get(path)
}
postRequest(path, payload) {
return this.client.post(path, payload)
}
root() {
return this.getRequest('/')
}
createGist(payload) {
return this.postRequest('/gists', payload)
}
getGist(gistId) {
return this.getRequest(`/gists/${gistId}`)
}
}
let token = "< < GITHUB TOKEN GOES HERE > >"
let ghWrapper = new GithHubWrapper(token)
let gistPayload = {
"description": "Hello World Examples",
"public": true,
"files": {
"hello_world.rb": {
"content": "class HelloWorld\n def initialize(name)\n @name = name.capitalize\n end\n def sayHi\n puts \"Hello !\"\n end\nend\n\nhello = HelloWorld.new(\"World\")\nhello.sayHi"
},
"hello_world.py": {
"content": "class HelloWorld:\n\n def __init__(self, name):\n self.name = name.capitalize()\n \n def sayHi(self):\n print \"Hello \" + self.name + \"!\"\n\nhello = HelloWorld(\"world\")\nhello.sayHi()"
},
"hello_world_ruby.txt": {
"content": "Run `ruby hello_world.rb` to print Hello World"
},
"hello_world_python.txt": {
"content": "Run `python hello_world.py` to print Hello World"
}
}
}
ghWrapper.root().then((response) => console.log(response.data))
ghWrapper.getGist('<< GIST ID>>').then((response) => console.log(response.data))
ghWrapper.createGist(gistPayload).then((response) => console.log(response.data))
Cwiczenie
- Dokumentacja
- Wrapper może tworzyć gist.
- Wrapper może czytać gist.
- Wrapper możę edytować gist.