-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Boulevard/lnikkila/v0.6.0
Release v0.6.0
- Loading branch information
Showing
6 changed files
with
598 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
defmodule ExQuickBooks.API do | ||
@moduledoc """ | ||
Functions for interacting with the API. | ||
This module directly implements operations from the official API: | ||
<https://developer.intuit.com/v2/docs/api/accounting> | ||
""" | ||
|
||
use ExQuickBooks.Endpoint, base_url: ExQuickBooks.accounting_api | ||
use ExQuickBooks.Endpoint.JSON | ||
|
||
alias ExQuickBooks.OAuth.AccessToken | ||
|
||
@doc """ | ||
Retrieves multiple entities using a SQL-like query. | ||
See the [query documentation][0] for more details. | ||
[0]: https://developer.intuit.com/docs/0100_quickbooks_online/0300_references/0000_programming_guide/0050_data_queries | ||
""" | ||
@spec query(AccessToken.t, String.t) :: {:ok, json_map} | {:error, any} | ||
def query(token, query) do | ||
headers = [{"Content-Type", "text/plain"}] | ||
options = [params: [{"query", query}]] | ||
|
||
request(:get, "company/#{token.realm_id}/query", nil, headers, options) | ||
|> sign_request(token) | ||
|> send_json_request | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
defmodule ExQuickBooks.APITest do | ||
use ExUnit.Case, async: false | ||
use ExQuickBooks.APICase | ||
|
||
alias ExQuickBooks.API | ||
alias ExQuickBooks.OAuth.AccessToken | ||
|
||
doctest API | ||
|
||
@token %AccessToken{ | ||
token: "token", | ||
token_secret: "secret", | ||
realm_id: "realm_id" | ||
} | ||
|
||
test "query/2 retrieves a query response" do | ||
load_response("api/query.json") |> send_response | ||
|
||
assert {:ok, %{"QueryResponse" => _}} = | ||
API.query(@token, "SELECT * FROM Item") | ||
|
||
assert %{ | ||
url: url, | ||
headers: headers, | ||
options: [params: params] | ||
} = take_request() | ||
|
||
assert String.contains?(url, "/query") | ||
|
||
assert {"Accept", "application/json"} in headers | ||
assert {"Content-Type", "text/plain"} in headers | ||
|
||
assert {"query", "SELECT * FROM Item"} in params | ||
end | ||
|
||
test "query/2 recovers from an error" do | ||
load_response("api/query_error.json") | ||
|> Map.put(:status_code, 400) | ||
|> send_response | ||
|
||
assert {:error, %{"Fault" => _}} = | ||
API.query(@token, "SELECT * FROM NULL") | ||
end | ||
end |
Oops, something went wrong.