client/src/APIClient.js
author Alexandre Segura <mex.zktk@gmail.com>
Mon, 19 Jun 2017 15:36:52 +0200
changeset 52 96f8a4a59bd9
parent 51 08d46c730397
child 53 d8588379529e
permissions -rw-r--r--
Implement logout.
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') || '';
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    34
    headers.append("Authorization", "Bearer " + token);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    35
    headers.append("Content-Type", "application/json");
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    36
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    37
    return this.createRequest(method, uri, data, headers);
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
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    40
  request = (method, uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    41
    console.log(method + ' ' + uri);
49
39f69dc1c2d2 Use token to identify requests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    42
    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
    43
    return this.fetch(req, { credentials: 'include' });
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
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    46
  get = (uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    47
    return this.request('GET', uri, data);
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
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    50
  post = (uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    51
    return this.request('POST', uri, data);
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
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    54
  put = (uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    55
    return this.request('PUT', uri, data);
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
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    58
  fetch = (req) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    59
    return new Promise((resolve, reject) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    60
      fetch(req)
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    61
        .then((response) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    62
          if (response.ok) {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    63
            return response.json().then((data) => resolve(data));
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    64
          } else {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    65
            return response.json().then((data) => reject(data));
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
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    72
export default APIClient