Skip to content

Instantly share code, notes, and snippets.

@jacobdubail
Last active August 10, 2021 20:44
Show Gist options
  • Select an option

  • Save jacobdubail/0773cf4b6ab44055ee45fd51584277cc to your computer and use it in GitHub Desktop.

Select an option

Save jacobdubail/0773cf4b6ab44055ee45fd51584277cc to your computer and use it in GitHub Desktop.

Revisions

  1. jacobdubail revised this gist Aug 10, 2021. 1 changed file with 92 additions and 47 deletions.
    139 changes: 92 additions & 47 deletions js
    Original file line number Diff line number Diff line change
    @@ -1,76 +1,121 @@
    class OrderDeskApiClient {
    constructor(store_id = 0, api_key = "") {
    this.store_id = store_id
    this.api_key = api_key
    this.base_url = "https://app.orderdesk.me/api/v2"
    this.last_status_code = ""
    }

    get(url = '', post = null) {
    return this.go('GET', url, post)
    }
    post(url, post = null) {
    return this.go('POST', url, post)
    }
    put(url, post = null) {
    return this.go('PUT', url, post)
    }
    delete(url, post = null) {
    return this.go('DELETE', url, post)
    }
    async go(method, url, data) {
    const OrderDeskApiClient = {
    storeId: process.env.ORDERDESK_STORE_ID,
    apiKey: process.env.ORDERDESK_API_KEY,
    baseUrl: 'https://app.orderdesk.me/api/v2',
    lastStatusCode: '',

    get: async (url = '', data = null) => {
    return OrderDeskApiClient.go('GET', url, data)
    },
    post: async (url, post = null) => {
    return OrderDeskApiClient.go('POST', url, post)
    },
    put: async (url, post = null) => {
    return OrderDeskApiClient.go('PUT', url, post)
    },
    delete: async (url, post = null) => {
    return OrderDeskApiClient.go('DELETE', url, post)
    },
    go: async (method, url, data) => {
    if (typeof data !== 'object') data = null

    if (!url) throw 'Please enter a destination url'

    url = `${this.base_url}/${url}`
    const headers = this.getHeaders()
    url = `${OrderDeskApiClient.baseUrl}/${url}`
    const headers = OrderDeskApiClient.getHeaders()

    //GET Override
    // GET Override
    if (method == 'GET' && data !== null) {
    url = url + (url.indexOf('?') === -1 ? '?' : '').this.buildQuery(data)
    url =
    url +
    (url.indexOf('?') === -1 ? '?' : '') +
    OrderDeskApiClient.buildQuery(data)
    data = ''
    }

    return await this.fetch(url, method, headers, data)
    .then(async (resp) => {
    return await resp.json()
    return await OrderDeskApiClient.fetch(url, method, headers, data)
    .then(resp => {
    return resp
    })
    .catch((error) => {
    this.last_status_code = error
    return { status: 'error', message: `Receive Error: ${response}` }
    .catch(error => {
    OrderDeskApiClient.lastStatusCode = error
    return {status: 'error', message: `Receive Error: ${error}`}
    })
    }
    },

    async fetch(url, method, headers, data) {
    return await fetch(url, {
    fetch: async (url, method, headers, data) => {
    const response = await fetch(url, {
    method,
    headers,
    body: data ? JSON.stringify(data) : null,
    body: method !== 'GET' ? JSON.stringify(data) : null, // body data type must match "Content-Type" header
    credentials: 'same-origin', // include, *same-origin, omit
    redirect: 'follow', // manual, *follow, error
    })
    }
    return response.json()
    },

    buildQuery(params) {
    buildQuery: params => {
    var searchParams = new URLSearchParams()

    Object.keys(params).forEach(function (name) {
    searchParams.append(name, params[name])
    })

    return searchParams.toString()
    }
    },

    //Get auth headers for this call
    getHeaders() {
    //Get auth headers for OrderDeskApiClient call
    getHeaders: () => {
    return {
    'ORDERDESK-STORE-ID': this.store_id,
    'ORDERDESK-API-KEY': this.api_key,
    'ORDERDESK-STORE-ID': OrderDeskApiClient.storeId,
    'ORDERDESK-API-KEY': OrderDeskApiClient.apiKey,
    'Content-Type': 'application/json',
    }
    }
    },

    // TODO: Implement POST Validation
    //Check Post JSON
    // validatePostedJson() {

    // //No Order
    // if (!isset($_POST['order'])) {
    // header(':', true, 400);
    // die('No Data Found');
    // }

    // //Check Store ID
    // if (!isset($_SERVER['HTTP_X_ORDER_DESK_STOREID']) || $_SERVER['HTTP_X_ORDER_DESK_STOREID'] != $OrderDeskApiClient->storeId) {
    // header(':', true, 403);
    // die('Unauthorized Store Request');
    // }

    // //Check Order Data
    // $order = json_decode($_POST['order'], 1);
    // if (!is_array($order)) {
    // header(':', true, 400);
    // die('Invalid Order Data');
    // }

    // //Check the Hash
    // $hash = hash_hmac('sha256', $_POST['order'], $OrderDeskApiClient->apiKey);
    // //echo $hash . "\n" . $_SERVER['HTTP_X_ORDER_DESK_HASH'] . "\n";
    // if (!isset($_SERVER['HTTP_X_ORDER_DESK_HASH']) || $hash != $_SERVER['HTTP_X_ORDER_DESK_HASH']) {
    // header(':', true, 403);
    // die('Unauthorized Hash Request');
    // }
    // }

    // get storeId() {
    // return OrderDeskApiClient.storeId;
    // }

    // get apiKey() {
    // return OrderDeskApiClient.apiKey;
    // }

    last_state_code: () => {
    return OrderDeskApiClient.lastStatusCode
    },
    }

    get lastStatusCode() {
    return this.last_status_code
    }
    }
    export {OrderDeskApiClient}
  2. jacobdubail revised this gist Dec 5, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions js
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,10 @@
    class OrderDeskApiClient {
    constructor(store_id = 0, api_key = "") {
    this.store_id = store_id
    this.store_id = store_id
    this.api_key = api_key
    this.base_url = "https://app.orderdesk.me/api/v2"
    this.last_status_code = ""
    }
    }

    get(url = '', post = null) {
    return this.go('GET', url, post)
  3. jacobdubail revised this gist Dec 5, 2020. No changes.
  4. jacobdubail created this gist Dec 5, 2020.
    76 changes: 76 additions & 0 deletions js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    class OrderDeskApiClient {
    constructor(store_id = 0, api_key = "") {
    this.store_id = store_id
    this.api_key = api_key
    this.base_url = "https://app.orderdesk.me/api/v2"
    this.last_status_code = ""
    }

    get(url = '', post = null) {
    return this.go('GET', url, post)
    }
    post(url, post = null) {
    return this.go('POST', url, post)
    }
    put(url, post = null) {
    return this.go('PUT', url, post)
    }
    delete(url, post = null) {
    return this.go('DELETE', url, post)
    }
    async go(method, url, data) {
    if (typeof data !== 'object') data = null

    if (!url) throw 'Please enter a destination url'

    url = `${this.base_url}/${url}`
    const headers = this.getHeaders()

    //GET Override
    if (method == 'GET' && data !== null) {
    url = url + (url.indexOf('?') === -1 ? '?' : '').this.buildQuery(data)
    data = ''
    }

    return await this.fetch(url, method, headers, data)
    .then(async (resp) => {
    return await resp.json()
    })
    .catch((error) => {
    this.last_status_code = error
    return { status: 'error', message: `Receive Error: ${response}` }
    })
    }

    async fetch(url, method, headers, data) {
    return await fetch(url, {
    method,
    headers,
    body: data ? JSON.stringify(data) : null,
    redirect: 'follow', // manual, *follow, error
    })
    }

    buildQuery(params) {
    var searchParams = new URLSearchParams()

    Object.keys(params).forEach(function (name) {
    searchParams.append(name, params[name])
    })

    return searchParams.toString()
    }

    //Get auth headers for this call
    getHeaders() {
    return {
    'ORDERDESK-STORE-ID': this.store_id,
    'ORDERDESK-API-KEY': this.api_key,
    'Content-Type': 'application/json',
    }
    }

    get lastStatusCode() {
    return this.last_status_code
    }
    }