client/src/APIClient.js
author duong tam kien <tk@deveha.com>
Mon, 19 Jun 2017 12:39:00 +0200
changeset 48 1105f23aa0de
parent 44 3b20e2b584fe
child 49 39f69dc1c2d2
permissions -rw-r--r--
iconography
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
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    24
  createAuthorizedRequest = (method, uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    25
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    26
    var headers = new Headers(),
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    27
        token = this.storage.get('token') || '';
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    28
    headers.append("Authorization", "Bearer " + token);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    29
    headers.append("Content-Type", "application/json");
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    30
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    31
    return this.createRequest(method, uri, data, headers);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    32
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    33
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    34
  request = (method, uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    35
    console.log(method + ' ' + uri);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    36
    var req = this.model ? this.createAuthorizedRequest(method, uri, data) : this.createRequest(method, uri, data);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    37
    return this.fetch(req, { credentials: 'include' });
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
  get = (uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    41
    return this.request('GET', uri, data);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    42
  }
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
  post = (uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    45
    return this.request('POST', uri, data);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    46
  }
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
  put = (uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    49
    return this.request('PUT', uri, data);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    50
  }
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
  fetch = (req) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    53
    return new Promise((resolve, reject) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    54
      fetch(req)
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    55
        .then((response) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    56
          if (response.ok) {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    57
            return response.json().then((data) => resolve(data));
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    58
          } else {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    59
            return response.json().then((data) => reject(data));
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    60
          }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    61
        });
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    62
    });
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    63
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    64
}
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
export default APIClient