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

Proposed Refactor for Heartbeat Resources #11

Open
theladyjaye opened this issue Aug 7, 2016 · 2 comments
Open

Proposed Refactor for Heartbeat Resources #11

theladyjaye opened this issue Aug 7, 2016 · 2 comments

Comments

@theladyjaye
Copy link
Contributor

theladyjaye commented Aug 7, 2016

What do you think about making the Heartbeat stuff USE another SwiftyHue API as opposed to right now it's all locked together.

For example, Heartbeat should could call a function to give it it's requested resources, in that way other clients could call the same thing if they didn't want to use the heartbeat.

Example implementation:

    public func fetch<Error: Swift.Error, Resource: BridgeResource & BridgeResourceDictGenerator where Resource.AssociatedBridgeResourceType == Resource>(resource: HeartbeatBridgeResourceType, callback:(Result<[String: Resource], Error>) -> ()){

        guard let authorization = _authorization else {
            log.error("No 'Authorization' set'")
            return
        }

        let url = "http://\(authorization.ipAddress)/api/\(authorization.username)/\(resource.rawValue.lowercased())"

        Alamofire.request(.GET, url)
            .responseJSON { response in

                switch response.result {
                case .success(let data):

                    let resources = Resource.dictionaryFromResourcesJSON(data as! JSON)
                    let result = Result<[String: Resource], Error>.success(resources)
                    callback(result)

                case .failure(let error):
                    let result = Result<[String: Resource], Error>.failure(error as! Error)
                    callback(result)
                }
        }
    }


    public func fetchLights(_ callback: (Result<[String: Light], NSError>) -> ()){
        fetch(resource: .lights, callback: callback)
    }

call site example:

something.fetchLights{
    (result) in
            switch result {
            case .success(let lights):
                print(lights)

            case .failure(let error):
                print(error)
            }
        }

Now heartbeat could call it on an interval, or anything else could just call it once.

Keeping with what is there, since a SendAPI exists, it would be introducing a ResourceAPI

@Spriter
Copy link
Owner

Spriter commented Aug 7, 2016

Not a bad idea. We have also discussed this before. We will discuss this again.

@SamwiseChepoackra
Copy link

Do you have any plans to release in swift 4?

Also are there any resources on implanting groups in swift?

Thanks for your work here

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

No branches or pull requests

3 participants