Skip to content

Instantly share code, notes, and snippets.

@wardi
Last active January 6, 2020 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wardi/0e3f20e492734dc53c2037dc768cc16f to your computer and use it in GitHub Desktop.
Save wardi/0e3f20e492734dc53c2037dc768cc16f to your computer and use it in GitHub Desktop.

Retrieve Records

Access this data through the CKAN Datastore API with resource_id="b076ced6-ef48-45b8-8db2-dd7d27c11fe2"

The CKAN API uses a JSON-RPC style, where you post a JSON object and receive another JSON object in response.

Retrieving records requires an API key. Your API key is shown on your profile page.

Do not include your API key in any code shared with other people. Use a configuration file or environment variable to let each user input their own key when they use your tool. Your API key is equivalent to your password and may be used to perform any action your user can on this site. All actions made with your API key will be logged as actions you performed. Example:

$json = @'
{
  "resource_id": "b076ced6-ef48-45b8-8db2-dd7d27c11fe2",
  "sort": "agreement_date desc",
  "limit": 10,
  "filters": {
    "recipient_province": "ON"
  }
}
'@
$response = Invoke-RestMethod https://registry.open.canada.ca/api/action/datastore_search `
  -Method Post -Body $json -Headers @{"Authorization"="$API_KEY"}
$response.result.records

Create and Update Records

Create and update records with the "datastore_upsert" endpoint. Example:

$json = @'
{
  "resource_id": "b076ced6-ef48-45b8-8db2-dd7d27c11fe2",
  "records": [{
    "additional_information_en": "Recipient reallocates or redistributes grant or contribution awarded to third-party beneficiaries", 
    "additional_information_fr": "Le bénéficiaire réaffecte ou redistribue la subvention ou la contribution octroyée à des tiers", 
    "agreement_end_date": "2020-04-30", 
    "agreement_number": "F1580-140737", 
    "agreement_start_date": "2019-04-01", 
    "agreement_title_en": "Building better buildings", 
    "agreement_title_fr": "Construire des meilleurs bâtiments", 
    "agreement_type": "G", 
    "agreement_value": "500000", 
    "amendment_date": "2018-04-01", 
    "amendment_number": "3", 
    "coverage": "Canada; Mexico|Mexique; United States|États-Unis", 
    "description_en": "Developing models and other tools to predict the safety performance of buildings.", 
    "description_fr": "Met au point des modèles et d'autres outils pour prévoir la sécurité des bâtiments.", 
    "expected_results_en": "Develop highly qualified personnel available to pursue various careers within industry, academia, government and other sectors of the economy.", 
    "expected_results_fr": "Développé un personnel hautement qualifié prêt à mener une carrière variée au sein de l’industrie, du milieu académique, de la fonction publique ou dans d’autres secteurs de l’économie.", 
    "federal_riding_name_en": "Winnipeg South Centre", 
    "federal_riding_name_fr": "Winnipeg-Centre-Sud", 
    "federal_riding_number": "46014", 
    "foreign_currency_type": "USD", 
    "foreign_currency_value": "450000", 
    "naics_identifier": "611710", 
    "prog_name_en": "Canada Granting Program", 
    "prog_name_fr": "Le programme de subvention du Canada", 
    "prog_purpose_en": "The Canada Granting Program encourage Canadians to learn about Canada’s geography, history, and economy. The Program provides networking opportunities to enhance recipients’ understanding of Canada.", 
    "prog_purpose_fr": "Le programme de subventions du Canada incite les Canadiens à se renseigner sur la géographie, l’histoire et l’économie du Canada. Le programme offre des opportunités de réseautage pour aider les bénéficiaires d’améliorer leur compréhension du Canada.", 
    "recipient_business_number": "123456789", 
    "recipient_city": "Grand Falls|Grand-Sault", 
    "recipient_country": "CA", 
    "recipient_legal_name": "ACME Services Limited|Services ACME Limitée", 
    "recipient_operating_name": "ACME", 
    "recipient_postal_code": "A1A 1A1", 
    "recipient_province": "ON", 
    "recipient_type": "G", 
    "ref_number": "001-2018-2019-Q2-00045", 
    "research_organization_name": "York University|Université York"
  }]
}
'@
$response = Invoke-RestMethod https://registry.open.canada.ca/api/action/datastore_upsert `
  -Method Post -Body $json -Headers @{"Authorization"="$API_KEY"}

Delete Records

First verify that the record you would like to remove is present with the "datastore_search" endpoint

Example:

$json = @'
{
  "resource_id": "b076ced6-ef48-45b8-8db2-dd7d27c11fe2",
  "records": [{
    "amendment_number": "0", 
    "ref_number": "001-2018-2019-Q2-00045"
  }]
}
'@
$response = Invoke-RestMethod https://registry.open.canada.ca/api/action/datastore_search `
  -Method Post -Body $json -Headers @{"Authorization"="$API_KEY"}
$response.result.records

Remove the record returned by passing the same parameters to the "datastore_delete" endpoint instead of "datastore_search". Example:

$response = Invoke-RestMethod https://registry.open.canada.ca/api/action/datastore_delete `
  -Method Post -Body $json -Headers @{"Authorization"="$API_KEY"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment