Skip to content

Commit

Permalink
feat: add making first request article
Browse files Browse the repository at this point in the history
Resolves: none.
  • Loading branch information
loay-ashraf committed Jan 27, 2024
1 parent eddda29 commit 112cc5d
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 21 deletions.
25 changes: 10 additions & 15 deletions Docs.docc/Articles/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ You also need to create ``Session`` and ``RESTClient`` objects so you can start

### Adding RxNetworkKit to your project

In this section, you will be adding RxNetworkKit to our project via Swift Package Manager.
In this section, you will be adding RxNetworkKit to your project via Swift Package Manager.

- First, open the dependencies tab of your project.

Expand All @@ -32,17 +32,15 @@ In this section, you will be adding RxNetworkKit to our project via Swift Packag

![Xcode has finished resolving the package graph and RxNetworkKit can be used in the project.](article-getting-started-#3.png)

### Adding required networking code to your project
### Adding required networking code

In this section, you will be creating ``Session`` and ``RESTClient`` objects.

- First, add a new Swift file to your project and name it *RequestInterceptor.swift*.

![RequestInyterceptor.swift is open and empty.](article-getting-started-#4.png)

- Second, add an import statement for **RxNetworkKit** to the top of the newly created file.

- Add a new class with the name *RequestInterceptor* and conforms to the ``HTTPRequestInterceptor`` protocol as done below:
- Add a new class with the name `RequestInterceptor` that conforms to the ``HTTPRequestInterceptor`` protocol as done below:

```swift
import Foundation
Expand All @@ -69,9 +67,7 @@ class RequestInterceptor: HTTPRequestInterceptor {
}
```

![RequestInyterceptor.swift is open and contains RequestInterceptor class.](article-getting-started-#5.png)

- Finally, add an import statement for **RxNetworkKit** in *ViewController.swift* file and create ``SessionConfiguration``, ``Session`` and ``RESTClient`` objects in the *viewDidLoad* method as done below:
- Finally, add an import statement for **RxNetworkKit** in *ViewController.swift* file and create ``SessionConfiguration``, ``Session`` and ``RESTClient`` objects in the `viewDidLoad` method as done below:

```swift
import UIKit
Expand All @@ -91,9 +87,7 @@ class ViewController: UIViewController {
}
```

![RequestInyterceptor.swift is open and contains RequestInterceptor class.](article-getting-started-#6.png)

- Alternatively, you can override the default ``SessionConfiguration`` by creating your own instance and providing different property values as done below:.
- Alternatively, you can override the default ``SessionConfiguration`` by creating your own instance and providing different property values as done below:

```swift
import UIKit
Expand All @@ -115,10 +109,11 @@ class ViewController: UIViewController {
}
```

![RequestInyterceptor.swift is open and contains RequestInterceptor class.](article-getting-started-#7.png)

## Conclusion

Now you are one step away from making requests using **RxNetworkKit** in your project.
Now you are one step away from making a request using **RxNetworkKit**.

In the next article, you will learn how to create a ``HTTPRequestRouter`` and a `Decodable` model and use them with ``RESTClient`` to make a request.

In the next article, you will learn how to create a ``HTTPRequestRouter`` and use it with ``RESTClient`` to make requests.
You can read the next article here:
- <doc:MakingFirstRequest>
147 changes: 147 additions & 0 deletions Docs.docc/Articles/MakingFirstRequest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# Making Your First Request

Make your first request with **RxNetworkKit**

## Overview

You will need to add a ``HTTPRequestRouter`` so that ``RESTClient`` can map api endpoints to a `URLRequest` and make the actual request.

### Adding a request router

In this section, you will be adding a ``HTTPRequestRouter``.

- First, add a new Swift file to your project and name it *RequestRouter.swift*.

- Second, add an import statement for **RxNetworkKit** to the top of the newly created file.

- Finally, add a new enumeration with the name `RequestRouter` that conforms to the ``HTTPRequestRouter`` protocol as done below:

```swift
import Foundation
import RxNetworkKit

enum RequestRouter: HTTPRequestRouter {

case `default`

var scheme: HTTPScheme {
.https // Replace with your endpoint http scheme
}

var method: HTTPMethod {
.get // Replace with your endpoint http method
}

var domain: String {
"domain.com" // Replace with your endpoint domain
}

var path: String {
"path" // Replace with your endpoint path
}

var headers: [String : String] {
[:] // Replace with your endpoint http headers
}

var parameters: [String : String]? {
nil // Replace with your endpoint query parameters
}

var body: [String : Any]? {
nil // Replace with your endpoint http body
}

}
```

- Tip: You can group related api endpoints in a single enumeration with dependencies (if any) as case associated values.

### Adding a model

In this section, you will be adding a `Decodable` model which will be a representation of the response body.

- First, add a new Swift file to your project and name it *Model.swift*.

- Second, add a structure with name `Model` that conforms to `Decodable` that matches the expected response body as done below:

```swift
import Foundation

struct Model: Decodable {

let id: Int
let avatarURL: URL
let htmlURL: URL
let login: String

enum CodingKeys: String, CodingKey {
case id
case avatarURL = "avatar_url"
case htmlURL = "html_url"
case login
}

}
```

- Now, you are ready to make a request using the ``HTTPRequestRouter`` and the `Decodable` model you created.

### Making a request

In this section, you will be using the ``HTTPRequestRouter`` you created in the previous section with ``RESTClient`` to make a request.

- First, go to *ViewController.swift* file and add an import statement for `RxSwift` to the top of the file.

- Second, create a dispose bag at the top of `ViewController` class.

- Create a `RequestRouter` in the `viewDidLoad` method.

- Call the `RESTClient.request` method and pass the `RequestRouter` as an argument.

- Subscribe to the output `Observable` as done below:

```swift
import UIKit
import RxSwift
import RxNetworkKit

class ViewController: UIViewController {

let disposeBag: DisposeBag = .init()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let sessionConfiguration = SessionConfiguration.default
let session = Session(configuration: sessionConfiguration)
let requestInterceptor = RequestInterceptor()
let restClient = RESTClient(session: session, requestInterceptor: requestInterceptor)
let requestRouter = RequestRouter.default
restClient.request(router: requestRouter)
.subscribe(onSuccess: { (model: Model) in
// dump the received response body
dump(model)
}, onError: { error in
// print the error description (if any)
print(error.localizedDescription)
})
.disposed(by: disposeBag)
}

}
```

- That's it, you made your first request.

- Tip: If you expect an empty response for the request you are making, you should subscribe to the `onCompleted` event instead of the `onSuccess` event.

- Note: It is best to apply an architectural pattern (like MVVM, MVP, VIPER, etc.) rather than making requests in the `ViewController` directly, but that is not done in the above example for the sake of simplicity.

- Warning: If you intend to make updates to the UI, you must use the `observe(on: MainScheduler.instance)` operator to avoid updating the UI on a background thread (which may lead to an unexpected behavior or a crash).

## Conclusion

Now, you are all set and can use **RxNetworkKit** to make requests.

You can check the other articles for more advanced usage.
14 changes: 8 additions & 6 deletions Docs.docc/Pages/RxNetworkKit.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,27 @@ a reactive networking framework based on URLSession and RxSwift.

RxNetworkKit is a generic reactive networking framework that leverages the stability and reliability of both URLSession and RxSwift.

### Why RxNetworkKit?
### RxNetwrkKit can be used to:

- can be used to make simple REST API calls.
- can be used to make download and upload requests while also tracking the progress.
- can be used to connect to websocket and listen to remote data changes.
- can intercept requests for adaptation or retry on failure.
- can observe and provide network reachability status.
- Make simple REST API calls.
- Make download and upload requests while also tracking the progress.
- Connect to websocket and listen to remote data changes.
- Intercept requests for adaptation or retry on failure.
- Observe network reachability status.

## Featured

@Links(visualStyle: detailedGrid) {
- <doc:GettingStarted>
- <doc:MakingFirstRequest>
}

## Topics

### Essentials

- <doc:GettingStarted>
- <doc:MakingFirstRequest>

### Foundation

Expand Down

0 comments on commit 112cc5d

Please sign in to comment.