Skip to content

Commit

Permalink
Allow specifying api_version and SFDC endpoint
Browse files Browse the repository at this point in the history
Prior to this commit, library users were not able to customize which
version of the API they used, (at least not prior to login).
Additionally, the library offered no way to log into a test or sandbox
instance of SalesForce. This commit allows library users to create
their own Forcex.Client structs as a starting point. This will allow
using different API versions and sandbox instances. This may also allow
different authentication grant_types other than password.

Close #7.
  • Loading branch information
Jeff Weiss committed May 9, 2016
1 parent beeb7ff commit cd8b4d2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ first_page = Forcex.query("select Id, Name from Account order by CreatedDate des
second_page = first_page |> Map.get("nextRecordsUrl") |> Forcex.get(client)
```

## Further Configuration

Forcex allows additional configuration of API endpoint and API version via the
`%Forcex.Client{}` struct. You may also use this mechanism if you have a
`grant_type` other than password.

This example shows how to use both an older API version and the SalesForce
sandbox API.
```elixir
Forcex.Client.default_config
|> Forcex.Client.login(%Forcex.Client{endpoint: "https://test.salesforce.com", api_version: "34.0"})
```


## Current State

See https://www.salesforce.com/us/developer/docs/api_rest/
Expand Down
14 changes: 9 additions & 5 deletions lib/forcex/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ defmodule Forcex.Client do
Forcex.Client.login
|> Forcex.Client.locate_services
"""
def login(c \\ config) do
def login(c \\ default_config) do
login(c, %__MODULE__{})
end

def login(conf, starting_struct) do
login_payload =
c
|> Map.put(:password, "#{c.password}#{c.security_token}")
conf
|> Map.put(:password, "#{conf.password}#{conf.security_token}")
|> Map.put(:grant_type, "password")
Forcex.post("/services/oauth2/token?#{URI.encode_query(login_payload)}", %__MODULE__{})
Forcex.post("/services/oauth2/token?#{URI.encode_query(login_payload)}", starting_struct)
|> handle_login_response
end

Expand All @@ -66,7 +70,7 @@ defmodule Forcex.Client do
%__MODULE__{}
end

defp config do
def default_config do
[:username, :password, :security_token, :client_id, :client_secret]
|> Enum.map(&( {&1, get_val_from_env(&1)}))
|> Enum.into(%{})
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Forcex.Mixfile do
def project do
[
app: :forcex,
version: "0.4.0",
version: "0.4.1",
elixir: "~> 1.0",
name: "Forcex",
description: @description,
Expand Down

0 comments on commit cd8b4d2

Please sign in to comment.