Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider 4ec62ba4 rédigé par Erwan BOUSSE's avatar Erwan BOUSSE
Parcourir les fichiers

refactor!: remove default gitlab host, add error

parent 24477a5e
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Pipeline #28450 réussi
import axios, { AxiosInstance } from 'axios' import axios, { AxiosInstance } from 'axios'
import { Problem } from '../exceptions'
import { HTTPClient, HTTPClientConfig, HTTPResponse, RequestConfig } from './httpClient.interface' import { HTTPClient, HTTPClientConfig, HTTPResponse, RequestConfig } from './httpClient.interface'
export class AxiosHTTPClient implements HTTPClient { export class AxiosHTTPClient implements HTTPClient {
...@@ -6,19 +7,27 @@ export class AxiosHTTPClient implements HTTPClient { ...@@ -6,19 +7,27 @@ export class AxiosHTTPClient implements HTTPClient {
constructor(readonly config: Partial<HTTPClientConfig> = {}) { constructor(readonly config: Partial<HTTPClientConfig> = {}) {
this.instance = axios.create({ this.instance = axios.create({
baseURL: (config.host || 'https://gitlab.univ-nantes.fr') + '/api/v4', baseURL: config.host + '/api/v4',
headers: { headers: {
Authorization: `Bearer ${config.accessToken}`, Authorization: `Bearer ${config.accessToken}`,
}, },
}) })
} }
private checkConfiguration() {
if (!this.config.host) {
throw new Problem('No configuration found. Please configure the Gitlab host to use.')
}
}
async get<T>(path: string, config?: RequestConfig): Promise<HTTPResponse<T>> { async get<T>(path: string, config?: RequestConfig): Promise<HTTPResponse<T>> {
this.checkConfiguration()
const { data, headers, status } = await this.instance.get(path, config || {}) const { data, headers, status } = await this.instance.get(path, config || {})
return { data, headers, status } return { data, headers, status }
} }
async post<T>(path: string, config?: RequestConfig): Promise<HTTPResponse<T>> { async post<T>(path: string, config?: RequestConfig): Promise<HTTPResponse<T>> {
this.checkConfiguration()
const { data, headers, status } = await this.instance.post(path, config?.body, config || {}) const { data, headers, status } = await this.instance.post(path, config?.body, config || {})
return { data, headers, status } return { data, headers, status }
} }
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter