Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace $http with axios #4497

Merged
merged 49 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
05117ed
Add axios dependency
gabrieldutra Dec 26, 2019
18e8bc5
Update plain usage of $http to axios
gabrieldutra Dec 26, 2019
52098b5
Temporarily use $q in AlertDestinations
gabrieldutra Dec 26, 2019
a9e581c
Abstract axios with default interceptor
gabrieldutra Dec 27, 2019
2d8d163
AlertDestination service
gabrieldutra Dec 27, 2019
e26df5b
DataSource service
gabrieldutra Dec 27, 2019
90d36ef
AlertSubscription service
gabrieldutra Dec 27, 2019
89b29ed
Allow non-angular requests in ItemsSource
gabrieldutra Dec 27, 2019
b934add
Alert service
gabrieldutra Dec 27, 2019
ddf40e3
getTags service
gabrieldutra Dec 27, 2019
8e548a9
recordEvent/Event service
gabrieldutra Dec 27, 2019
7fe15b7
QuerySnippet service (with class example)
gabrieldutra Dec 27, 2019
823daa0
Group service
gabrieldutra Dec 27, 2019
7c3355d
Conver missing Alert mute and unmute
gabrieldutra Dec 27, 2019
e7f60da
Update UserShow jest test
gabrieldutra Dec 27, 2019
b888e18
OrganizationSettings service
gabrieldutra Dec 27, 2019
4fa6a04
OrganizationStatus service
gabrieldutra Dec 27, 2019
7393b55
Add getResponse to axios error interceptor
gabrieldutra Dec 27, 2019
ac1e047
User service
gabrieldutra Dec 27, 2019
9d38114
Use plain axios usages to import from service
gabrieldutra Dec 27, 2019
7a9409f
Query service
gabrieldutra Dec 29, 2019
410feb1
Temp add $applyAsync to query pages
gabrieldutra Dec 29, 2019
7a0f47d
Widget service
gabrieldutra Dec 29, 2019
2c6d895
Inject apiKey in axios (tmp with $injector)
gabrieldutra Dec 29, 2019
8cf00f3
Fix associatedDropdown
gabrieldutra Dec 29, 2019
9fdc33e
Map query results
gabrieldutra Dec 30, 2019
34b7ef0
Fix axios call in widget service
gabrieldutra Dec 30, 2019
3a89092
QueryResult service
gabrieldutra Dec 30, 2019
20ab4cd
Temp add $applyAsync to axios
gabrieldutra Dec 30, 2019
aa7cb1f
Address comments
gabrieldutra Dec 30, 2019
4ca22d7
Visualization service
gabrieldutra Dec 30, 2019
702bf63
Dashboard service
gabrieldutra Dec 30, 2019
5cbbaf3
Update $favorite and $unfavorite
gabrieldutra Dec 30, 2019
95b3585
Add dashboard delete
gabrieldutra Dec 30, 2019
dc7a4bb
Auth service
gabrieldutra Dec 30, 2019
fadd78b
Update dashboard after archive it
gabrieldutra Dec 30, 2019
89b4e4c
Updates for GH comments
gabrieldutra Dec 31, 2019
acd7974
Remove angular-resource
gabrieldutra Dec 31, 2019
261715f
Merge branch 'master' into axios
gabrieldutra Jan 6, 2020
b589be5
Remove events service
gabrieldutra Jan 6, 2020
b3e12c8
Remove temp $promise handlers
gabrieldutra Jan 6, 2020
0ed6dc2
Fix AddToDashboardDialog
gabrieldutra Jan 6, 2020
131aabc
Use save to create Alerts
gabrieldutra Jan 6, 2020
0ac577d
Remove $applyAsync for query angular pages
gabrieldutra Jan 7, 2020
1f949a9
Remove unnecessary import
gabrieldutra Jan 7, 2020
6d27c39
Keep refresh in getSchema
gabrieldutra Jan 7, 2020
aad39dc
Add $applyAsync for Param url update
gabrieldutra Jan 7, 2020
484e248
Merge branch 'master' into axios
gabrieldutra Jan 8, 2020
a2ae12b
Merge branch 'master' into axios
gabrieldutra Jan 12, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions client/app/components/dashboards/CreateDashboardDialog.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { trim } from "lodash";
import React, { useRef, useState, useEffect } from "react";
import axios from "axios";
import Modal from "antd/lib/modal";
import Input from "antd/lib/input";
import DynamicComponent from "@/components/DynamicComponent";
import { wrap as wrapDialog, DialogPropType } from "@/components/DialogWrapper";
import { $location, $http } from "@/services/ng";
import { $location } from "@/services/ng";
import recordEvent from "@/services/recordEvent";
import { policy } from "@/services/policy";

