ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API:...

42
John Foster ArcGIS REST API Getting Started

Transcript of ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API:...

Page 1: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

John Foster

ArcGIS REST APIGetting Started

Page 2: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

ArcGIS REST API

• Who is this talk talking to?• What are we going to cover in this talk?• This is intro + getting started

• Also check out: ArcGIS REST API: Advanced Techniques- Wed 2/12 1:30 PM 102B

Presenter
Presentation Notes
Sample the audience to understand who we are talking to
Page 3: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

• ArcGIS Online, MapViewer• Storymaps, Operations Dashboard, Insights• SDKs: JavaScript, Runtime, Python• ArcGIS Hub• Developer’s website• ArcGIS Pro• …and many others

Who is using REST?

Have portal, will REST

Page 4: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

What is REST?

• REST == REpresentational State Transfer

• For distributed hypermedia systems (HTTP/HTTPS)

• First presented by Roy Fielding in 2000 • See https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

Page 5: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

• URI “endpoint”• operations, or methods:

- GET- POST- PUT- PATCH- DELETE

• parameters• headers• response status code• response

HTTP/S protocol

https://developer.mozilla.org/en-US/docs/Web/HTTP

Presenter
Presentation Notes
URI = Uniform Resource Identifier Show the mozilla page
Page 6: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

REST in action

• Let’s look at some REST

Presenter
Presentation Notes
Switch to Chrome and use devtools. Show ArcGIS Online and Developers Dashboard https://developers.arcgis.com/dashboard https://intern-hackathon.maps.arcgis.com/home/content.html?view=table&sortOrder=desc&sortField=modified&folder=JohnFoster#content
Page 7: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Examplehttps://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?f=json&SingleLine=Washington%20Convention%20Center%2C%20Washington%20DC&category=POI&outFields=*&forStorage=false

Presenter
Presentation Notes
https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?f=json&SingleLine=Washington%20Convention%20Center%2C%20Washington%20DC&category=POI&outFields=*&forStorage=false
Page 8: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

ExampleEndpoint (URI):https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates

Parameters:f=jsonSingleLine=Washington%20Convention%20Center%2C%20Washington%20DCcategory=POIoutFields=*forStorage=false

Presenter
Presentation Notes
Switch to FIREFOX and use devtools. https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?f=json&SingleLine=Washington%20Convention%20Center%2C%20Washington%20DC&category=POI&outFields=*&forStorage=false
Page 9: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

REST in action

• Let’s try a geocode

Presenter
Presentation Notes
Run a Geocode in the browser https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?f=json&SingleLine=Washington%20Convention%20Center%2C%20Washington%20DC&category=POI&outFields=*&forStorage=false Run Geocode on the command line: curl "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?f=json&SingleLine=Washington%20Convention%20Center%2C%20Washington%20DC&category=POI&outFields=*&forStorage=false" Run a Geocode in a shell script sh ./curltest.sh Washington%20Convention%20Center%2C%20Washington%20DC
Page 10: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

REST in action

• Let’s try a geocode – type this into a browser:

https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?f=json&SingleLine=Washington%20Convention%20Center%2C%20Washington%20DC&category=POI&outFields=*&forStorage=false

Presenter
Presentation Notes
Run a Geocode in the browser https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?f=json&SingleLine=Washington%20Convention%20Center%2C%20Washington%20DC&category=POI&outFields=*&forStorage=false Run Geocode on the command line: curl "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?f=json&SingleLine=Washington%20Convention%20Center%2C%20Washington%20DC&category=POI&outFields=*&forStorage=false" Run a Geocode in a shell script sh ./curltest.sh Washington%20Convention%20Center%2C%20Washington%20DC
Page 11: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

REST in action

• Let’s try a geocode – create an HTML page:

<form method="post"action="https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates"><label for="SingleLine">Point of interest:</label><input type="text"

name="SingleLine"><input type="hidden" name="category" value="POI"><input type="hidden" name="outFields" value="*"><input type="hidden" name="f" value="json"><input type="submit" value="Post">

</form>

Presenter
Presentation Notes
Run a Geocode in the browser but using a form Show demo page http://localhost/rest/postform.html
Page 12: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

REST in action

• Let’s try a geocode – create a shell script:

#!/bin/bash##################################################protocol="https://"host="geocode.arcgis.com"endpoint="/arcgis/rest/services/World/GeocodeServer/findAddressCandidates"url="${protocol}${host}${endpoint}"parameters="f=json&category=POI&outFields=*&SingleLine=${1}"echo "${url}?${parameters}"curl "${url}?${parameters}"

