<commit_before>import Ember from 'ember';
import baseMixin from 'ember-data-offline/mixins/base';

export default Ember.Object.create(baseMixin, {
  init: function(){
    this.set('offlineAdapter', this);
    this.set('onlineAdapter', this);
  },
  get: function(url){
    if (this.get('online')){
      //setup Job for offline after fetch with method
    }
    else{
     //setup Job online....
    }
  },

  _makeOnlineRequest: function(url){

  },

  fetchOffline: function(url){

  }
});<commit_msg>add proposed request api<commit_after>import Ember from 'ember';
import baseMixin from 'ember-data-offline/mixins/base';
import jobMixin from 'ember-data-offline/mixins/job';

export default Ember.Object.createWithMixin(baseMixin, {
  init: function() {
    this.set('offlineAdapter', this);
    this.set('onlineAdapter', this);
  },

  createOfflineJob(url, onlineResp, store) {
    let caller = this;
    let job = Ember.Object.extend(jobMixin).create({
      task() {
        caller.persistOffline(url, onlineResp);
      }
    });
    this.addToQueue(job, store);
  },

  createOnlineJob(url, method, data, store) {
    let caller = this;
    let job = Ember.Object.extend(jobMixin).create({
      task() {
        caller.ajax(url, method, data);
      }
    });
    this.addToQueue(job, store);
  },

  get(url, store) {
    this.make(url, 'get', null, store);
  },

  post(url, data, store) {
    this.make(url, 'post', data, store);
  },

  put(url, data, store) {
    this.make(url, 'put', data, store);
  },

  delete(url, store) {
    this.make(url, 'delete', null, store);
  },

  //fetch stands for online requests and peek for offline ones

  make(url, method, data, store) {
    return Ember.RSVP.resolve().then(() => {
      if (this.get('isOnline')) {
        this.fetch(url, method, data, store);
      } else {
        this.peek(url, method, data, store);
      }
    }).catch(console.log.bind(console));
  },

  fetch(url, method, data, store) {
    let onlineResp = this.ajax(url, method, data);
    this.createOfflineJob(url, onlineResp, store);
    return onlineResp;
  },

  peek(url, method, data) {
    let offlineResp = this.requestOffline(url, method, data);
    this.createOnlineJob(url, method, data, store);
    return offlineResp;
  },

  requestOffline(url, method, data) {
    let _method = method.toLowerCase();
    if (_method === 'get') {
      localstorage.getItem(url);
    } else if (_method === 'post' || _method === 'put' || _method === 'patch') {
      localstorage.setItem(url, data);
    } else if (_method === 'delete') {
      localstorage.removeItem(url);
    }
    return Ember.RSVP.Promise.resolve();
  },

  persistOffline(key, onlineResp) {
    onlineResp.then(payload => {
      localstorage.setItem(key, data);
    });
  },

  _defaultParamsForOnline: Ember.computed({
    get() {
      let session = this.container.lookup('simple-auth-sesion:main');
      let defaults = {
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
      };
      if (session && session.get('isAuthenticated')) {
        defaults.headers = {
          "Authorization": 'UserId: ' + session.get('secure.userId'),
        };
      }
      return defaults;
    }
  }),
  ajax: function(url, method, data) {
    let opts = {
      url: url,
      type: method
    };
    if (data) {
      params.data = data;
    }
    let params = Ember.merge(this._defaultParamsForOnline, opts);
    return Ember.$.ajax(params);
  },
});
{'fraction_non_alphanumeric': 0.15876641918903484, 'fraction_numerical': 0.00028555111364934324, 'mean_word_length': 2.8664459161147904, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2307003', 'n_tokens_mistral': 1179, 'n_tokens_neox': 1147, 'n_words': 249}