Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response style #3

Closed
frct1 opened this issue Jan 20, 2018 · 17 comments
Closed

Response style #3

frct1 opened this issue Jan 20, 2018 · 17 comments

Comments

@frct1
Copy link

frct1 commented Jan 20, 2018

Hi, schroffl yesterday i'm met with your library, it's awesome, but have an problem.
How to get command with one mere elements (serverlist for example) output like this, because not very comfortable to work with output like this.
Last library which i was work it is node-teamspeak thats return response this.

@schroffl
Copy link
Owner

Could you please add the raw response of the server? I can't test it myself right now since I'm in the process of reinstalling Windows.

As for the issue, I was thinking of letting users pass custom parsers for certain commands.
Maybe something along the lines of this:

function myParser(str) {
  // Do some parsing
  return data;
}

const TeamspeakQuery = require('teamspeak-query');
const query = new TeamspeakQuery('127.0.0.1', 10011);

query.send('login', 'serveradmin', 'changeme')
  .then(() => query.send(myParser, 'serverlist'))
  .then(console.log);

If one needs to run a command with a specific parser frequently, some javascript-magic could be applied:

const niceSend = query.send.bind(query, myParser);

// use it
query.send('login', 'serveradmin', 'changeme')
  .then(() => niceSend('serverlist'))
  .then(() => niceSend('some other command'))
  .then(() => niceSend('some other other command'));

Common parsers for a case like yours could be added to the library.

@frct1
Copy link
Author

frct1 commented Jan 20, 2018

<Removed>

or privilegekeylist command

<Removed>

Thank you for fast answer

Edit (schroffl): I removed the output of the two commands, just in case it contained any confidential data.

@schroffl
Copy link
Owner

schroffl commented Jan 21, 2018

Hey,

I am currently not really sure why I parse responses as if it had this syntax:
virtualserver_id=4|9 virtualserver_port=9950|9987 (multiple values being specified for the same property) etc.
Do you know of any command where this might be the case? If not, I could just split by | and parse each entry in that list.

@frct1
Copy link
Author

frct1 commented Jan 21, 2018

Hi, I think command snapshotcreate that what you need, but in both libraries I used was a strange parsed output of the result of this command.
In the library which I used it was just such a parsing (split by | and parse each entry), since there might be several elements in the response from the server.

@schroffl
Copy link
Owner

schroffl commented Jan 21, 2018

snapshotcreate doesn't seem to be properly parsable with either method. It has too many inconsistencies.
However, the parseList function in this snippet should be sufficient to produce a nice output of commands like clientdblist, clientlist, etc. and work with the current version of the library.

const TeamspeakQuery = require('teamspeak-query');
const query = new TeamspeakQuery('127.0.0.1', 10011);

function parseList(data) {
  return data.raw()
    .split('|')
    .map(TeamspeakQuery.parse)
    .map(entry => entry.params);
}

query.send('login', 'serveradmin', 'changeme')
  .then(() => query.send('use', 1))
  .then(() => query.send('clientdblist'))
  .then(parseList)
  .then(console.log)
  .catch(console.error);

and if you use that pattern a few times you can create a small helper function

function sendList() {
  return query.send.apply(query, Array.from(arguments)).then(parseList);
}

query.send('login', 'serveradmin', 'changeme')
  .then(() => query.send('use', 1))
  .then(() => sendList('clientdblist'))
  .then(() => sendList('clientlist'))
  .then(console.log)
  .catch(console.error);

I would prefer to keep the core library as concise as possible and only expose a simple and clean API.
And if anybody ever needs to do some custom parsing, they can do so by calling the raw function of the response object (which I forgot to document, but will do now) to obtain the plaintext.

@frct1
Copy link
Author

frct1 commented Jan 21, 2018

Oh, check the bug, if output will parse by custom function in every element have raw data of server response.

@schroffl
Copy link
Owner

schroffl commented Jan 21, 2018

This is a result of mapping TeamspeakQuery.parse over the array that was created by splitting the string.
If you don't want it you can update your parseList function to look like this:

function parseList(data) {
  return data.raw()
    .split('|')
    .map(TeamspeakQuery.parse)
    .map(entry => entry.params)
    .map(entry => {
      delete entry.raw;
      return entry;
    });
}

or if you want to keep it small

function parseList(data) {
  return data.raw()
    .split('|')
    .map(TeamspeakQuery.parse)
    .map(entry => entry.params)
    .map(entry => (delete entry.raw, entry));
}

@frct1
Copy link
Author

frct1 commented Jan 21, 2018

Ok, I have the last question when I try to connect to several instances by getting a list of servers from the api (returning object with data of every server instance), then there is a problem that connects only to one server.
But if you specify a list of servers at the beginning of the application, then all connections to the described servers are successful.

@schroffl
Copy link
Owner

Could you add a small example? I'm not really sure what you mean.

@frct1
Copy link
Author

frct1 commented Jan 22, 2018

Hi, @schroffl.
Yes, sure, that's example with server list, which described in applicaton.
That's second example with billing api usage

@schroffl
Copy link
Owner

I don't see any problems in your code. Do you get any network errors while trying to connect? Are you sure that you are using the correct ip and port for your servers?

@frct1
Copy link
Author

frct1 commented Jan 22, 2018

Nope, haven't any errors. In api response same object, which was write in first example.
Buuuuut first example working, and second no 🤔
I'm can send you data of testing instances, via e-mail or messenger

@schroffl
Copy link
Owner

You can send it to this email: schroffl@users.noreply.github.com
Just leave a comment when you send it, because I'm not entirely sure if this email works ^^

@frct1
Copy link
Author

frct1 commented Jan 22, 2018

Heh, okay, I prepare the instances and send the data for connection.

@schroffl
Copy link
Owner

schroffl commented Jan 22, 2018

Doesn't seem to have worked, can you try this one: <removed>

@frct1
Copy link
Author

frct1 commented Jan 22, 2018

Successful send data about instances to <removed>

@schroffl
Copy link
Owner

Resolved via e-mail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants