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

Post on 12-Aug-2020

7 views 0 download

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

John Foster

ArcGIS REST APIGetting Started

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

• 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

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

• 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

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

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

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

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

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

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

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

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"

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/

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/

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

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

Presenter
Presentation Notes
Open Postman and show the ArcGIS Collection

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

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

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

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

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

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

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

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/

Is there a better way?

ArcGIS REST JS

@esri/arcgis-rest-js helps you

talk to ArcGIS Online and Enterprise

from modern browsers and Node.js.

ArcGIS REST JS

kind of analogous to ArcGIS API for Python

much different than the ArcGIS API for JavaScript

different goals and use cases

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

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

• 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

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

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`

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

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!

Demo

Presenter
Presentation Notes
https://github.com/Esri/arcgis-rest-js/tree/master/demos/node-cli-item-management/

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/

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

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/

Questions?

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

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.