Expand Down Expand Up @@ -36,7 +37,7 @@ function CreateDashboardDialog({ dialog }) {
if (name !== "") {
setSaveInProgress(true);

$http.post("api/dashboards", { name }).then(({ data }) => {
axios.post("api/dashboards", { name }).then(({ data }) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that things like this should be moved to services (e.g. have a method called updateDashboard of whatever) - there should be no direct HTTP calls in components. There are a lot of similar places in other components as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. It was wrong to use $http all over. But wherever things are just working, we should keep things as they were (except for adopting axios so we can drop $http). In parallel, let's decide on a pattern we want to use for our API calls/resources, that will be more inline with how React works (returning immutable objects for example). Any suggestions for such pattern? Let's discuss in the issue.

Wherever things are not working as is or we have to jump through hoops to make it work, we can start adopting the new pattern. The rest we will update when we are done with the React migration.

dialog.close();
$location
.path(`/dashboard/${data.slug}`)
Expand Down
7 changes: 4 additions & 3 deletions client/app/components/items-list/classes/ItemsSource.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isFunction, identity, map, extend } from "lodash";
import { isFunction, identity, map, extend, get } from "lodash";
import Paginator from "./Paginator";
import Sorter from "./Sorter";
import PromiseRejectionError from "@/lib/promise-rejection-error";
Expand Down Expand Up @@ -156,8 +156,9 @@ export class ResourceItemsSource extends ItemsSource {
super({
...rest,
doRequest: (request, context) => {
const resource = getResource(context);
return resource(request).$promise;
const resource = getResource(context)(request);
// ANGULAR_REMOVE_ME just return resource when all Services are migrated
return get(resource, "$promise", resource);
},
processResults: (results, context) => {
let processItem = getItemProcessor(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState, useEffect, useCallback } from "react";
import axios from "axios";
import PropTypes from "prop-types";
import { each, debounce, get, find } from "lodash";
import Button from "antd/lib/button";
Expand All @@ -8,7 +9,6 @@ import Select from "antd/lib/select";
import Tag from "antd/lib/tag";
import Tooltip from "antd/lib/tooltip";
import { wrap as wrapDialog, DialogPropType } from "@/components/DialogWrapper";
import { $http } from "@/services/ng";
import { toHuman } from "@/filters";
import HelpTrigger from "@/components/HelpTrigger";
import { UserPreviewCard } from "@/components/PreviewCard";
Expand All @@ -23,7 +23,7 @@ const DEBOUNCE_SEARCH_DURATION = 200;
function useGrantees(url) {
const loadGrantees = useCallback(
() =>
$http.get(url).then(({ data }) => {
axios.get(url).then(({ data }) => {
const resultGrantees = [];
each(data, (grantees, accessType) => {
grantees.forEach(grantee => {
Expand All @@ -38,15 +38,15 @@ function useGrantees(url) {

const addPermission = useCallback(
(userId, accessType = "modify") =>
$http
axios
.post(url, { access_type: accessType, user_id: userId })
.catch(() => notification.error("Could not grant permission to the user")),
[url]
);

const removePermission = useCallback(
(userId, accessType = "modify") =>
$http
axios
.delete(url, { data: { access_type: accessType, user_id: userId } })
.catch(() => notification.error("Could not remove permission from the user")),
[url]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from "prop-types";
import cx from "classnames";
import { AceEditor, snippetsModule, updateSchemaCompleter } from "./ace";
import resizeObserver from "@/services/resizeObserver";
import { QuerySnippet } from "@/services/query-snippet";
import QuerySnippet from "@/services/query-snippet";

const editorProps = { $blockScrolling: Infinity };

Expand Down Expand Up @@ -91,7 +91,7 @@ const QueryEditorComponent = React.forwardRef(function(
}
});

QuerySnippet.query(snippets => {
QuerySnippet.query().then(snippets => {
const snippetManager = snippetsModule.snippetManager;
const m = {
snippetText: "",
Expand Down
5 changes: 3 additions & 2 deletions client/app/pages/admin/Jobs.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { flatMap, values } from "lodash";
import React from "react";
import axios from "axios";
import { react2angular } from "react2angular";

import Alert from "antd/lib/alert";
Expand All @@ -9,7 +10,7 @@ import Layout from "@/components/admin/Layout";
import { CounterCard } from "@/components/admin/CeleryStatus";
import { WorkersTable, QueuesTable, OtherJobsTable } from "@/components/admin/RQStatus";

import { $http, $location, $rootScope } from "@/services/ng";
import { $location, $rootScope } from "@/services/ng";
import recordEvent from "@/services/recordEvent";
import { routesToAngularRoutes } from "@/lib/utils";
import moment from "moment";
Expand Down Expand Up @@ -41,7 +42,7 @@ class Jobs extends React.Component {
}

refresh = () => {
$http
axios
.get("/api/admin/queries/rq_status")
.then(({ data }) => this.processQueues(data))
.catch(error => this.handleError(error));
Expand Down
4 changes: 2 additions & 2 deletions client/app/pages/admin/OutdatedQueries.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { map } from "lodash";
import React from "react";
import axios from "axios";
import { react2angular } from "react2angular";

import Switch from "antd/lib/switch";
Expand All @@ -18,7 +19,6 @@ import LoadingState from "@/components/items-list/components/LoadingState";
import { PageSizeSelect } from "@/components/items-list/components/Sidebar";
import ItemsTable, { Columns } from "@/components/items-list/components/ItemsTable";

import { $http } from "@/services/ng";
import { Query } from "@/services/query";
import recordEvent from "@/services/recordEvent";
import { routesToAngularRoutes } from "@/lib/utils";
Expand Down Expand Up @@ -156,7 +156,7 @@ export default function init(ngModule) {
new ItemsSource({
doRequest(request, context) {
return (
$http
axios
.get("/api/admin/queries/outdated")
// eslint-disable-next-line camelcase
.then(({ data: { queries, updated_at } }) => {
Expand Down
4 changes: 2 additions & 2 deletions client/app/pages/admin/SystemStatus.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { omit } from "lodash";
import React from "react";
import axios from "axios";
import PropTypes from "prop-types";
import { react2angular } from "react2angular";

import Layout from "@/components/admin/Layout";
import * as StatusBlock from "@/components/admin/StatusBlock";

import { $http } from "@/services/ng";
import recordEvent from "@/services/recordEvent";
import PromiseRejectionError from "@/lib/promise-rejection-error";
import { routesToAngularRoutes } from "@/lib/utils";
Expand Down Expand Up @@ -41,7 +41,7 @@ class SystemStatus extends React.Component {
}

refresh = () => {
$http
axios
.get("/status.json")
.then(({ data }) => {
this.setState({
Expand Down
4 changes: 2 additions & 2 deletions client/app/pages/admin/Tasks.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { values, each } from "lodash";
import moment from "moment";
import React from "react";
import axios from "axios";
import { react2angular } from "react2angular";

import Alert from "antd/lib/alert";
Expand All @@ -9,7 +10,6 @@ import * as Grid from "antd/lib/grid";
import Layout from "@/components/admin/Layout";
import { CounterCard, QueuesTable, QueriesTable } from "@/components/admin/CeleryStatus";

import { $http } from "@/services/ng";
import recordEvent from "@/services/recordEvent";
import { routesToAngularRoutes } from "@/lib/utils";

Expand All @@ -34,7 +34,7 @@ class Tasks extends React.Component {

componentDidMount() {
recordEvent("view", "page", "admin/tasks");
$http
axios
.get("/api/admin/queries/tasks")
.then(({ data }) => this.processTasks(data.tasks))
.catch(error => this.handleError(error));
Expand Down
27 changes: 13 additions & 14 deletions client/app/pages/alert/Alert.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { $route } from "@/services/ng";
import { currentUser } from "@/services/auth";
import navigateTo from "@/services/navigateTo";
import notification from "@/services/notification";
import { Alert as AlertService } from "@/services/alert";
import AlertService from "@/services/alert";
import { Query as QueryService } from "@/services/query";

import LoadingState from "@/components/items-list/components/LoadingState";
Expand Down Expand Up @@ -51,20 +51,20 @@ class AlertPage extends React.Component {

if (mode === MODES.NEW) {
this.setState({
alert: new AlertService({
alert: {
options: {
op: ">",
value: 1,
muted: false,
},
}),
},
pendingRearm: 0,
canEdit: true,
});
} else {
const { alertId } = $route.current.params;
AlertService.get({ id: alertId })
.$promise.then(alert => {
.then(alert => {
if (this._isMounted) {
const canEdit = currentUser.canEdit(alert);

Expand Down Expand Up @@ -100,12 +100,12 @@ class AlertPage extends React.Component {
alert.name = trim(alert.name) || getDefaultName(alert);
alert.rearm = pendingRearm || null;

return alert
.$save()
.then(() => {
const saveAlert = alert.id ? AlertService.save : AlertService.create;
return saveAlert(alert)
.then(alert => {
notification.success("Saved.");
navigateTo(`/alerts/${alert.id}`, true, false);
this.setState({ mode: MODES.VIEW });
this.setState({ alert, mode: MODES.VIEW });
})
.catch(() => {
notification.error("Failed saving alert.");
Expand Down Expand Up @@ -157,15 +157,14 @@ class AlertPage extends React.Component {

delete = () => {
const { alert } = this.state;
return alert.$delete(
() => {
return AlertService.delete(alert)
.then(() => {
notification.success("Alert deleted successfully.");
navigateTo("/alerts");
},
() => {
})
.catch(() => {
notification.error("Failed deleting alert.");
}
);
});
};

mute = () => {
Expand Down
26 changes: 12 additions & 14 deletions client/app/pages/alert/components/AlertDestinations.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { without, find, isEmpty, includes, map } from "lodash";
import SelectItemsDialog from "@/components/SelectItemsDialog";
import { Destination as DestinationType, UserProfile as UserType } from "@/components/proptypes";

import { Destination as DestinationService, IMG_ROOT } from "@/services/destination";
import { AlertSubscription } from "@/services/alert-subscription";
import { $q } from "@/services/ng";
import DestinationService, { IMG_ROOT } from "@/services/destination";
import AlertSubscription from "@/services/alert-subscription";
import { clientConfig, currentUser } from "@/services/auth";
import notification from "@/services/notification";
import ListItemAddon from "@/components/groups/ListItemAddon";
Expand Down Expand Up @@ -71,9 +70,9 @@ export default class AlertDestinations extends React.Component {

componentDidMount() {
const { alertId } = this.props;
$q.all([
DestinationService.query().$promise, // get all destinations
AlertSubscription.query({ alertId }).$promise, // get subcriptions per alert
Promise.all([
DestinationService.query(), // get all destinations
AlertSubscription.query({ alertId }), // get subcriptions per alert
]).then(([dests, subs]) => {
subs = subs.map(normalizeSub);
this.setState({ dests, subs });
Expand Down Expand Up @@ -142,12 +141,12 @@ export default class AlertDestinations extends React.Component {
subscribe = dest => {
const { alertId } = this.props;

const sub = new AlertSubscription({ alert_id: alertId });
const sub = { alert_id: alertId };
if (dest) {
sub.destination_id = dest.id;
}

return sub.$save(() => {
return AlertSubscription.create(sub).then(sub => {
const { subs } = this.state;
this.setState({
subs: [...subs, normalizeSub(sub)],
Expand All @@ -156,18 +155,17 @@ export default class AlertDestinations extends React.Component {
};

unsubscribe = sub => {
sub.$delete(
() => {
AlertSubscription.delete(sub)
.then(() => {
// not showing subscribe notification cause it's redundant here
const { subs } = this.state;
this.setState({
subs: without(subs, sub),
});
},
() => {
})
.catch(() => {
notification.error("Failed unsubscribing.");
}
);
});
};

render() {
Expand Down
4 changes: 2 additions & 2 deletions client/app/pages/alerts/AlertsList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { StateStorage } from "@/components/items-list/classes/StateStorage";
import LoadingState from "@/components/items-list/components/LoadingState";
import ItemsTable, { Columns } from "@/components/items-list/components/ItemsTable";

import { Alert } from "@/services/alert";
import Alert from "@/services/alert";
import { routesToAngularRoutes } from "@/lib/utils";

export const STATE_CLASS = {
Expand Down Expand Up @@ -121,7 +121,7 @@ export default function init(ngModule) {
return Alert.query.bind(Alert);
},
getItemProcessor() {
return item => new Alert(item);
return item => item;
},
}),
new StateStorage({ orderByField: "created_at", orderByReverse: true, itemsPerPage: 20 })
Expand Down
14 changes: 7 additions & 7 deletions client/app/pages/dashboards/ShareDashboardDialog.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { replace } from "lodash";
import React from "react";
import axios from "axios";
import PropTypes from "prop-types";
import Switch from "antd/lib/switch";
import Modal from "antd/lib/modal";
import Form from "antd/lib/form";
import Alert from "antd/lib/alert";
import { $http } from "@/services/ng";
import notification from "@/services/notification";
import { wrap as wrapDialog, DialogPropType } from "@/components/DialogWrapper";
import InputWithCopy from "@/components/InputWithCopy";
Expand Down Expand Up @@ -53,13 +53,13 @@ class ShareDashboardDialog extends React.Component {
const { dashboard } = this.props;
this.setState({ saving: true });

$http
axios
.post(this.apiUrl)
.success(data => {
.then(({ data }) => {
dashboard.publicAccessEnabled = true;
dashboard.public_url = data.public_url;
})
.error(() => {
.catch(() => {
notification.error("Failed to turn on sharing for this dashboard");
})
.finally(() => {
Expand All @@ -71,13 +71,13 @@ class ShareDashboardDialog extends React.Component {
const { dashboard } = this.props;
this.setState({ saving: true });

$http
axios
.delete(this.apiUrl)
.success(() => {
.then(() => {
dashboard.publicAccessEnabled = false;
delete dashboard.public_url;
})
.error(() => {
.catch(() => {
notification.error("Failed to turn off sharing for this dashboard");
})
.finally(() => {
Expand Down
Loading