Hi! I will appreciate an advice on how to implemen...
# dev
t
Hi! I will appreciate an advice on how to implement the following: This is my code that wraps a call to our objects API -
Copy code
async get(repoId, ref, path, additionalHeaders) {
        const query = qs({path});
        const response = await apiRequest(`/repositories/${repoId}/refs/${ref}/objects?${query}`, {
            method: 'GET',
            headers: new Headers(additionalHeaders)
        });
        if (response.status !== 200 && response.status !== 206) {
            throw new Error(await extractError(response));
        }
        return response.text()
    }
The promise it returns reads the object text from a stream and then returning. This get operation receives a Range header as a parameter and I need to track the response “Content-Range” to determine whether the full object content is returned or only part of it. The problem i’m facing is how to return this information while the get function must return a promise because i’m invoking it with useAPI? Can I somehow wrap the
response.text(), response.headers.get("Content-Range")
with a promise? @Barak Amar @Guy Hardonag @Ariel Shaqed (Scolnicov) maybe you have an advice?
b
it looks like you want to split the 'get' implementation - extract the complete implementation and return 'response'. the code will use 'get' that will return and 'response.text()' and you will need to to the lower one that returns the response. this way you will have access to the response headers and wait for the body if needed.
👀 1
t
Another questions about using the “Range” header - When I use it sometimes the server responds with HTTP/1.1 200 and sometimes with HTTP/1.1 206 in an inconsistent manner. I couldn’t find useful help online, any idea why this is happening?
a
(IIUC a server is allowed to send more bytes than requested?)
t
thanks @Ariel Shaqed (Scolnicov)! this question is really useful.. I understand this the same as you do.. Ok, so I will need to know the object size ahead of making the get request
Whic actually works with another product requirement I got to also show stats for readable objects
a
I am not sure why you need to know the size of an object in order to query it. (You can get it from a HEAD HTTP query, of course, I'm just not sure why you need to do it.)