Coder Social home page Coder Social logo

jar's Introduction

Jar

An example TaxJar API client.

Usage

Jar can be configured globally.

import Config

config :jar, Jar.Config,
  token: "your api token",
  sandbox: false,
  debug: false,
  mock_http: false

The global client can be fetched like this:

client = Jar.global()

Clients can also be configured at runtime.

client = Jar.configure(%{
  token: "your api token",
  sandbox: false,
  debug: false,
  mock_http: false
%})

API functions take your %Jar.Client{} as the first argument. Here's an example API call to validate an address, using global configuration:

Jar.validate_address(client, %{
  country: "US",
  state: "AZ",
  zip: "85297",
  city: "Gilbert",
  street: "3301 South Greenfield Rd"
})

# {:ok, %{addresses: [...]}}

Testing

By default, tests run against mocks.

Testing against the Sandbox

To instead run tests against the live Sandbox API service, create the following config/test.secret.exs file, substituting your own Sandbox API token:

import Config

config :jar, Jar.Config,
  token: "your token",
  mock_http: false

Endpoints that the Sandbox doesn't support

At least 3 functions can't be tested against the live Sandbox API service.

To prevent you from accidentally altering your live production data or incurring charges on your TaxJar bill, tests for these endpoints are set up to always run against mocks, regardless of test configuration. When altering test code, take care to ensure that this behavior is maintained.

See prod_only_endpoints_test.exs and the always_mocked_production_client_factory/0 function in test/support/factory.ex.

jar's People

Contributors

jwietelmann avatar

Watchers

 avatar  avatar

jar's Issues

Add the option to use a global client configuration

Passing a client struct to each call is good because it makes the lib more flexible, but ideally there should also be a way to use a global configuration. Rather than refactor to make the client argument of API functions optional, I think it should suffice for now just to make something like this possible:

Jar.global() # instead of Jar.configure(options)
|> Jar.list_categories()
|> case do
   # ...
end

Test functions whose API endpoints are missing from the sandbox

They are:

  • Jar.list_customers/1
  • Jar.validate_address/2
  • Jar.validate_vat_number/2

They should probably get checked against the non-sandbox API at least once before everything in tests gets mocked (#1). Careful not to accidentally run the whole test suite against the live, non-sandbox API in the process.

Handle unexpected 204 errors?

When using the address validation endpoint, I momentarily forgot to have my test POST any params. Instead of receiving any kind of error, I got a 204 No Content, which implies success. (This is something I'd consider a bug on TaxJar's end, but it would demand an API version bump if fixed, IMO.)

Anyway, my code's returning {:ok, _} in this case. But it's not okay! What actually happened was the API server more or less rejected the request for lack of appropriate params.

(Cursory testing suggests the problem is probably limited to /addresses/validate.)

Some level of pre-validation for params?

Related to #8 maybe.

It probably doesn't make sense to go overboard on pre-validating everything possible.

What probably does make sense: If the API endpoint requires params, the Elixir function's signature shouldn't have an optional params arg.`

Complete API coverage

I made notes of which sections of the API I originally skipped in the jar.ex file, with TODO: comments. Time to actually finish those up.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.