chulkilee / ex_force

A Salesforce REST API wrapper for Elixir
https://hex.pm/packages/ex_force
MIT License
38 stars 28 forks source link

Tesla 1.0 #21

Closed chulkilee closed 6 years ago

chulkilee commented 6 years ago

17

coveralls commented 6 years ago

Coverage Status

Coverage increased (+6.1%) to 100.0% when pulling 53fcc95219eb2ca4432a682c25462ef416b2348b on tesla-1.0 into f67d852860b7a8be40271c5cb08d34302bdf2bd0 on master.

zblanco commented 6 years ago

https://github.com/chulkilee/ex_force/blob/0da000d037adf28671b93784112b7c1e0287fca6/lib/ex_force.ex#L18

  client = ExForce.build_client("
    https://login.salesforce.com",
    access_token: access_token,
    api_version: "40.0"
  )

The instance_url returned as the OAuth.Response struct is what needs to be passed into the client instead of something like "https://login.salesforce.com" or "https://test.salesforce.com" or an error like the following will occur.

iex> ExForce.query(client, "Select Id, Name FROM Account")
{:error,
 {:no_default_port, :" https",
  ' https://test.salesforce.com/services/data/v41.0/query?q=Select+Id%2C+Name+FROM+Account'}}

Since the data is already available in the OAuth.Response struct, maybe the build_client/2 function can accept the OAuth.Response struct, a map, tuple, or another struct that contains the instance_url, and access_token.

EDIT: This works too

## Usage

{:ok, %{access_token: access_token, instance_url: instance_url}} = ExForce.OAuth.get_token(
    "https://login.salesforce.com",
    grant_type: "password",
    client_id: "client_id",
    client_secret: "client_secret",
    username: "username",
    password: "password"
  )

  client = ExForce.build_client("
    instance_url,
    access_token: access_token,
    api_version: "40.0"
  )
chulkilee commented 6 years ago

@MrDoops Thanks a lot for feedback!

First of all, the example code had a leading whitespaces to https://.

Also I found that REST API call must be called on instance url, not login.salesforce.com (or you will get following error:

{:error, [%{"errorCode" => "URL_NOT_RESET", "message" => "Destination URL not reset. The URL returned from login must be set"}]}

So, it should be actual instance_url, not just login.salesforce.com.

And I agree that it's better to let build_client/2 accept the map (compatible with ExForce.OAuthResponse).

Pushed a commit for this (with some refactoring)