1.0.6 • Published 3 years ago

pxys-request-occ v1.0.6

Weekly downloads
28
License
ISC
Repository
-
Last release
3 years ago

pxysService

Build Status

pxysService is a minimal utility to test, sign request and send them through to the proxy. Also can help you to parse proxy responses to show results to the client (front-end) with the errorManager.

Requirements

  • Access to proxy server you want to test
  • App Key & Secret Key for your proxy user
  • A machine with nodejs

Before start

It is highly recommended that you install cross-env utility globaly as it will help you later to set npm commands with personalized configs. From your cmd or bash use:

npm install -g cross-env

Setup

Create an object that will help you with your configuration like this:

{
  "host": "http://localhost:8080/",
  "timeout": 1000000,
  "appKey": "######",
  "secretKey": "$$$$$"
}

*Some test configs are provided by default on the ./config folder.

What values do I need to put?

  • host: The url for your proxy server, environment dependent
  • timeout: Maximum time before providing a timeout in ms
  • appKey: Credential given by your administrator
  • secretKey: Credential password given by your administrator Depending on the resources required you might need more or less time on the timeout key and different appKey & secretKey.

So now I want to start testing the project

To run with default config:

node ./server/index.js

Or if you have cross-env installed

npm run dev

Example:

url service:

The following code should provide an entry point to the url seus service:

// Document on: ./routes/testUrl
const { Router } = require('express');
const config = require('config');
const proxyConfig = config.get('proxy');
const PxysService = new require('../../services/pxysService');
const errorService = require('../../services/pxysErrorManager');

const router = new Router();
router.route('/testUrl').get((req, res) => {
  const reqObj = {};
  reqObj.service = 'seus';
  reqObj.version = 'v1_0';
  reqObj.method = 'post';
  reqObj.resource = 'seo';
  reqObj.instance = 'url';
  reqObj.parameters = '';

  const pxysService = new PxysService(proxyConfig);
  pxysService.pxysService((error, resPxys) => {
    if (error) {
      try {
        res.send(error);
        return;
      } catch (ex) {
        res.send(ex);
        return;
      }
    }
    res.send(resPxys);
  }, reqObj);
});

module.exports = router;

Then add it to ./server/index.js

const testUrl = require('./routes/testUrl');
...
server.use('/v1', [testUrl]);

Start your project with npm run dev Then in postman (GET) or browser perform the request to http://localhost:3000/v1/testUrl You should see the following output:

{
    "urls": [
        {
            "key": "",
            "url": "empleos/",
            "solr_url": "q=*:*",
            "redirect": false,
            "filters": null,
            "safe": false
        }
    ],
    "meta": {
        "time": 0.0646,
        "host": "dev-solr-01",
        "ip": "10.10.30.85 172.17.0.1 ",
        "version": "1.0.0"
    }
}

And in the console you should see:

### Ready on http://localhost:3000, NODE_ENV=development
::1 - - [04/Apr/2019:23:36:02 +0000] "GET /v1/testUrl HTTP/1.1" 200 195 "-" "PostmanRuntime/7.6.0"

Exception handling

When you perform a request there is a chance that you get an exception, this can be caused by some of the following:

  • Bad request
  • Service you are looking is down
  • Timeout with proxy or with service required
  • Proxy is down
  • Your computer cannot reach proxy
  • Error within your code

To handle this errors we provide an errorManager that can be imported from the library:

pxysService.pxysService((error, resPxys) => {
    if (error === null) {
        res.send({ resPxys, status: 200 });
    }
    try {
        // Bad request or similar
        // This will parse the errors from atreel
        const responseError = errorService.getErrorDescriptionFromErrorArray(error);
        console.log(`#### pxysService :: Error Description: ${JSON.stringify(responseError)} ####`);
        const response = { error: 'Ocurrió un error, intente más tarde.', status: 501 };
        res.send(response);
    } catch (ex) {
        // Internal error
        // This will handle timeouts and 404 nginx responses...
        console.log(`#### pxysService :: Error While parsing response #### ${JSON.stringify(ex)}`);
        const response = { error: 'Ocurrió un error, intente más tarde.', status: 500 };
        res.send(response);
    }
  }, reqObj);