aaronsky / appstoreconnect

Unofficial REST API client for Apple's App Store Connect API
MIT License
25 stars 15 forks source link

Unable to read gzipped data from v1.financeReports #3

Open christianbueschi opened 5 years ago

christianbueschi commented 5 years ago

First of all, thank you for this awesome library. It's exactly what I was looking for!

Now I'm trying to get fincanceReports without any success! The response is a-gzip encoded and I don't know how to decode it by using nodes zlib module. I'm getting the error: Error: unexpected end of file. This is the adjusted code of the request:

async function call(api, path, method, options = {}) {
    let rawResponse;
    try {
        rawResponse = await got_1.default(path, {
            baseUrl: api.baseUrl,
            method,
            decompress: true,
            headers:
                 {
                    'Authorization': "Bearer" + api.token",
                    'Accept': '*/*',
                    'Content-Type': 'application/a-gzip',
                    'Accept-Encoding' : 'agzip, deflate',
                },
            query: query(options.query),
            body: options.body && JSON.stringify(options.body),
        });
    }
    catch (error) {
        throw new Error(error.response.body);
    }
    try {
        var buffer = [];
        var gunzip = zlib.createGunzip();            
        rawResponse.pipe(gunzip);
        gunzip.on('data', function(data) {
            // decompression chunk ready, add it to the buffer
            console.log(data.toString());
            buffer.push(data.toString())
        }).on("end", function() {
            // response and decompression complete, join the buffer and return
            buffer.join(""); 
            console.log(buffer);
        }).on("error", function(e) {
            console.log(e);
        })
    }
    catch (jsonError) {
        throw jsonError;
    }
    return json;
}

When I test the request with Postman I get the same response but I can then download the file over postman's UI and get a file.txt.gz. When I unzip that, I get a readable text file with the financeReport.

Do you have any idea what I am missing here?

aaronsky commented 5 years ago

I just put out a change in 0.2.1 that should make it easier to consume the compressed data. appstoreconnect will return a Buffer when hitting one of the two gzip endpoints. Could you try it out?