Socrata v2.0.0 Socrata.Client View Source

The Client is the main interface of the library. Using a Client, you can get the records and metadata for a data set.

The Client accepts optional paramaters default_format and app_token. These values can either be configured when you call new/3 or in your application’s config/config.exs file:

config :socrata,
  default_format: "json",
  app_token: "blah blah blah"

For more information about tokens and their use, see the Socrata App Tokens docs.

Link to this section Summary

Functions

Gets the response object from Socrata.Client.get_records/2 or raises an error

Gets the records for a data set

Gets the response object from Socrata.Client.get_view/2 or raises an error

Gets the view information (metadata) for a data set

Link to this section Types

Link to this section Functions

Link to this function get_records!(query, opts \\ []) View Source
get_records!(Socrata.Query.t(), keyword()) :: response!()

Gets the response object from Socrata.Client.get_records/2 or raises an error.

Link to this function get_records(query, opts \\ []) View Source
get_records(Socrata.Query.t(), keyword()) :: response()

Gets the records for a data set.

Options

There are two keys that the function looks for in the opts argument:

  • app_token is used to configure the X-App-Token header if the value was either not set in the application config or override it for this call.
  • format is used to specify the response content type — this defaults to "json".

The remainder of opts is passed directly to HTTPoison.get!/3 so you can control the request/response life cycle.

Examples

  iex> # get regular json response
  iex> alias Socrata.{Client, Query}
  iex> query = Query.new("yama-9had", "data.cityofchicago.org") |> Query.limit(2)
  iex> {:ok, %HTTPoison.Response{body: body}} = Client.get_records(query)
  iex> records = Jason.decode!(body)
  iex> length(records)
  2

  iex> # get csv response
  iex> alias Socrata.{Client, Query}
  iex> query = Query.new("yama-9had", "data.cityofchicago.org") |> Query.limit(2)
  iex> {:ok, %HTTPoison.Response{body: body}} = Client.get_records(query, format: "csv")
  iex> {:ok, stream} = StringIO.open(body)
  iex> records = IO.binstream(stream, :line) |> CSV.decode!(headers: true) |> Enum.map(& &1)
  iex> length(records)
  2

  iex> # get tsv response
  iex> alias Socrata.{Client, Query}
  iex> query = Query.new("yama-9had", "data.cityofchicago.org") |> Query.limit(2)
  iex> {:ok, %HTTPoison.Response{body: body}} = Client.get_records(query, format: "tsv")
  iex> {:ok, stream} = StringIO.open(body)
  iex> records = IO.binstream(stream, :line) |> CSV.decode!(separator: ?\t, headers: true) |> Enum.map(& &1)
  iex> length(records)
  2

  iex> # get geojson response
  iex> alias Socrata.{Client, Query}
  iex> query = Query.new("yama-9had", "data.cityofchicago.org") |> Query.limit(2)
  iex> {:ok, %HTTPoison.Response{body: body}} = Client.get_records(query, format: "geojson")
  iex> %{"crs" => _, "type" => "FeatureCollection", "features" => records} = Jason.decode!(body)
  iex> length(records)
  2

  # get an asynchronous response
  iex> alias Socrata.{Client, Query}
  iex> query = Query.new("yama-9had", "data.cityofchicago.org") |> Query.limit(2)
  iex> {:ok, %HTTPoison.AsyncResponse{id: id}} = Client.get_records(query, stream_to: self())
  iex> is_reference(id)
  true
Link to this function get_view!(query, opts \\ []) View Source
get_view!(Socrata.Query.t(), keyword()) :: response!()

Gets the response object from Socrata.Client.get_view/2 or raises an error.

Link to this function get_view(query, opts \\ []) View Source
get_view(Socrata.Query.t(), keyword()) :: response()

Gets the view information (metadata) for a data set.

Options

There is one key that the function looks for in the opts argument:

  • app_token is used to configure the X-App-Token header if the value was either not set in the application config or override it for this call.

The remainder of opts is passed directly to HTTPoison.get!/3 so you can control the request/response life cycle.

Example

iex> alias Socrata.{Client, Query}
iex> q = Query.new(fourby: "yama-9had", domain: "data.cityofchicago.org")
iex> {:ok, %HTTPoison.Response{body: body}} = Client.get_view(q)
iex> details = Jason.decode!(body)
iex> Map.keys(details)
["oid", "publicationAppendEnabled", "category", "numberOfComments", "createdAt", "attribution", "hideFromDataJson", "query", "id", "tableAuthor", "rights", "tableId", "attributionLink", "owner", "viewCount", "grants", "downloadCount", "flags", "publicationGroup", "name", "averageRating", "publicationDate", "hideFromCatalog", "provenance", "totalTimesRated", "description", "metadata", "viewLastModified", "rowsUpdatedAt", "rowsUpdatedBy", "viewType", "newBackend", "publicationStage", "tags", "columns"]