export default class MainApi {
  private _url: string;
  constructor(url: string) {
    this._url = url;
  }

  _checkStatus(promise: any) {
    return promise.then((res: any) => {
      if (res.ok) {
        return res.json();
      }
      return Promise.reject(`Ошибка ${res.status}`);
    });
  }

  getRC() {
    const promise = fetch(`${this._url}/title/get`, {
      method: "GET",
    });
    return this._checkStatus(promise);
  }

  createSession(name: any) {
    const promise = fetch(
      `${this._url}/session/create?` + new URLSearchParams({ title: name }),
      {
        method: "GET",
      }
    );
    return this._checkStatus(promise);
  }

  connectSession(code: any) {
    const promise = fetch(
      `${this._url}/session/connect?` +
        new URLSearchParams({ connection_code: code }),
      {
        method: "GET",
      }
    );
    return this._checkStatus(promise);
  }
}
