-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
603 lines (536 loc) · 35.3 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
<!DOCTYPE html>
<html lang="en">
<head>
<title>Moya Reference</title>
<link rel="stylesheet" type="text/css" href="css/jazzy.css" />
<link rel="stylesheet" type="text/css" href="css/highlight.css" />
<meta charset='utf-8'>
<script src="js/jquery.min.js" defer></script>
<script src="js/jazzy.js" defer></script>
</head>
<body>
<a title="Moya Reference"></a>
<header>
<div class="content-wrapper">
<p><a href="index.html">Moya Docs</a> (82% documented)</p>
</div>
</header>
<div class="content-wrapper">
<p id="breadcrumbs">
<a href="index.html">Moya Reference</a>
<img id="carat" src="img/carat.png" />
Moya Reference
</p>
</div>
<div class="content-wrapper">
<nav class="sidebar">
<ul class="nav-groups">
<li class="nav-group-name">
<a href="Guides.html">Guides</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="authentication.html">Authentication</a>
</li>
<li class="nav-group-task">
<a href="communityprojects.html">CommunityProjects</a>
</li>
<li class="nav-group-task">
<a href="endpoints.html">Endpoints</a>
</li>
<li class="nav-group-task">
<a href="plugins.html">Plugins</a>
</li>
<li class="nav-group-task">
<a href="providers.html">Providers</a>
</li>
<li class="nav-group-task">
<a href="readme.html">README</a>
</li>
<li class="nav-group-task">
<a href="reactiveswift.html">ReactiveSwift</a>
</li>
<li class="nav-group-task">
<a href="releasing.html">Releasing</a>
</li>
<li class="nav-group-task">
<a href="rxswift.html">RxSwift</a>
</li>
<li class="nav-group-task">
<a href="targets.html">Targets</a>
</li>
<li class="nav-group-task">
<a href="testing.html">Testing</a>
</li>
<li class="nav-group-task">
<a href="threading.html">Threading</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Classes.html">Classes</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Classes/CancellableToken.html">CancellableToken</a>
</li>
<li class="nav-group-task">
<a href="Classes/CredentialsPlugin.html">CredentialsPlugin</a>
</li>
<li class="nav-group-task">
<a href="Classes/Endpoint.html">Endpoint</a>
</li>
<li class="nav-group-task">
<a href="Classes/MoyaProvider.html">MoyaProvider</a>
</li>
<li class="nav-group-task">
<a href="Classes/NetworkActivityPlugin.html">NetworkActivityPlugin</a>
</li>
<li class="nav-group-task">
<a href="Classes/NetworkLoggerPlugin.html">NetworkLoggerPlugin</a>
</li>
<li class="nav-group-task">
<a href="Classes/NetworkLoggerPlugin/Configuration.html">– Configuration</a>
</li>
<li class="nav-group-task">
<a href="Classes/Response.html">Response</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Enums.html">Enumerations</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Enums/AuthorizationType.html">AuthorizationType</a>
</li>
<li class="nav-group-task">
<a href="Enums/EndpointSampleResponse.html">EndpointSampleResponse</a>
</li>
<li class="nav-group-task">
<a href="Enums/MoyaError.html">MoyaError</a>
</li>
<li class="nav-group-task">
<a href="Enums/MultiTarget.html">MultiTarget</a>
</li>
<li class="nav-group-task">
<a href="Enums/NetworkActivityChangeType.html">NetworkActivityChangeType</a>
</li>
<li class="nav-group-task">
<a href="Enums/StubBehavior.html">StubBehavior</a>
</li>
<li class="nav-group-task">
<a href="Enums/Task.html">Task</a>
</li>
<li class="nav-group-task">
<a href="Enums/ValidationType.html">ValidationType</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Extensions.html">Extensions</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Extensions/Method.html">Method</a>
</li>
<li class="nav-group-task">
<a href="Extensions/Request.html">Request</a>
</li>
<li class="nav-group-task">
<a href="Extensions/URL.html">URL</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Functions.html">Functions</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Functions.html#/s:4Moya23convertResponseToResult_7request4data5errors0E0OyAA0C0CAA0A5ErrorOGSo17NSHTTPURLResponseCSg_10Foundation10URLRequestVSgAP4DataVSgs0I0_pSgtF">convertResponseToResult(_:request:data:error:)</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Protocols.html">Protocols</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Protocols/AccessTokenAuthorizable.html">AccessTokenAuthorizable</a>
</li>
<li class="nav-group-task">
<a href="Protocols/Cancellable.html">Cancellable</a>
</li>
<li class="nav-group-task">
<a href="Protocols/MoyaProviderType.html">MoyaProviderType</a>
</li>
<li class="nav-group-task">
<a href="Protocols/PluginType.html">PluginType</a>
</li>
<li class="nav-group-task">
<a href="Protocols/RequestType.html">RequestType</a>
</li>
<li class="nav-group-task">
<a href="Protocols/TargetType.html">TargetType</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Structs.html">Structures</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Structs/AccessTokenPlugin.html">AccessTokenPlugin</a>
</li>
<li class="nav-group-task">
<a href="Structs/MultipartFormData.html">MultipartFormData</a>
</li>
<li class="nav-group-task">
<a href="Structs/MultipartFormData/FormDataProvider.html">– FormDataProvider</a>
</li>
<li class="nav-group-task">
<a href="Structs/ProgressResponse.html">ProgressResponse</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Typealiases.html">Type Aliases</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Typealiases.html#/s:4Moya10Completiona">Completion</a>
</li>
<li class="nav-group-task">
<a href="Typealiases.html#/s:4Moya19DownloadDestinationa">DownloadDestination</a>
</li>
<li class="nav-group-task">
<a href="Typealiases.html#/s:4Moya5Imagea">Image</a>
</li>
<li class="nav-group-task">
<a href="Typealiases.html#/ImageType">ImageType</a>
</li>
<li class="nav-group-task">
<a href="Typealiases.html#/s:4Moya9ImageTypea">ImageType</a>
</li>
<li class="nav-group-task">
<a href="Typealiases.html#/s:4Moya12JSONEncodinga">JSONEncoding</a>
</li>
<li class="nav-group-task">
<a href="Typealiases.html#/s:4Moya6Methoda">Method</a>
</li>
<li class="nav-group-task">
<a href="Typealiases.html#/s:4Moya17ParameterEncodinga">ParameterEncoding</a>
</li>
<li class="nav-group-task">
<a href="Typealiases.html#/s:4Moya13ProgressBlocka">ProgressBlock</a>
</li>
<li class="nav-group-task">
<a href="Typealiases.html#/s:4Moya18RequestInterceptora">RequestInterceptor</a>
</li>
<li class="nav-group-task">
<a href="Typealiases.html#/s:4Moya24RequestMultipartFormDataa">RequestMultipartFormData</a>
</li>
<li class="nav-group-task">
<a href="Typealiases.html#/s:4Moya7Sessiona">Session</a>
</li>
<li class="nav-group-task">
<a href="Typealiases.html#/s:4Moya11URLEncodinga">URLEncoding</a>
</li>
</ul>
</li>
</ul>
</nav>
<article class="main-content">
<section>
<section class="section">
<p align="center">
<img height="160" src="web/logo_github.png" />
</p>
<h1 id='moya-14-0-0' class='heading'>Moya 14.0.0</h1>
<p><a href="https://circleci.com/gh/Moya/Moya/tree/master"><img src="https://img.shields.io/circleci/project/github/Moya/Moya/master.svg" alt="CircleCI"></a>
<a href="https://codecov.io/github/Moya/Moya?branch=master"><img src="https://codecov.io/github/Moya/Moya/coverage.svg?branch=master" alt="codecov.io"></a>
<a href="https://github.com/Carthage/Carthage"><img src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat" alt="Carthage compatible"></a>
<a href="https://github.com/JamitLabs/Accio"><img src="https://img.shields.io/badge/Accio-supported-0A7CF5.svg?style=flat" alt="Accio supported"></a>
<a href="https://cocoapods.org/pods/Moya"><img src="https://img.shields.io/cocoapods/v/Moya.svg" alt="CocoaPods compatible"></a>
<a href="https://github.com/apple/swift-package-manager"><img src="https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg" alt="Swift Package Manager compatible"></a></p>
<p><em>A Chinese version of this document can be found <a href="https://github.com/Moya/Moya/blob/master/Readme_CN.md">here</a>.</em></p>
<p>You’re a smart developer. You probably use <a href="https://github.com/Alamofire/Alamofire">Alamofire</a> to abstract away access to
<code>URLSession</code> and all those nasty details you don’t really care about. But then,
like lots of smart developers, you write ad hoc network abstraction layers. They
are probably called <q>APIManager</q> or <q>NetworkModel</q>, and they always end in tears.</p>
<p><img src="web/diagram.png" alt="Moya Overview"></p>
<p>Ad hoc network layers are common in iOS apps. They’re bad for a few reasons:</p>
<ul>
<li>Makes it hard to write new apps (<q>where do I begin?</q>)</li>
<li>Makes it hard to maintain existing apps (<q>oh my god, this mess…</q>)</li>
<li>Makes it hard to write unit tests (<q>how do I do this again?</q>)</li>
</ul>
<p>So the basic idea of Moya is that we want some network abstraction layer that
sufficiently encapsulates actually calling Alamofire directly. It should be simple
enough that common things are easy, but comprehensive enough that complicated things
are also easy.</p>
<blockquote>
<p>If you use Alamofire to abstract away <code>URLSession</code>, why not use something
to abstract away the nitty gritty of URLs, parameters, etc?</p>
</blockquote>
<p>Some awesome features of Moya:</p>
<ul>
<li>Compile-time checking for correct API endpoint accesses.</li>
<li>Lets you define a clear usage of different endpoints with associated enum values.</li>
<li>Treats test stubs as first-class citizens so unit testing is super-easy.</li>
</ul>
<p>You can check out more about the project direction in the <a href="https://github.com/Moya/Moya/blob/master/Vision.md">vision document</a>.</p>
<h2 id='sample-projects' class='heading'>Sample Projects</h2>
<p>We have provided two sample projects in the repository. To use it download the repo, run <code>carthage update</code> to download the required libraries and open <a href="https://github.com/Moya/Moya/tree/master/Moya.xcodeproj">Moya.xcodeproj</a>. You’ll see two schemes: <code>Basic</code> and <code>Multi-Target</code> - select one and then build & run! Source files for these are in the <code>Examples</code> directory in project navigator. Have fun!</p>
<h2 id='project-status' class='heading'>Project Status</h2>
<p>This project is actively under development, and is being used in <a href="https://github.com/Artsy/eidolon">Artsy’s
new auction app</a>. We consider it
ready for production use.</p>
<h2 id='installation' class='heading'>Installation</h2>
<h3 id='moya-version-vs-swift-version' class='heading'>Moya version vs Swift version.</h3>
<p>Below is a table that shows which version of Moya you should use for
your Swift version.</p>
<table><thead>
<tr>
<th>Swift</th>
<th>Moya</th>
<th>RxMoya</th>
<th>ReactiveMoya</th>
</tr>
</thead><tbody>
<tr>
<td>5.X</td>
<td>>= 13.0.0</td>
<td>>= 13.0.0</td>
<td>>= 13.0.0</td>
</tr>
<tr>
<td>4.X</td>
<td>9.0.0 - 12.0.1</td>
<td>10.0.0 - 12.0.1</td>
<td>9.0.0 - 12.0.1</td>
</tr>
<tr>
<td>3.X</td>
<td>8.0.0 - 8.0.5</td>
<td>8.0.0 - 8.0.5</td>
<td>8.0.0 - 8.0.5</td>
</tr>
<tr>
<td>2.3</td>
<td>7.0.2 - 7.0.4</td>
<td>7.0.2 - 7.0.4</td>
<td>7.0.2 - 7.0.4</td>
</tr>
<tr>
<td>2.2</td>
<td><= 7.0.1</td>
<td><= 7.0.1</td>
<td><= 7.0.1</td>
</tr>
</tbody></table>
<p><em>Note: If you are using Swift 4.2 in your project, but you are using Xcode 10.2, Moya 13 should work correctly even though we use Swift 5.0.</em></p>
<p><strong>Upgrading to a new major version of Moya? Check out our <a href="https://github.com/Moya/Moya/blob/master/docs/MigrationGuides">migration guides</a>.</strong></p>
<h3 id='swift-package-manager' class='heading'>Swift Package Manager</h3>
<p>To integrate using Apple’s Swift package manager, add the following as a dependency to your <code>Package.swift</code>:</p>
<pre class="highlight swift"><code><span class="o">.</span><span class="nf">package</span><span class="p">(</span><span class="nv">url</span><span class="p">:</span> <span class="s">"https://github.com/Moya/Moya.git"</span><span class="p">,</span> <span class="o">.</span><span class="nf">upToNextMajor</span><span class="p">(</span><span class="nv">from</span><span class="p">:</span> <span class="s">"14.0.0"</span><span class="p">))</span>
</code></pre>
<p>and then specify <code>"Moya"</code> as a dependency of the Target in which you wish to use Moya.
If you want to use reactive extensions, add also <code>"ReactiveMoya"</code> or <code>"RxMoya"</code> as your Target dependency respectively.
Here’s an example <code>PackageDescription</code>:</p>
<pre class="highlight swift"><code><span class="c1">// swift-tools-version:5.0</span>
<span class="kd">import</span> <span class="kt">PackageDescription</span>
<span class="k">let</span> <span class="nv">package</span> <span class="o">=</span> <span class="kt">Package</span><span class="p">(</span>
<span class="nv">name</span><span class="p">:</span> <span class="s">"MyPackage"</span><span class="p">,</span>
<span class="nv">products</span><span class="p">:</span> <span class="p">[</span>
<span class="o">.</span><span class="nf">library</span><span class="p">(</span>
<span class="nv">name</span><span class="p">:</span> <span class="s">"MyPackage"</span><span class="p">,</span>
<span class="nv">targets</span><span class="p">:</span> <span class="p">[</span><span class="s">"MyPackage"</span><span class="p">]),</span>
<span class="p">],</span>
<span class="nv">dependencies</span><span class="p">:</span> <span class="p">[</span>
<span class="o">.</span><span class="nf">package</span><span class="p">(</span><span class="nv">url</span><span class="p">:</span> <span class="s">"https://github.com/Moya/Moya.git"</span><span class="p">,</span> <span class="o">.</span><span class="nf">upToNextMajor</span><span class="p">(</span><span class="nv">from</span><span class="p">:</span> <span class="s">"14.0.0"</span><span class="p">))</span>
<span class="p">],</span>
<span class="nv">targets</span><span class="p">:</span> <span class="p">[</span>
<span class="o">.</span><span class="nf">target</span><span class="p">(</span>
<span class="nv">name</span><span class="p">:</span> <span class="s">"MyPackage"</span><span class="p">,</span>
<span class="nv">dependencies</span><span class="p">:</span> <span class="p">[</span><span class="s">"ReactiveMoya"</span><span class="p">])</span>
<span class="p">]</span>
<span class="p">)</span>
</code></pre>
<p>Note: If you are using <strong>ReactiveMoya</strong>, we are using <a href="https://github.com/Moya/ReactiveSwift">our own fork of ReactiveSwift</a>. This fork adds 2 commits to remove testing dependencies on releases (starting 6.1.0). This is to prevent Xcode Previews on Xcode 11/11.1 to build testing dependencies (FB7316430). If you don’t want to use our fork, you can just add another dependency to your SPM package list: <code>[email protected]:ReactiveCocoa/ReactiveSwift.git</code> and it should fetch the original repository.</p>
<h3 id='accio' class='heading'>Accio</h3>
<p><a href="https://github.com/JamitLabs/Accio">Accio</a> is a dependency manager based on SwiftPM which can build frameworks for iOS/macOS/tvOS/watchOS. Therefore the integration steps of Moya are exactly the same as described above. Once your <code>Package.swift</code> file is configured, run <code>accio update</code> instead of <code>swift package update</code>.</p>
<h3 id='cocoapods' class='heading'>CocoaPods</h3>
<p>For Moya, use the following entry in your Podfile:</p>
<pre class="highlight ruby"><code><span class="n">pod</span> <span class="s1">'Moya'</span><span class="p">,</span> <span class="s1">'~> 14.0'</span>
<span class="c1"># or </span>
<span class="n">pod</span> <span class="s1">'Moya/RxSwift'</span><span class="p">,</span> <span class="s1">'~> 14.0'</span>
<span class="c1"># or</span>
<span class="n">pod</span> <span class="s1">'Moya/ReactiveSwift'</span><span class="p">,</span> <span class="s1">'~> 14.0'</span>
</code></pre>
<p>Then run <code>pod install</code>.</p>
<p>In any file you’d like to use Moya in, don’t forget to
import the framework with <code>import Moya</code>.</p>
<h3 id='carthage' class='heading'>Carthage</h3>
<p>Carthage users can point to this repository and use whichever
generated framework they’d like, <code>Moya</code>, <code>RxMoya</code>, or <code>ReactiveMoya</code>.</p>
<p>Make the following entry in your Cartfile:</p>
<pre class="highlight plaintext"><code>github "Moya/Moya" ~> 14.0
</code></pre>
<p>Then run <code>carthage update</code>.</p>
<p>If this is your first time using Carthage in the project, you’ll need to go through some additional steps as explained <a href="https://github.com/Carthage/Carthage#adding-frameworks-to-an-application">over at Carthage</a>.</p>
<blockquote>
<p>NOTE: At this time, Carthage does not provide a way to build only specific repository submodules. All submodules and their dependencies will be built with the above command. However, you don’t need to copy frameworks you aren’t using into your project. For instance, if you aren’t using <code><a href="reactiveswift.html">ReactiveSwift</a></code>, feel free to delete that framework along with <code>ReactiveMoya</code> from the Carthage Build directory after <code>carthage update</code> completes. Or if you are using <code><a href="reactiveswift.html">ReactiveSwift</a></code> but not <code><a href="rxswift.html">RxSwift</a></code>, then <code>RxMoya</code>, <code>RxTest</code>, <code>RxCocoa</code>, etc. can safely be deleted.</p>
</blockquote>
<h3 id='manually' class='heading'>Manually</h3>
<ul>
<li>Open up Terminal, <code>cd</code> into your top-level project directory, and run the following command <em>if</em> your project is not initialized as a git repository:</li>
</ul>
<pre class="highlight shell"><code><span class="nv">$ </span>git init
</code></pre>
<ul>
<li>Add Alamofire, Result & Moya as a git <a href="http://git-scm.com/docs/git-submodule">submodule</a> by running the following commands:</li>
</ul>
<pre class="highlight shell"><code><span class="nv">$ </span>git submodule add https://github.com/Alamofire/Alamofire.git
<span class="nv">$ </span>git submodule add https://github.com/antitypical/Result.git
<span class="nv">$ </span>git submodule add https://github.com/Moya/Moya.git
</code></pre>
<ul>
<li>Open the new <code>Alamofire</code> folder, and drag the <code>Alamofire.xcodeproj</code> into the Project Navigator of your application’s Xcode project. Do the same with the <code>Result.xcodeproj</code> in the <code>Result</code> folder and <code>Moya.xcodeproj</code> in the <code>Moya</code> folder.</li>
</ul>
<blockquote>
<p>They should appear nested underneath your application’s blue project icon. Whether it is above or below all the other Xcode groups does not matter.</p>
</blockquote>
<ul>
<li>Verify that the deployment targets of the <code>xcodeproj</code>s match that of your application target in the Project Navigator.</li>
<li>Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the <q>Targets</q> heading in the sidebar.</li>
<li>In the tab bar at the top of that window, open the <q>General</q> panel.</li>
<li>Click on the <code>+</code> button under the <q>Embedded Binaries</q> section.</li>
<li>You will see two different <code>Alamofire.xcodeproj</code> folders each with two different versions of the <code>Alamofire.framework</code> nested inside a <code>Products</code> folder.</li>
</ul>
<blockquote>
<p>It does not matter which <code>Products</code> folder you choose from, but it does matter whether you choose the top or bottom <code>Alamofire.framework</code>.</p>
</blockquote>
<ul>
<li>Select the top <code>Alamofire.framework</code> for iOS and the bottom one for macOS.</li>
</ul>
<blockquote>
<p>You can verify which one you selected by inspecting the build log for your project. The build target for <code>Alamofire</code> will be listed as either <code>Alamofire iOS</code>, <code>Alamofire macOS</code>, <code>Alamofire tvOS</code> or <code>Alamofire watchOS</code>.</p>
</blockquote>
<ul>
<li>Click on the <code>+</code> button under <q>Embedded Binaries</q> again and add the build target you need for <code>Result</code>.</li>
<li><p>Click on the <code>+</code> button again and add the correct build target for <code>Moya</code>.</p></li>
<li><p>And that’s it!</p></li>
</ul>
<blockquote>
<p>The three frameworks are automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.</p>
</blockquote>
<h2 id='usage' class='heading'>Usage</h2>
<p>After <a href="https://github.com/Moya/Moya/blob/master/docs/Examples/Basic.md">some setup</a>, using Moya is really simple. You can access an API like this:</p>
<pre class="highlight swift"><code><span class="n">provider</span> <span class="o">=</span> <span class="kt">MoyaProvider</span><span class="o"><</span><span class="kt">GitHub</span><span class="o">></span><span class="p">()</span>
<span class="n">provider</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="n">zen</span><span class="p">)</span> <span class="p">{</span> <span class="n">result</span> <span class="k">in</span>
<span class="k">switch</span> <span class="n">result</span> <span class="p">{</span>
<span class="k">case</span> <span class="kd">let</span> <span class="o">.</span><span class="nf">success</span><span class="p">(</span><span class="n">moyaResponse</span><span class="p">):</span>
<span class="k">let</span> <span class="nv">data</span> <span class="o">=</span> <span class="n">moyaResponse</span><span class="o">.</span><span class="n">data</span>
<span class="k">let</span> <span class="nv">statusCode</span> <span class="o">=</span> <span class="n">moyaResponse</span><span class="o">.</span><span class="n">statusCode</span>
<span class="c1">// do something with the response data or statusCode</span>
<span class="k">case</span> <span class="kd">let</span> <span class="o">.</span><span class="nf">failure</span><span class="p">(</span><span class="n">error</span><span class="p">):</span>
<span class="c1">// this means there was a network failure - either the request</span>
<span class="c1">// wasn't sent (connectivity), or no response was received (server</span>
<span class="c1">// timed out). If the server responds with a 4xx or 5xx error, that</span>
<span class="c1">// will be sent as a ".success"-ful response.</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
<p>That’s a basic example. Many API requests need parameters. Moya encodes these
into the enum you use to access the endpoint, like this:</p>
<pre class="highlight swift"><code><span class="n">provider</span> <span class="o">=</span> <span class="kt">MoyaProvider</span><span class="o"><</span><span class="kt">GitHub</span><span class="o">></span><span class="p">()</span>
<span class="n">provider</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="nf">userProfile</span><span class="p">(</span><span class="s">"ashfurrow"</span><span class="p">))</span> <span class="p">{</span> <span class="n">result</span> <span class="k">in</span>
<span class="c1">// do something with the result</span>
<span class="p">}</span>
</code></pre>
<p>No more typos in URLs. No more missing parameter values. No more messing with
parameter encoding.</p>
<p>For more examples, see the <a href="https://github.com/Moya/Moya/blob/master/docs/Examples">documentation</a>.</p>
<h2 id='reactive-extensions' class='heading'>Reactive Extensions</h2>
<p>Even cooler are the reactive extensions. Moya provides reactive extensions for
<a href="https://github.com/ReactiveCocoa/ReactiveSwift">ReactiveSwift</a> and
<a href="https://github.com/ReactiveX/RxSwift">RxSwift</a>.</p>
<h3 id='reactiveswift' class='heading'>ReactiveSwift</h3>
<p><a href="https://github.com/Moya/Moya/blob/master/docs/ReactiveSwift.md"><code><a href="reactiveswift.html">ReactiveSwift</a></code> extension</a> provides both <code>reactive.request(:callbackQueue:)</code> and
<code>reactive.requestWithProgress(:callbackQueue:)</code> methods that immediately return
<code>SignalProducer</code>s that you can start, bind, map, or whatever you want to do.
To handle errors, for instance, we could do the following:</p>
<pre class="highlight swift"><code><span class="n">provider</span> <span class="o">=</span> <span class="kt">MoyaProvider</span><span class="o"><</span><span class="kt">GitHub</span><span class="o">></span><span class="p">()</span>
<span class="n">provider</span><span class="o">.</span><span class="n">reactive</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="nf">userProfile</span><span class="p">(</span><span class="s">"ashfurrow"</span><span class="p">))</span><span class="o">.</span><span class="n">start</span> <span class="p">{</span> <span class="n">event</span> <span class="k">in</span>
<span class="k">switch</span> <span class="n">event</span> <span class="p">{</span>
<span class="k">case</span> <span class="kd">let</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="n">response</span><span class="p">):</span>
<span class="n">image</span> <span class="o">=</span> <span class="kt">UIImage</span><span class="p">(</span><span class="nv">data</span><span class="p">:</span> <span class="n">response</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
<span class="k">case</span> <span class="kd">let</span> <span class="o">.</span><span class="nf">failed</span><span class="p">(</span><span class="n">error</span><span class="p">):</span>
<span class="nf">print</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
<span class="k">default</span><span class="p">:</span>
<span class="k">break</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
<h3 id='rxswift' class='heading'>RxSwift</h3>
<p><a href="https://github.com/Moya/Moya/blob/master/docs/RxSwift.md"><code><a href="rxswift.html">RxSwift</a></code> extension</a> also provide both <code>rx.request(:callbackQueue:)</code> and
<code>rx.requestWithProgress(:callbackQueue:)</code> methods, but return type is
different for both. In case of a normal <code>rx.request(:callbackQueue)</code>, the
return type is <code>Single<Response></code> which emits either single element or an
error. In case of a <code>rx.requestWithProgress(:callbackQueue:)</code>, the return
type is <code>Observable<ProgressResponse></code>, since we may get multiple events
from progress and one last event which is a response.</p>
<p>To handle errors, for instance, we could do the following:</p>
<pre class="highlight swift"><code><span class="n">provider</span> <span class="o">=</span> <span class="kt">MoyaProvider</span><span class="o"><</span><span class="kt">GitHub</span><span class="o">></span><span class="p">()</span>
<span class="n">provider</span><span class="o">.</span><span class="n">rx</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="nf">userProfile</span><span class="p">(</span><span class="s">"ashfurrow"</span><span class="p">))</span><span class="o">.</span><span class="n">subscribe</span> <span class="p">{</span> <span class="n">event</span> <span class="k">in</span>
<span class="k">switch</span> <span class="n">event</span> <span class="p">{</span>
<span class="k">case</span> <span class="kd">let</span> <span class="o">.</span><span class="nf">success</span><span class="p">(</span><span class="n">response</span><span class="p">):</span>
<span class="n">image</span> <span class="o">=</span> <span class="kt">UIImage</span><span class="p">(</span><span class="nv">data</span><span class="p">:</span> <span class="n">response</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
<span class="k">case</span> <span class="kd">let</span> <span class="o">.</span><span class="nf">error</span><span class="p">(</span><span class="n">error</span><span class="p">):</span>
<span class="nf">print</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
<p>In addition to the option of using signals instead of callback blocks, there are
also a series of signal operators for RxSwift and ReactiveSwift that will attempt
to map the data received from the network response into either an image, some JSON,
or a string, with <code>mapImage()</code>, <code>mapJSON()</code>, and <code>mapString()</code>, respectively. If the mapping is unsuccessful, you’ll get an error on the signal. You also get handy methods
for filtering out certain status codes. This means that you can place your code for
handling API errors like 400’s in the same places as code for handling invalid
responses.</p>
<h2 id='community-projects' class='heading'>Community Projects</h2>
<p><a href="https://github.com/Moya/Moya/blob/master/docs/CommunityProjects.md">Moya has a great community around it and some people have created some very helpful extensions</a>.</p>
<h2 id='contributing' class='heading'>Contributing</h2>
<p>Hey! Do you like Moya? Awesome! We could actually really use your help!</p>
<p>Open source isn’t just writing code. Moya could use your help with any of the
following:</p>
<ul>
<li>Finding (and reporting!) bugs.</li>
<li>New feature suggestions.</li>
<li>Answering questions on issues.</li>
<li>Documentation improvements.</li>
<li>Reviewing pull requests.</li>
<li>Helping to manage issue priorities.</li>
<li>Fixing bugs/new features.</li>
</ul>
<p>If any of that sounds cool to you, send a pull request! After your first
contribution, we will add you as a member to the repo so you can merge pull
requests and help steer the ship :ship: You can read more details about that <a href="https://github.com/Moya/Moya/blob/master/Contributing.md">in our contributor guidelines</a>.</p>
<p>Moya’s community has a tremendous positive energy, and the maintainers are committed to keeping things awesome. Like <a href="https://github.com/CocoaPods/CocoaPods/wiki/Communication-&-Design-Rules">in the CocoaPods community</a>, always assume positive intent; even if a comment sounds mean-spirited, give the person the benefit of the doubt.</p>
<p>Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by <a href="https://github.com/Moya/Moya/blob/master/Code%20of%20Conduct.md">its terms</a>.</p>
<h3 id='adding-new-source-files' class='heading'>Adding new source files</h3>
<p>If you add or remove a source file from Moya, a corresponding change needs to be made to the <code>Moya.xcodeproj</code> project at the root of this repository. This project is used for Carthage. Don’t worry, you’ll get an automated warning when submitting a pull request if you forget.</p>
<h3 id='help-us-improve-moya-documentation' class='heading'>Help us improve Moya documentation</h3>
<p>Whether you’re a core member or a user trying it out for the first time, you can make a valuable contribution to Moya by improving the documentation. Help us by:</p>
<ul>
<li>sending us feedback about something you thought was confusing or simply missing</li>
<li>suggesting better wording or ways of explaining certain topics</li>
<li>sending us a pull request via GitHub</li>
<li>improving the <a href="https://github.com/Moya/Moya/blob/master/Readme_CN.md">Chinese documentation</a></li>
</ul>
<h2 id='license' class='heading'>License</h2>
<p>Moya is released under an MIT license. See <a href="https://github.com/Moya/Moya/blob/master/License.md">License.md</a> for more information.</p>
</section>
</section>
<section id="footer">
<p>© 2020 <a class="link" href="https://github.com/Moya/Moya" target="_blank" rel="external">Ash Furrow</a>. All rights reserved. (Last updated: 2020-02-24)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.6</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
</div>
</body>
</div>
</html>