Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request decorators, chains, and repeats #98

Merged
merged 22 commits into from Aug 17, 2016
Merged

Conversation

pcantrell
Copy link
Member

@pcantrell pcantrell commented Aug 15, 2016

This PR covers three related areas of functionality:

  • Configuration.decorateRequests(...) replaces beforeStartingRequest, and allows you to replace requests based on configuration instead of just listening to them.
  • Request.chained(...) allows you to chain multiple requests together and have them behave as if they were one request.
  • Request.repeated() allows you to send a request again, picking up configuration changes.

Putting it all together, this allows you to configure sophisticated service-wide retry behavior. For example, this hypothetical code automatically renews auth tokens and retries unauthorized requests for a whole service:

service.configure {
  $0.config.decorateRequests {
    refreshTokenOnAuthFailure($1)
  }
}

// Refactor away this pyramid of doom however you see fit
func refreshTokenOnAuthFailure(request: Request) -> Request {
  request.chained {
      guard case .Failure(let error) = $0.response where error.httpStatusCode == 401 else {
        return .UseThisResponse
      }

      return .PassTo(refreshToken().chained {
        if case .Failure = $0.response {
          return .UseThisResponse
        } else {
          return .PassTo(request.repeated())
        }
      })
    }
  }
}

func refreshToken() -> Request {
  // request new token, add onSuccess handler to update service config
}

@vdka and @jyounus: This should give you at least some of what you are looking for with #48.

This work is a first draft. Kick the tires!

@pcantrell pcantrell mentioned this pull request Aug 16, 2016
@jyounus
Copy link

jyounus commented Aug 16, 2016

Hey @pcantrell, thanks for implementing this functionality. The project I was working on is done now, we ended up using a different API lib to accomplish what we were trying to do. I'm definitely going to use Siesta in the future to give this a try!

@pcantrell pcantrell changed the title Request decorators Request decorators, chains, and repeats Aug 17, 2016
@pcantrell pcantrell merged commit d34c5e7 into master Aug 17, 2016
@pcantrell pcantrell deleted the request-decorators branch August 17, 2016 15:09
@pcantrell pcantrell mentioned this pull request Aug 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants