<commit_before>// @flow
import {loadLoginCredentials} from '../login'
import buildFormData from '../formdata'
import {OLECARD_AUTH_URL, OLECARD_DATA_ENDPOINT} from './urls'
import type {BalancesShapeType, OleCardBalancesType} from './types'

type BalancesOrErrorType =
	| {error: true, value: Error}
	| {error: false, value: BalancesShapeType}

export async function getBalances(): Promise<BalancesOrErrorType> {
	const {username, password} = await loadLoginCredentials()

	if (!username || !password) {
		return {error: true, value: new Error('Not logged in')}
	}

	const form = buildFormData({username, password})

	try {
		let loginResponse = await fetch(OLECARD_AUTH_URL, {
			method: 'POST',
			body: form,
		})

		if (loginResponse.url.includes('message=')) {
			return {error: true, value: new Error('Login failed')}
		}

		const resp: OleCardBalancesType = await fetchJson(OLECARD_DATA_ENDPOINT)

		if (resp.error != null) {
			return {
				error: true,
				value: new Error(resp.error),
			}
		}
<commit_msg>getBalances: Include credentials after redirects<commit_after>// @flow
import {loadLoginCredentials} from '../login'
import buildFormData from '../formdata'
import {OLECARD_AUTH_URL, OLECARD_DATA_ENDPOINT} from './urls'
import type {BalancesShapeType, OleCardBalancesType} from './types'

type BalancesOrErrorType =
	| {error: true, value: Error}
	| {error: false, value: BalancesShapeType}

export async function getBalances(): Promise<BalancesOrErrorType> {
	const {username, password} = await loadLoginCredentials()

	if (!username || !password) {
		return {error: true, value: new Error('Not logged in')}
	}

	const form = buildFormData({username, password})

	try {
		let loginResponse = await fetch(OLECARD_AUTH_URL, {
			method: 'POST',
			body: form,
			credentials: 'include',
		})

		if (loginResponse.url.includes('message=')) {
			return {error: true, value: new Error('Login failed')}
		}

		const resp: OleCardBalancesType = await fetchJson(OLECARD_DATA_ENDPOINT)

		if (resp.error != null) {
			return {
				error: true,
				value: new Error(resp.error),
			}
		}

{'fraction_non_alphanumeric': 0.129293893129771, 'fraction_numerical': 0.0, 'mean_word_length': 4.698369565217392, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '952416', 'n_tokens_mistral': 738, 'n_tokens_neox': 676, 'n_words': 182}