imob-sdk
Version:
SDK para acessar a API do Imóvel Periciado.
155 lines (143 loc) • 7.32 kB
JavaScript
const logger = require('../settings/logger');
const apiClient = require('../settings/apiClient');
const PostResponse = require('../client/responses/PostResponse');
const { postBaseUrl } = require('../settings/config');
const { ResponseStatus } = require('../client/enums/PostEnum');
class PostApi {
constructor(auth, basic_auth = undefined) {
this.auth = auth;
this.basic_auth = basic_auth ? `Basic ${btoa(basic_auth)}` : undefined;
logger.info('PostApi instanciado com sucesso!');
}
async publishPost(post) {
try {
if (!this.auth.authenticated())
throw new Error('Usuário não autenticado!');
const url = `${postBaseUrl}/post/pages`;
const data = post.toJSON();
logger.info(`Publicando post: ${JSON.stringify(data['title'])}`);
const response = await apiClient.post(url, data, {
headers: {
'IP-Authorization': this.auth.session.session_object.authorization_header(),
...(this.basic_auth ? { 'Authorization': this.basic_auth } : {}),
}
});
logger.info(`Post publicado com sucesso: ${JSON.stringify(response)}`);
return new PostResponse(ResponseStatus.SUCCESS, { postId: response['id'] }, 'Post publicado com sucesso!');
} catch (error) {
logger.error(`Erro ao publicar post:`, error);
return new PostResponse(ResponseStatus.ERROR, { postId: null,response: error.response.data || null }, `Erro ao publicar post: ${error.message}`);
}
}
async deletePost(postId) {
try {
if (!this.auth.authenticated())
throw new Error('Usuário não autenticado!');
if (!postId)
throw new Error('Id do post não informado!');
const url = `${postBaseUrl}/post/pages/${postId}`;
logger.warn(`Deletando post: ${postId}`);
const response = await apiClient.delete(url, {
headers: {
'IP-Authorization': this.auth.session.session_object.authorization_header(),
...(this.basic_auth ? { 'Authorization': this.basic_auth } : {}),
}
});
if (!response)
throw new Error('Post não encontrado!');
return new PostResponse(ResponseStatus.SUCCESS, response, 'Post deletado com sucesso!');
} catch (error) {
logger.error(`Erro ao publicar post:`, error);
return new PostResponse(ResponseStatus.ERROR, error.response.data, `Erro ao deletar post: ${error.message}`);
}
}
async updatePost(postId, post) {
try {
if (!this.auth.authenticated())
throw new Error('Usuário não autenticado!');
if (!postId)
throw new Error('Id do post não informado!');
const url = `${postBaseUrl}/post/pages/${postId}`;
const data = post.toJSON();
logger.info(`Atualizando post: ${postId}`);
const response = await apiClient.put(url, data, {
headers: {
'IP-Authorization': this.auth.session.session_object.authorization_header(),
...(this.basic_auth ? { 'Authorization': this.basic_auth } : {}),
}
});
if (!response)
throw new Error('Post não encontrado!');
return new PostResponse(ResponseStatus.SUCCESS, { post: response }, 'Post atualizado com sucesso!');
} catch (error) {
logger.error(`Erro ao publicar post:`, error);
return new PostResponse(ResponseStatus.ERROR, error.response.data, `Erro ao atualizar o post: ${error.message}`);
}
}
async getPost(postId) {
try {
if (!this.auth.authenticated())
throw new Error('Usuário não autenticado!');
if (!postId)
throw new Error('Id do post não informado!');
const url = `${postBaseUrl}/post/pages/${postId}`;
logger.info(`Buscando post: ${postId}`);
const response = await apiClient.get(url, {
headers: {
'IP-Authorization': this.auth.session.session_object.authorization_header(),
...(this.basic_auth ? { 'Authorization': this.basic_auth } : {}),
}
});
if (!response)
throw new Error('Post não encontrado!');
return new PostResponse(ResponseStatus.SUCCESS, { post: response }, 'Post encontrado com sucesso!');
} catch (error) {
logger.error(`Erro ao publicar post:`, error);
return new PostResponse(ResponseStatus.ERROR, error.response.data, `Erro ao buscar o post: ${error.message}`);
}
}
async renderPost(postId) {
try {
if (!this.auth.authenticated())
throw new Error('Usuário não autenticado!');
if (!postId)
throw new Error('Id do post não informado!');
const url = `${postBaseUrl}/post/pages/${postId}/rendered`;
logger.info(`Renderizando post: ${postId}`);
const response = await apiClient.get(url, {
headers: {
'IP-Authorization': this.auth.session.session_object.authorization_header(),
...(this.basic_auth ? { 'Authorization': this.basic_auth } : {}),
}
});
if (!response)
throw new Error('Post não encontrado!');
return new PostResponse(ResponseStatus.SUCCESS, { post: response }, 'Post renderizado com sucesso!');
} catch (error) {
logger.error(`Erro ao publicar post:`, error);
return new PostResponse(ResponseStatus.ERROR, error.response.data, `Erro ao renderizar o post: ${error.message}`);
}
}
async getPosts(page = 1, size = 10) {
try {
if (!this.auth.authenticated())
throw new Error('Usuário não autenticado!');
const url = `${postBaseUrl}/post/pages?page=${page}&size=${size}`;
logger.info(`Buscando todos os posts deste usuário`);
const response = await apiClient.get(url, {
headers: {
'IP-Authorization': this.auth.session.session_object.authorization_header(),
...(this.basic_auth ? { 'Authorization': this.basic_auth } : {}),
}
});
if (!response)
throw new Error('Nenhum post encontrado!');
logger.info(`Posts encontrados: ${response.total}`);
return new PostResponse(ResponseStatus.SUCCESS, response, 'Todos os posts encontrados com sucesso!');
} catch (error) {
logger.error(`Erro ao publicar post: ${JSON.stringify(error?.response?.data)}`);
return new PostResponse(ResponseStatus.ERROR, error?.response?.data, `Erro ao buscar a lista de post: ${error.message}`);
}
}
}
module.exports = PostApi;