<commit_before>import axios from 'axios';
import store from '../../store';

const host = '/api';

// Internal help/generic functions
function getCookie(name) {
  let cookieValue = null;
  if (document.cookie && document.cookie !== '') {
    const cookies = document.cookie.split(';');
    for (let i = 0; i < cookies.length; i++) {
      const cookie = cookies[i].trim();
      // Does this cookie string begin with the name we want?
      if (cookie.substring(0, name.length + 1) === (`${name}=`)) {
        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
        break;
      }
    }
  }
  return cookieValue;
}

function buildHeaders(authorize = false, extraHeaders = {}) {
  const token = store.getState().session.token;
  const partnerId = store.getState().session.partnerId;
  const officeId = store.getState().session.officeId;
  let headers = {
    Pragma: 'no-cache',
    'Cache-Control': 'no-cache',
  };
  if (authorize) headers = { ...headers, Authorization: `token ${token}` };
  if (partnerId) headers = { ...headers, 'Partner-ID': partnerId };
  if (officeId) headers = { ...headers, 'Agency-Office-ID': officeId };
  return { ...headers, ...extraHeaders };
}

function get(uri, params = {}, options) {
  const opt = { method: 'GET', params, headers: buildHeaders() };
  return axios.get(`${host}${uri}`, { ...opt, ...options })
    .then(response => response.data);
}

function post(uri, body = {}) {
  const options = {
    headers: buildHeaders(false, { 'X-CSRFToken': getCookie('csrftoken') }),
  };
  return axios.post(`${host}${uri}`, body, options)
    .then(response => response.data);
}

function authorizedGet({ uri, params = {}, options }) {
  const opt = {
    params,
    headers: buildHeaders(true),
  };
  return axios.get(`${host}${uri}`, { ...options, ...opt })
    .then(response => response.data);
}

function authorizedPost({ uri, params, body = {} }) {
  const options = {
    params,
    headers: buildHeaders(true, { 'X-CSRFToken': getCookie('csrftoken') }),
  };
  return axios.post(`${host}${uri}`, body, options)
    .then(response => response.data);
}

function authorizedDelete({ uri, params }) {
  const options = {
    params,
    headers: buildHeaders(true, { 'X-CSRFToken': getCookie('csrftoken') }),
  };
  return axios.delete(`${host}${uri}`, options)
    .then(response => response.data);
}

function authorizedPatch({ uri, params, body = {} }) {
  const options = {
    params,
    headers: buildHeaders(true, { 'X-CSRFToken': getCookie('csrftoken') }),
  };
  return axios.patch(`${host}${uri}`, body, options)
    .then(response => response.data);
}

function authorizedPut({ uri, params, body = {} }) {
  const options = {
    params,
    headers: buildHeaders(true, { 'X-CSRFToken': getCookie('csrftoken') }),
  };
  return axios.put(`${host}${uri}`, body, options)
    .then(response => response.data);
}

function authorizedPostUpload({ uri, body = {}, params }) {
  const options = {
    params,
    headers: buildHeaders(true, {
      'content-type': 'multipart/form-data',
      'X-CSRFToken': getCookie('csrftoken'),
    }),
  };
  return axios.post(`${host}${uri}`, body, options)
    .then(response => response.data);
}


// Accounts
export function postRegistration(body) {
  return post('/accounts/registration', body);
}

export function login(body) {
  return post('/rest-auth/login/', body);
}

export function logout() {
  return post('/rest-auth/logout/');
}

export function getUserData() {
  return authorizedGet({ uri: '/accounts/me/' });
}

// Config
export function getCountries() {
  return get('/config/countries');
}

export function getSectors() {
  return get('/config/sectors');
}

// Project
export function getOpenCfei(filters, options) {
  return authorizedGet({ uri: '/projects/open', params: filters, options });
}

export function getPinnedCfei(filters, options) {
  return authorizedGet({ uri: '/projects/pins', params: filters, options });
}

export function getDirectCfei(filters, options) {
  return authorizedGet({ uri: '/projects/direct', params: filters, options });
}

export function postOpenCfei(body) {
  return authorizedPost({ uri: '/projects/open/', body });
}

export function postDirectCfei(body) {
  return authorizedPost({ uri: '/projects/direct/', body });
}

export function patchCfei(body, id) {
  return authorizedPatch({ uri: `/projects/${id}/`, body });
}

export function convertCnToDirectSelection(body, id) {
  return authorizedPost({ uri: `/projects/application/${id}/convert-unsolicited/`, body });
}

export function deleteConceptNote(projectId) {
  return authorizedDelete({ uri: `/projects/${projectId}/partner-applications-delete/` });
}

export function uploadConceptNote(projectId, body) {
  return authorizedPost({ uri: `/projects/${projectId}/partner-applications/`, body });
}

export function uploadCommonFile(body) {
  return authorizedPostUpload({ uri: '/common/file/', body });
}<commit_msg>add publish endpoint<commit_after>import axios from 'axios';
import store from '../../store';

const host = '/api';

// Internal help/generic functions
function getCookie(name) {
  let cookieValue = null;
  if (document.cookie && document.cookie !== '') {
    const cookies = document.cookie.split(';');
    for (let i = 0; i < cookies.length; i++) {
      const cookie = cookies[i].trim();
      // Does this cookie string begin with the name we want?
      if (cookie.substring(0, name.length + 1) === (`${name}=`)) {
        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
        break;
      }
    }
  }
  return cookieValue;
}

function buildHeaders(authorize = false, extraHeaders = {}) {
  const token = store.getState().session.token;
  const partnerId = store.getState().session.partnerId;
  const officeId = store.getState().session.officeId;
  let headers = {
    Pragma: 'no-cache',
    'Cache-Control': 'no-cache',
  };
  if (authorize) headers = { ...headers, Authorization: `token ${token}` };
  if (partnerId) headers = { ...headers, 'Partner-ID': partnerId };
  if (officeId) headers = { ...headers, 'Agency-Office-ID': officeId };
  return { ...headers, ...extraHeaders };
}

function get(uri, params = {}, options) {
  const opt = { method: 'GET', params, headers: buildHeaders() };
  return axios.get(`${host}${uri}`, { ...opt, ...options })
    .then(response => response.data);
}

function post(uri, body = {}) {
  const options = {
    headers: buildHeaders(false, { 'X-CSRFToken': getCookie('csrftoken') }),
  };
  return axios.post(`${host}${uri}`, body, options)
    .then(response => response.data);
}

function authorizedGet({ uri, params = {}, options }) {
  const opt = {
    params,
    headers: buildHeaders(true),
  };
  return axios.get(`${host}${uri}`, { ...options, ...opt })
    .then(response => response.data);
}

function authorizedPost({ uri, params, body = {} }) {
  const options = {
    params,
    headers: buildHeaders(true, { 'X-CSRFToken': getCookie('csrftoken') }),
  };
  return axios.post(`${host}${uri}`, body, options)
    .then(response => response.data);
}

function authorizedDelete({ uri, params }) {
  const options = {
    params,
    headers: buildHeaders(true, { 'X-CSRFToken': getCookie('csrftoken') }),
  };
  return axios.delete(`${host}${uri}`, options)
    .then(response => response.data);
}

function authorizedPatch({ uri, params, body = {} }) {
  const options = {
    params,
    headers: buildHeaders(true, { 'X-CSRFToken': getCookie('csrftoken') }),
  };
  return axios.patch(`${host}${uri}`, body, options)
    .then(response => response.data);
}

function authorizedPut({ uri, params, body = {} }) {
  const options = {
    params,
    headers: buildHeaders(true, { 'X-CSRFToken': getCookie('csrftoken') }),
  };
  return axios.put(`${host}${uri}`, body, options)
    .then(response => response.data);
}

function authorizedPostUpload({ uri, body = {}, params }) {
  const options = {
    params,
    headers: buildHeaders(true, {
      'content-type': 'multipart/form-data',
      'X-CSRFToken': getCookie('csrftoken'),
    }),
  };
  return axios.post(`${host}${uri}`, body, options)
    .then(response => response.data);
}


// Accounts
export function postRegistration(body) {
  return post('/accounts/registration', body);
}

export function login(body) {
  return post('/rest-auth/login/', body);
}

export function logout() {
  return post('/rest-auth/logout/');
}

export function getUserData() {
  return authorizedGet({ uri: '/accounts/me/' });
}

// Config
export function getCountries() {
  return get('/config/countries');
}

export function getSectors() {
  return get('/config/sectors');
}

// Project
export function getOpenCfei(filters, options) {
  return authorizedGet({ uri: '/projects/open', params: filters, options });
}

export function getPinnedCfei(filters, options) {
  return authorizedGet({ uri: '/projects/pins', params: filters, options });
}

export function getDirectCfei(filters, options) {
  return authorizedGet({ uri: '/projects/direct', params: filters, options });
}

export function postOpenCfei(body) {
  return authorizedPost({ uri: '/projects/open/', body });
}

export function postDirectCfei(body) {
  return authorizedPost({ uri: '/projects/direct/', body });
}

export function patchCfei(body, id) {
  return authorizedPatch({ uri: `/projects/${id}/`, body });
}

export function publishDsr(id) {
  return authorizedPost({ uri: `/projects/${id}/publish/` });
}

export function convertCnToDirectSelection(body, id) {
  return authorizedPost({ uri: `/projects/application/${id}/convert-unsolicited/`, body });
}

export function deleteConceptNote(projectId) {
  return authorizedDelete({ uri: `/projects/${projectId}/partner-applications-delete/` });
}

export function uploadConceptNote(projectId, body) {
  return authorizedPost({ uri: `/projects/${projectId}/partner-applications/`, body });
}

export function uploadCommonFile(body) {
  return authorizedPostUpload({ uri: '/common/file/', body });
}
{'fraction_non_alphanumeric': 0.17813157104235505, 'fraction_numerical': 0.0008010413537598879, 'mean_word_length': 4.0241448692152915, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '794860', 'n_tokens_mistral': 3120, 'n_tokens_neox': 3141, 'n_words': 735}