Giter Site home page Giter Site logo

Comments (5)

slavafomin avatar slavafomin commented on September 16, 2024

I'm not very familiar with Etherscan API response format, but my guess would be that each section of the API (or a group of sections) have different response format (which leads to an issues like this).

My suggestion would be to actually pass the module and action variables to the getRequest function and to handle the response parsing according to the module/action.

Please, consider the following example:

function getRequest(query, module, action) {

  return client.get('/api?' + query)
    .then(response => {

      const data = response.data;

      switch (module) {

        case 'account':

          /**
          {
            "status": "0",
            "message": "No transactions found",
            "result": []
          }
          */

          if (0 === data.status) {
            return Promise.reject(new Error(data.message));
          } else {
            return data.result;
          }

        default:

          // ... default (common) handling logic

          return data;

      }

    })
    // Probably this catch is not required at all
    .catch(error => {
      if (error instanceof Error) {
        reject(error); // passing as is
      } else {
        reject(new Error(error)); // wrapping in error
      }
    })
  ;

}

It's not a final implementation, but a very much close to it. Also, notice that your implementation could be simplified. No need to call new Promise() because axios already returns a promise.

from etherscan-api.

sebs avatar sebs commented on September 16, 2024

Throw me a PR, Ill merge all sane code ;)
I guess I would remark if statements when looking at what you propose.

There was a change regarding error reporting and surely this is my bad, i was just to stubborn to follow the specs. I have some time in the 2019 and will put this in.

I might consider using this as a starter for mocks.

Thanks for your time.

from etherscan-api.

sebs avatar sebs commented on September 16, 2024

Hold your horses with a PR -.....

I guess I will write some swagger definition and re-release the mod as a open api export. This will take some time, but it will tackle the issue of apis giving different responses in a well defined way. + focus is on testing the code and not writing boiler plate stuff.

from etherscan-api.

sebs avatar sebs commented on September 16, 2024

I guess we have to do #43

from etherscan-api.

sebs avatar sebs commented on September 16, 2024

can I close this? ;) Not sure atm. If not, there is time to improve more

from etherscan-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.