Presenter
Presentation Notes
Run shell script from Visual Studio Code
Page 13: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

REST in action

• Let’s try a feature service – type this on a command line:

curl -G "https://services8.arcgis.com/LLNIdHmmdjO2qQ5q/arcgis/rest/admin/services/ufo_sightings_de/FeatureServer?f=json"

Page 14: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Get a token

• Developer dashboard: https://developers.arcgis.com/dashboard

• By script: https://developers.arcgis.com/labs/rest/get-an-access-token/

Presenter
Presentation Notes
Get a token at https://developers.arcgis.com/dashboard Tutorial: https://developers.arcgis.com/labs/rest/get-an-access-token/
Page 15: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

REST in action

• Let’s try a feature service – type this on a command line:

curl -G "https://services8.arcgis.com/LLNIdHmmdjO2qQ5q/arcgis/rest/admin/services/ufo_sightings_de/FeatureServer?f=json&token="

Presenter
Presentation Notes
Get a token at https://developers.arcgis.com/dashboard Tutorial: https://developers.arcgis.com/labs/rest/get-an-access-token/
Page 16: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Pitfalls!• cURL only gets you so far• Very inflexible• Not handling parameters, auth, errors• Writing lots of (bad) code

Page 17: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Postman• https://www.getpostman.com/downloads/

Presenter
Presentation Notes
Open Postman and show the ArcGIS Collection
Page 18: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Postman• ArcGIS tutorials:

https://developers.arcgis.com/labs/browse/?product=rest-api&topic=any

• ArcGIS REST API: https://github.com/esri-es/ArcGIS-REST-API

Presenter
Presentation Notes
Open Postman and show the ArcGIS Collection
Page 19: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Postman• https://www.getpostman.com/downloads/• Great for managing collections of endpoints• Great for testing endpoints, API development• Not great for building an app

Presenter
Presentation Notes
Open Postman and show the ArcGIS Collection
Page 20: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

JavaScript• fairly easy to encode REST API calls with JavaScript• XMLHttpRequest• fetch• jquery• 3rd party libs, npm

Page 21: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

JavaScript• XMLHttpRequest• https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

Page 22: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

JavaScript// construct the url yourself and don't forget to tack on f=jsonconst url = "https://www.arcgis.com/sharing/rest/community/users/dmfenton";url += "?f=json";var xhr = new XMLHttpRequest();xhr.onreadystatechange = function() {

if (xhr.readyState == XMLHttpRequest.DONE) {// make sure JSON response doesn’t indicate an errorif (!xhr.responseText.error) {xhr.responseText; //{firstName:"Daniel",description:”I’m a dev"...}

}}

}xhr.open('GET', url, true);xhr.send(null);

Presenter
Presentation Notes
Demo the xmlhttprequest.html from localhost
Page 23: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

JavaScript• fetch• https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

Page 24: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

JavaScriptconst url = "https://www.arcgis.com/sharing/rest/community/users/dmfenton";fetch(url, {method: "POST", // set the request typeheaders: {"Content-Type": "application/x-www-form-urlencoded" // append the right header

},// concat and encode parameters, append f=jsonbody: encodeURIComponent("f=json")

}).then(response => {if (response.ok) { return response.json() } // dig out the json

}).then(response => {// trap for errors inside a 200 responseif (!response.error) { return response; }})

Presenter
Presentation Notes
Demo the fetchapp.html from localhost
Page 25: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Many issues handling all this on your own:

• Lots of boiler plate code 🤕🤕• What are all the error codes? 🤷🤷🤷♀

• How do you handle authentication? 🤷🤷🤷♀

• How are dates supposed to be encoded? 🤷🤷🤷♀

• Proper encoding for objects? 🤷🤷🤷♀

• How do you manage tokens for federated servers? 🤷🤷🤷♀

• Refreshing authentication when necessary? 🤕🤕

Managing complexity

Presenter
Presentation Notes
Go to https://developers.arcgis.com/rest/
Page 26: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Is there a better way?

Page 27: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

ArcGIS REST JS

@esri/arcgis-rest-js helps you

talk to ArcGIS Online and Enterprise

from modern browsers and Node.js.

Page 28: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

ArcGIS REST JS

kind of analogous to ArcGIS API for Python

much different than the ArcGIS API for JavaScript

different goals and use cases

Page 29: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

ArcGIS REST JS

Open source on GitHub

Code 🎛🎛 https://github.com/Esri/arcgis-rest-js

Doc 📚📚 https://esri.github.io/arcgis-rest-js

Presenter
Presentation Notes
* API reference is generated from comments within the code * Guides * its an open source project * pull requests (suggestions, improvements) welcome
Page 30: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

ArcGIS REST JS simplifiesimport { request } from "@esri/arcgis-rest-request";

request(url).then(response) // {firstName:"Daniel",description:"open source geodev" ... }.catch((error => {if(err.name === "ArcGISAuthError"){// handle and auth error

} else {// handle a regular error

}})

