Git Product home page Git Product logo

Comments (6)

tcr-ableton avatar tcr-ableton commented on August 23, 2024

Implementation Idea:

interface

type AuthenticationProvider interface {
    Authenticate() (AccessToken, error)
}

usage

travis.NewClient(TRAVIS_API_DEFAULT_URL, GithubAuthProvider("mytoken"))
// or
travis.NewClient(TRAVIS_API_DEFAULT_URL, TravisAuthProvider())

from go-travis.

tcr-ableton avatar tcr-ableton commented on August 23, 2024

A possible implementation of it would consist in the following:

interface implementation

type GithubAuthProvider struct {
        GithubToken string
}

type TravisAuthProvider struct {
        TravisToken string
}

func (*GithubAuthProvider) Authenticate() (AccessToken, error) {
    // get and return a travis token from the api using the github token 
}

func (*TravisAuthProvider) Authenticate() (AccessToken, error) {
    // return the TravisToken
}

client implementation

func (c *Client) Authenticate(auth AuthenticationProvider) error {
    c.client.Headers["Authorization"] = "token " + auth.Authenticate()
}

from go-travis.

tcr-ableton avatar tcr-ableton commented on August 23, 2024

Spotted a problem in the implementation of this design: the GithubAuthProvider would need the client instance to query travis for a travis token. Which would actually lead to an impossible design as the client constructor would need to pass himself to the provider.

# Not possible
client := NewClient(TRAVIS_API_DEFAULT_URL, GithubAuthProvider(clientIHaventBuiltJustYet, token))

from go-travis.

tcr-ableton avatar tcr-ableton commented on August 23, 2024

A workaround could be to add a client attribute to the provider when relevant. To pass the provider to the constructor with its client attribute uninitialized. And when calling Client.Authenticate initializing the provider client attribute, and calling its Authenticate method.

func (c *Client) Authenticate(auth AuthenticationProvider) error {
    auth.client = c.client
    c.client.Headers["Authorization"] = "token " + auth.Authenticate()
}

But this sounds super hacky, and introduces mutability, which was exactly the aspect of current implementation we are trying to get rid of.

from go-travis.

nsh-ableton avatar nsh-ableton commented on August 23, 2024

Hey @tcr-ableton, do you think it would make sense to close this?

from go-travis.

mst-ableton avatar mst-ableton commented on August 23, 2024

Given the state of things, I don't think this issue will be addressed anytime soon.

from go-travis.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.