<commit_before>###
See for more information
http://arcturo.github.io/library/coffeescript/07_the_bad_parts.html
###
"use strict"

Promise = require 'bluebird'
request = require 'superagent'
localStorage = require 'localStorage'

class ApiError extends Error
    # bad request url
    @url = null 
    # request method
    @method = null
    # request response code
    @status = null
    # message of error, taken from meta or error message
    @message = null
    # data content from response body
    @data = null

standartize = (req, token = {}, query = {}) ->
    req = req
        .accept 'application/json'
        .type 'application/json'
    req = req.set("Authorization", "Token #{token}") if token?
    req

handler = (resolve, reject) ->
    (res) ->
        body = res.body
        if res.ok is yes
            resolve body
        else
            error = new ApiError()
            error.message = res.error.message
            if 'message' of body.meta
                error.message = body.meta.message
            error.status = res.status
            error.url = res.req.url
            error.data = body.data
            reject error

module.exports = {
    token: localStorage.getItem('token'),

    get: (url, query = {}) ->
        token = @token
        new Promise (resolve, reject) ->
            standartize request.get(url), token, query
                .end handler resolve, reject

    post: (url, data = {}, query = {}) ->
        token = @token
        new Promise (resolve, reject) ->
            standartize request.post(url), token, query
                .send data
                .end handler resolve, reject

    put: (url, data = {}, query = {}) ->
        token = @token
        new Promise (resolve, reject) ->
            standartize request.put(url), token, query
                .send data
                .end handler resolve, reject

    delete: (url, query = {}) ->
        token = @token
        new Promise (resolve, reject) ->
            standartize request.delete(url), token, query
                .end handler resolve, reject
}<commit_msg>Fetcher: fixing delete method by fixing name of the attribute for super agent<commit_after>###
See for more information
http://arcturo.github.io/library/coffeescript/07_the_bad_parts.html
###
"use strict"

Promise = require 'bluebird'
request = require 'superagent'
localStorage = require 'localStorage'

class ApiError extends Error
    # bad request url
    @url = null 
    # request method
    @method = null
    # request response code
    @status = null
    # message of error, taken from meta or error message
    @message = null
    # data content from response body
    @data = null

standartize = (req, token = {}, query = {}) ->
    req = req
        .accept 'application/json'
        .type 'application/json'
    req = req.set("Authorization", "Token #{token}") if token?
    req

handler = (resolve, reject) ->
    (res) ->
        body = res.body
        if res.ok is yes
            resolve body
        else
            error = new ApiError()
            error.message = res.error.message
            if 'message' of body.meta
                error.message = body.meta.message
            error.status = res.status
            error.url = res.req.url
            error.data = body.data
            reject error

module.exports = {
    token: localStorage.getItem('token'),

    get: (url, query = {}) ->
        token = @token
        new Promise (resolve, reject) ->
            standartize request.get(url), token, query
                .end handler resolve, reject

    post: (url, data = {}, query = {}) ->
        token = @token
        new Promise (resolve, reject) ->
            standartize request.post(url), token, query
                .send data
                .end handler resolve, reject

    put: (url, data = {}, query = {}) ->
        token = @token
        new Promise (resolve, reject) ->
            standartize request.put(url), token, query
                .send data
                .end handler resolve, reject

    delete: (url, query = {}) ->
        token = @token
        new Promise (resolve, reject) ->
            standartize request.del(url), token, query
                .end handler resolve, reject
}
{'fraction_non_alphanumeric': 0.1086495404195145, 'fraction_numerical': 0.0009427292010370022, 'mean_word_length': 1.8617666891436278, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '504299', 'n_tokens_mistral': 1145, 'n_tokens_neox': 1138, 'n_words': 370}