Presenter
Presentation Notes
Code is much simpler Handling errors more clear
Page 31: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

• Knows how to converse with ArcGIS portals- Authentication, token management- f=json - parameter encoding- uses FormData when required- clear and informative error handling

• but, not for- map and layer rendering- client-side analysis

ArcGIS REST JS value adds

Page 32: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Pass-through parameters// url, IRequestOptionsrequest(url, {

params: { // anything you want to pass to the endpointfoo: true,bar: "baz",attachment: File(),num: 999,when: Date().now() // etc.

},// httpMethod: "GET",// authentication// portal,// headers,// fetch})

Presenter
Presentation Notes
IRequestOptions give you more control over the request authentication helps you generate tokens when you cant make an anonymous request a custom Fetch implementation can be passed in too
Page 33: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Request is the foundationimport { geocode } from "@esri/arcgis-rest-geocoder";

// assumes you want to use ArcGIS Onlinegeocode("Washington Convention Center, Washington DC").then(response) // { ... candidates: [] }

// IRequestOptions is still availablegeocode("Washington Convention Center, Washington DC", {params: {

category: "POI",outFields: "*"

},authentication})

Presenter
Presentation Notes
the rest of the API builds on top of `request`
Page 34: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

ArcGIS REST JS Goals• Ease the use of ArcGIS REST endpoints• Node.js and (modern) browsers• a la carte services• framework agnostic• align with JS ecosystem

Page 35: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

ArcGIS REST JS Disclaimer• not a product, no roadmap• not a product, no official support• work in progress, compare https://developers.arcgis.com/rest/• open source, contributions welcome!

Page 36: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Demo

Presenter
Presentation Notes
https://github.com/Esri/arcgis-rest-js/tree/master/demos/node-cli-item-management/
Page 37: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Location services- Geocode- Routing- Demographics- Analysis- Elevation- Packaging

What services are available?

ArcGIS Online and Enterprise- Users- Groups- Items

ArcGIS Enterprise- Server- Portal- Notebooks- Extensions

https://developers.arcgis.com/rest/

Presenter
Presentation Notes
Go to https://developers.arcgis.com/rest/
Page 38: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Resources• ArcGIS REST API https://developers.arcgis.com/rest/

• arcgis-rest-js code https://github.com/Esri/arcgis-rest-js

• arcgis-rest-js documentation https://esri.github.io/arcgis-rest-js

• Hosted Feature Service resource https://github.com/esri-es/ArcGIS-REST-

API/tree/master/HostedFeatureServices

• HTTP: https://developer.mozilla.org/en-US/docs/Web/HTTP

Page 39: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Resources• ArcGIS REST API https://developers.arcgis.com/rest/

• arcgis-rest-js code https://github.com/Esri/arcgis-rest-js

• arcgis-rest-js documentation https://esri.github.io/arcgis-rest-js

• Hosted Feature Service resource https://github.com/esri-es/ArcGIS-REST-

API/tree/master/HostedFeatureServices

• HTTP: https://developer.mozilla.org/en-US/docs/Web/HTTP

• cURL: https://curl.haxx.se/

Page 40: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Questions?

Page 41: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Print Your Certificate of Attendance

Print Stations Located in 150 Concourse Lobby

Tuesday12:30 pm – 6:30 pmExpoHall B

5:15 pm – 6:30 pmExpo SocialHall B

Wednesday10:45 am – 5:15 pmExpoHall B

6:30 pm – 9:30 pmNetworking ReceptionSmithsonian National Museumof Natural History

Page 42: ArcGIS REST API Getting StartedThis is intro + getting started • Also check out: ArcGIS REST API: Advanced Techniques-Wed 2/12 1:30 PM 102B Sample the audience to understand who

Download the EsriEvents app and find

your event

Select the session you attended

Scroll down to “Survey”

Log in to access the survey

Complete the survey and select “Submit”

Please Share Your Feedback in the App

Presenter
Presentation Notes
Guidance: This is the survey slide. You can use the example as is or you can screenshot your session.