client/src/APIClient.js
author Alexandre Segura <mex.zktk@gmail.com>
Mon, 19 Jun 2017 17:56:41 +0200
changeset 53 d8588379529e
parent 51 08d46c730397
child 56 96543c395baa
permissions -rw-r--r--
Add settings page.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     1
class APIClient {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     2
  constructor(baseURL) {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     3
    this.baseURL = baseURL;
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     4
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     5
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     6
  createRequest = (method, uri, data, headers) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     7
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     8
    headers = headers || new Headers();
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     9
    headers.append("Content-Type", "application/json");
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    10
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    11
    var options = {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    12
      method: method,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    13
      headers: headers,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    14
    };
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    15
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    16
    if (data) {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    17
      options.body = JSON.stringify(data);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    18
    }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    19
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    20
    // TODO : use URL-module to build URL
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    21
    return new Request(this.baseURL + uri, options);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    22
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    23
49
39f69dc1c2d2 Use token to identify requests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    24
  hasToken = () => {
51
08d46c730397 Fix APIClient.
Alexandre Segura <mex.zktk@gmail.com>
parents: 49
diff changeset
    25
    const token = localStorage.getItem('token');
49
39f69dc1c2d2 Use token to identify requests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    26
39f69dc1c2d2 Use token to identify requests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    27
    return token !== null;
39f69dc1c2d2 Use token to identify requests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    28
  }
39f69dc1c2d2 Use token to identify requests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    29
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    30
  createAuthorizedRequest = (method, uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    31
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    32
    var headers = new Headers(),
51
08d46c730397 Fix APIClient.
Alexandre Segura <mex.zktk@gmail.com>
parents: 49
diff changeset
    33
        token = localStorage.getItem('token') || '';
53
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 51
diff changeset
    34
    headers.append("Authorization", "JWT " + token);
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    35
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    36
    return this.createRequest(method, uri, data, headers);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    37
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    38
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    39
  request = (method, uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    40
    console.log(method + ' ' + uri);
49
39f69dc1c2d2 Use token to identify requests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    41
    var req = this.hasToken() ? this.createAuthorizedRequest(method, uri, data) : this.createRequest(method, uri, data);
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    42
    return this.fetch(req, { credentials: 'include' });
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    43
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    45
  get = (uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    46
    return this.request('GET', uri, data);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    47
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    48
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    49
  post = (uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    50
    return this.request('POST', uri, data);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    51
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    52
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    53
  put = (uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    54
    return this.request('PUT', uri, data);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    55
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    56
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    57
  fetch = (req) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    58
    return new Promise((resolve, reject) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    59
      fetch(req)
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    60
        .then((response) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    61
          if (response.ok) {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    62
            return response.json().then((data) => resolve(data));
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    63
          } else {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    64
            return response.json().then((data) => reject(data));
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    65
          }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    66
        });
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    67
    });
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    68
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    69
}
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    70
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    71
export default APIClient