Skip to content
Snippets Groups Projects
requests.js 4.15 KiB
Newer Older
  • Learn to ignore specific revisions
  • Eugen Ciur's avatar
    Eugen Ciur committed
    import Service from '@ember/service';
    
    Eugen Ciur's avatar
    Eugen Ciur committed
    // eslint-disable-next-line ember/no-computed-properties-in-native-classes
    
    Eugen Ciur's avatar
    Eugen Ciur committed
    import { computed } from '@ember/object';
    
    Eugen Ciur's avatar
    Eugen Ciur committed
    import { service } from '@ember/service';
    
    import {
      insert_blob,
      extract_file_name
    } from 'papermerge/utils';
    
    Eugen Ciur's avatar
    Eugen Ciur committed
    
    
    import { base_url } from 'papermerge/utils/host';
    
    
    Eugen Ciur's avatar
    Eugen Ciur committed
    
    export default class Requests extends Service {
      @service session;
    
      @service store;
    
    Eugen Ciur's avatar
    Eugen Ciur committed
    
    
    Eugen Ciur's avatar
    Eugen Ciur committed
      async runOCR({doc_id, lang}) {
        /*
          Request sent with ContentType: application/json
        */
        let url, headers_copy = {};
    
    Eugen Ciur's avatar
    Eugen Ciur committed
    
    
    Eugen Ciur's avatar
    Eugen Ciur committed
        url = `${base_url()}/ocr/`;
    
    Eugen Ciur's avatar
    Eugen Ciur committed
    
    
    Eugen Ciur's avatar
    Eugen Ciur committed
        Object.assign(headers_copy, this.headers);  // create a copy of `this.headers`
        headers_copy['Content-Type'] = 'application/json';
        headers_copy['Accept'] = 'application/json';
    
    
    Eugen Ciur's avatar
    Eugen Ciur committed
        return fetch(url, {
          method: 'POST',
    
    Eugen Ciur's avatar
    Eugen Ciur committed
          headers: headers_copy,
          body: JSON.stringify({doc_id, lang})
    
    Eugen Ciur's avatar
    Eugen Ciur committed
        });
      }
    
    
      /**
      *  `document_version` contains following attributes:
      *    id
      *    number
      *    file_name
      *    lang
      *    pages
      *    size
      *    page_count
      *    short_description
      *
      *  attributes which correspond to server side (or client side) DocumentVersion model
      */
    
    Eugen Ciur's avatar
    Eugen Ciur committed
      async downloadDocumentVersion(document_version) {
    
        let response, blob;
    
        response = await this._get(`/document-versions/${document_version.id}/download/`);
    
        blob = await response.blob();
        insert_blob(
          document_version.file_name,
          blob
        );
      }
    
      async downloadNodes(selected_nodes) {
        let params_arr,
          params_str,
          response,
          blob,
          file_name;
    
        params_arr = selected_nodes.map(node => `node_ids=${node.id}`);
        params_str = params_arr.join('&');
    
        response = await this._get('/nodes/download/', params_str);
    
        file_name = extract_file_name(response, 'fallback.zip');
        blob = await response.blob();
    
        insert_blob(file_name, blob);
    
      async nodesMove(data) {
        let url, headers_copy = {};
    
    
    Eugen Ciur's avatar
    Eugen Ciur committed
        url = `${base_url()}/nodes/move/`;
    
    
        Object.assign(headers_copy, this.headers);  // create a copy of `this.headers`
        headers_copy['Content-Type'] = 'application/json';
    
        return fetch(url, {
          method: 'POST',
          headers: headers_copy,
          body: JSON.stringify(data)
        });
      }
    
    
    Eugen Ciur's avatar
    Eugen Ciur committed
      async search(query) {
    
        return this._get('/search/', `q=${query}`);
    
    Eugen Ciur's avatar
    Eugen Ciur committed
      }
    
      async preferences({section_name}={}) {
    
        let params = {};
    
    Eugen Ciur's avatar
    Eugen Ciur committed
    
        if (section_name) {
    
          params = {'section': section_name};
    
    Eugen Ciur's avatar
    Eugen Ciur committed
        }
    
    
        return this._get(
          '/preferences/',
          new URLSearchParams(params).toString()
        );
    
    Eugen Ciur's avatar
    Eugen Ciur committed
      async preferencesUpdate(data) {
    
        let url,
          headers_copy = {},
          response,
          that = this;
    
    Eugen Ciur's avatar
    Eugen Ciur committed
    
        url = `${base_url()}/preferences/bulk/`;
    
        Object.assign(headers_copy, this.headers);  // create a copy of `this.headers`
        headers_copy['Content-Type'] = 'application/json';
    
    
        response = fetch(url, {
    
    Eugen Ciur's avatar
    Eugen Ciur committed
          method: 'POST',
          headers: headers_copy,
          body: JSON.stringify(data)
        });
    
    
        response.then(response => response.json()).then(
          list_of_attrs => {
            list_of_attrs.data.map(item => {
              that.store.push({data: {
                id: item.id,
                type: "preferences",
                attributes: {
                  name: item.name,
                  value: item.value,
                  section: item.section,
                  identifier: item.identifier
                }
              }});
            });
        });
    
    Eugen Ciur's avatar
    Eugen Ciur committed
      async getInboxCount() {
        return this._get('/nodes/inboxcount/').then(
          response => response.json()
        ).then( data => {
          return data.data.count;
        });
      }
    
    
      async _get(url, params_str) {
        let url_with_base,
          headers_copy = {};
    
        if (params_str) {
         url_with_base = `${base_url()}${url}?${params_str}`;
        } else {
          url_with_base = `${base_url()}${url}`;
        }
        Object.assign(headers_copy, this.headers);
    
        return fetch(url_with_base, {
          method: 'GET',
          headers: headers_copy,
        });
      }
    
    
    Eugen Ciur's avatar
    Eugen Ciur committed
      @computed('session.{data.authenticated.token,isAuthenticated}')
    
    Eugen Ciur's avatar
    Eugen Ciur committed
      get headers() {
        let _headers = {},
          token;
    
        if (this.session.isAuthenticated) {
          token = this.session.data.authenticated.token;
          _headers['Authorization'] = `Token ${token}`;
        }
    
        return _headers;
      }
    
    }