<commit_before>#### Important things to know before you begin coding

There are some very important things to know before you begin coding network tasks:

**Basic knowledge of HTTP is required.** You must know what a ‘request’ is, and what a ‘response’ is. These are the fundamental building blocks in an app that uses the network.

**Basic knowledge of web services is required.** You must know what a ‘web service’ is, and how to use one. In this course, you do not have to code/create a web service – you simply have to use one. You must also be able to understand and use [JSON](http://json.org).

**Network operations are asynchronous.** That means that we do not know _if_ or _when_ our request will be responded to.

**You must learn something about closures.** A closure in Swift is an executable code object. Network operations use closures.

**Simple tasks are easy to code, but complex tasks require more study.** Simple tasks include ‘get one’ and ‘get all’ from a resource. More complex tasks include HTTP POST, data modifications, authentication, and so on.

#### Getting started, hands-on

This week, you will use the Project_Templates/WebServiceModel template to create a simple app that uses a web service.

Download the template from the [GitHub code example repository](../Project_Templates/WebServiceModel/). Then, perform the project-rename task.

We will use a public web service that your professor created recently. It is here:

http://<fill in>.azurewebsites.net/api

The web service is designed to be self-documenting, so you can discover its data and permitted operations.

By itself, a web browser is not a good tool to use to inspect a web service. Instead, use this web app:

[http://jsonformatter.curiousconcept.com](http://jsonformatter.curiousconcept.com)

Enter a resource URI in its “JSON Data Url” field, and click its “Process” button. The response to each request will be displayed in its own grey-bordered box. Try it with these URIs:

http://<fill in>azurewebsites.net/api/programs

[https://itunes.apple.com/search?term=big+bang+theory&entity=tvEpisode&limit=10&sort=recent](https://itunes.apple.com/search?term=big+bang+theory&entity=tvEpisode&limit=10&sort=recent)


#### Composing a network request, and handling the response

In iOS (and OS X), we use a ‘task’ object to compose a network request, and handle the response.

The ‘task’ object is an instance of [NSURLSessionDataTask](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionDataTask_class/Reference/Reference.html). It uses a ‘session’ object, and a ‘request’ object.

The ‘session’ object is an instance of [NSURLSession](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/Introduction/Introduction.html), which represents a logical session between your app and a web service. A ‘session’ object must be configured, using a ‘session configuration’ object (which is an instance of [NSURLSessionConfiguration](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionConfiguration_class/Reference/Reference.html)).

The ‘request’ object is an instance of [NSMutableURLRequest](https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSMutableURLRequest_Class/Reference/Reference.html), which needs a ‘URL’ object, and can be configured with request headers if necessary.

In summary, a ‘task’ object relies on the presence of a number of other initialized and configured objects.

#### Configuring and executing the ‘task’ object

When a ‘task’ object is created, it does not immediately execute. You must execute it, when you are ready, by sending the ‘resume’ message to the object. The ‘task’ object executes in the background, so it does not impair the responsiveness of your app’s user interface.

More important is the configuration of the ‘task’ object: You must write a _block of code_ that will run when the task completes execution.

This _block of code_ is known as a _closure_ in Swift. Think of a _closure_ as an _inline function_. (You have likely worked with similar constructs in the past, including JavaScript functions (and closures), and C# lambda expressions.)

The _closure_ is defined on the ‘task’ object’s _completionHandler_ parameter. It exposes these values:

*   The data returned in the response body
*   Metadata about the response
*   If necessary, error information

The data type of the data returned in the response body is NSData. Therefore, we must transform it into the format we expect. In this course, we plan to work with web services that use JSON, so we will [transform](https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html) the NSData object to JSON and then to an array or dictionary, as appropriate for the request.

#### Code example

See the “ICT WS v1” code example on the GitHub code example repository, in the Week_06 folder.
<commit_msg>Update monday.md<commit_after>#### Important things to know before you begin coding

There are some very important things to know before you begin coding network tasks:

**Basic knowledge of HTTP is required.** You must know what a ‘request’ is, and what a ‘response’ is. These are the fundamental building blocks in an app that uses the network.

**Basic knowledge of web services is required.** You must know what a ‘web service’ is, and how to use one. In this course, you do not have to code/create a web service – you simply have to use one. You must also be able to understand and use [JSON](http://json.org).

**Network operations are asynchronous.** That means that we do not know _if_ or _when_ our request will be responded to.

**You must learn something about closures.** A closure in Swift is an executable code object. Network operations use closures.

**Simple tasks are easy to code, but complex tasks require more study.** Simple tasks include ‘get one’ and ‘get all’ from a resource. More complex tasks include HTTP POST, data modifications, authentication, and so on.

#### Getting started, hands-on

This week, you will use the Project_Templates/WebServiceModel template to create a simple app that uses a web service.

Download the template from the [GitHub code example repository](../Project_Templates/WebServiceModel/). Then, perform the project-rename task.

We will use a public web service that your professor created recently. It is here:

http://<fill in>.azurewebsites.net/api

The web service is designed to be self-documenting, so you can discover its data and permitted operations.

By itself, a web browser is not a good tool to use to inspect a web service. Instead, use this web app:

[http://jsonformatter.curiousconcept.com](http://jsonformatter.curiousconcept.com)

Enter a resource URI in its “JSON Data Url” field, and click its “Process” button. The response to each request will be displayed in its own grey-bordered box. Try it with these URIs:

http://<fill in>azurewebsites.net/api/programs

[https://itunes.apple.com/search?term=big+bang+theory&entity=tvEpisode&limit=10&sort=recent](https://itunes.apple.com/search?term=big+bang+theory&entity=tvEpisode&limit=10&sort=recent)


#### Composing a network request, and handling the response

In iOS (and OS X), we use a ‘task’ object to compose a network request, and handle the response.

The ‘task’ object is an instance of [NSURLSessionDataTask](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionDataTask_class/Reference/Reference.html). It uses a ‘session’ object, and a ‘request’ object.

The ‘session’ object is an instance of [NSURLSession](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/Introduction/Introduction.html), which represents a logical session between your app and a web service. A ‘session’ object must be configured, using a ‘session configuration’ object (which is an instance of [NSURLSessionConfiguration](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionConfiguration_class/Reference/Reference.html)).

The ‘request’ object is an instance of [NSMutableURLRequest](https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSMutableURLRequest_Class/Reference/Reference.html), which needs a ‘URL’ object, and can be configured with request headers if necessary.

In summary, a ‘task’ object relies on the presence of a number of other initialized and configured objects.

#### Configuring and executing the ‘task’ object

When a ‘task’ object is created, it does not immediately execute. You must execute it, when you are ready, by sending the ‘resume’ message to the object. The ‘task’ object executes in the background, so it does not impair the responsiveness of your app’s user interface.

More important is the configuration of the ‘task’ object: You must write a _block of code_ that will run when the task completes execution.

This _block of code_ is known as a _closure_ in Swift. Think of a _closure_ as an _inline function_. (You have likely worked with similar constructs in the past, including JavaScript functions (and closures), and C# lambda expressions.)

The _closure_ is defined on the ‘task’ object’s _completionHandler_ parameter. It exposes these values:

*   The data returned in the response body
*   Metadata about the response
*   If necessary, error information

The data type of the data returned in the response body is NSData. Therefore, we must transform it into the format we expect. In this course, we plan to work with web services that use JSON, so we will [transform](https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html) the NSData object to JSON and then to an array or dictionary, as appropriate for the request.

#### Code example

See the <fill in> code example on the GitHub code example repository, in the Week_08 folder.

{'fraction_non_alphanumeric': 0.07398901647528706, 'fraction_numerical': 0.0012980529206190715, 'mean_word_length': 4.940688018979834, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 14, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4857211', 'n_tokens_mistral': 2574, 'n_tokens_neox': 2488, 'n_words': 1290}