MCPcopy Create free account
hub / github.com/OpenHands/OpenHands / createMarsApiClient

Function createMarsApiClient

scripts/mars-api.mjs:156–351  ·  view source on GitHub ↗
({
  getToken,
  baseUrl = DEFAULT_MARS_API_BASE_URL,
  deviceId = null,
  fetchImpl = globalThis.fetch,
})

Source from the content-addressed store, hash-verified

154 * @param {typeof globalThis.fetch} [options.fetchImpl] Injectable for tests.
155 */
156export function createMarsApiClient({
157 getToken,
158 baseUrl = DEFAULT_MARS_API_BASE_URL,
159 deviceId = null,
160 fetchImpl = globalThis.fetch,
161}) {
162 async function request(
163 method,
164 path,
165 { query, timeoutMs, body, signal } = {},
166 ) {
167 const token = await getToken();
168 if (!token) {
169 throw new MarsApiError("Not signed in to DigitalOcean.", { status: 401 });
170 }
171
172 const headers = { Authorization: `Bearer ${token}` };
173 if (deviceId) headers[DEVICE_ID_HEADER] = deviceId;
174 if (body !== undefined) headers["Content-Type"] = "application/json";
175
176 const timeout = AbortSignal.timeout(timeoutMs ?? DEFAULT_TIMEOUT_MS);
177 let response;
178 try {
179 response = await fetchImpl(buildMarsUrl(baseUrl, path, query), {
180 method,
181 headers,
182 body: body === undefined ? undefined : JSON.stringify(body),
183 signal: signal ? AbortSignal.any([signal, timeout]) : timeout,
184 });
185 } catch (error) {
186 throw new MarsApiError(
187 `Could not reach DigitalOcean: ${error instanceof Error ? error.message : String(error)}`,
188 { cause: error },
189 );
190 }
191
192 if (!response.ok) {
193 throw new MarsApiError(await readErrorMessage(response), {
194 status: response.status,
195 });
196 }
197
198 // 204 on pause/resume/delete — no body to parse.
199 if (response.status === 204) return null;
200 return response.json();
201 }
202
203 return {
204 /** `{sessions, next_page_token}`; results are always team-scoped. */
205 async listSessions({ pageSize, pageToken, status } = {}) {
206 const body = await request("GET", SESSIONS_PATH, {
207 query: { page_size: pageSize, page_token: pageToken, status },
208 });
209 return {
210 sessions: Array.isArray(body?.sessions) ? body.sessions : [],
211 nextPageToken: body?.next_page_token || null,
212 };
213 },

Callers 3

clientForFunction · 0.90
startFakeSessionsApiFunction · 0.90
clientAnsweringFunction · 0.90

Calls

no outgoing calls

Tested by 2

startFakeSessionsApiFunction · 0.72
clientAnsweringFunction · 0.72